iOS開發中各種版本、設備的區分

jopen 9年前發布 | 5K 次閱讀 Objective-C IOS

可以從 UIDevice 的屬性 model 得到在現在執行的環境。例子如下:

NSString *modelname = [[UIDevice currentDevice]model];
if ([modelname isEqualToString:@"iPhone"]) {
  // iPhone
}
if ([modelname isEqualToString:@"IPod Touch"]) {
  // iPod touch
}
if ([modelname isEqualToString:@"iPhone Simulator"]) {
  // iPhone Simulator
}

 也可以通過宏定義區分 

#if TARGET_OS_IPHONE
    // iPhone Device

endif

if TARGET_IPHONE_SIMULATOR

// iPhone Simulator

endif

if !TARGET_IPHONE_SIMULATOR

// iPhone Device

endif</pre>


 ios設備版本的區分-iphone3gs,iphone4.... 

ios提供了幾種c函數來獲得相應信息如下

struct utsname u;
uname(&u); ///-----get device struct info
NSString *machine = [NSString stringWithCString:u.machine];

if ([machine isEqualToString:@"iPhone1,1"]) { // iPhone 1G } if ([machine isEqualToString:@"iPhone1,2"]) { // iPhone 3G } if ([machine isEqualToString:@"iPhone2,1"]) { // iPhone 3GS } if ([machine isEqualToString:@"iPod1,1"]) { // iPod touch 1G } if ([machine isEqualToString:@"iPod2,1"]) { // iPod touch 2G } if ([machine isEqualToString:@"iPod3,1"]) { // iPod touch Late2009 } </pre>

 或者

- (NSString ) platform
{
  size_t size;
  sysctlbyname("hw.machine", NULL, &size, NULL, 0);
  char machine = malloc(size);
  sysctlbyname("hw.machine", machine, &size, NULL, 0);///-----get device struct info

/ Possible values: "iPhone1,1" = iPhone 1G "iPhone1,2" = iPhone 3G "iPhone2,1" = iPhone 3GS "iPod1,1" = iPod touch 1G "iPod2,1" = iPod touch 2G / NSString *platform = [NSString stringWithCString:machine];

free(machine); return platform; } </pre>

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!