2012-09-23 26 views
4

嗨,我想檢查iOS設備上的iPhone設備版本。如何在iOS中查看iPhone設備版本?

我的意思是,當前正在運行的設備是iPhone 4或iPhone 5

我需要檢查設備,是iPhone 5或沒有?

因爲我的應用程序需要知道iPhone 5的問題。

那麼我該怎麼辦?

回答

9

添加以下代碼:

if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone){ 
     if ([[UIScreen mainScreen] respondsToSelector: @selector(scale)]) { 
      CGSize result = [[UIScreen mainScreen] bounds].size; 
      CGFloat scale = [UIScreen mainScreen].scale; 
      result = CGSizeMake(result.width * scale, result.height * scale); 

      if(result.height == 960) { 
       NSLog(@"iPhone 4 Resolution"); 
       resolution_number = 1; 
      } 
      if(result.height == 1136) { 
       NSLog(@"iPhone 5 Resolution"); 
      } 
     } 
     else{ 
      NSLog(@"Standard Resolution"); 
     } 
    } 
9

添加此宏到您的代碼:

#define HEIGHT_IPHONE_5 568 
#define IS_IPHONE ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) 
#define IS_IPHONE_5 ([[UIScreen mainScreen] bounds ].size.height == HEIGHT_IPHONE_5) 

然後只檢查每當你需要..

if (IS_IPHONE_5) { 
    //Code for iPhone5 
}else{ 
    //Code for earlier version 
} 
1

我居然發現使用#define那是訣竅

#define IS_IPHONE_5 (fabs((double)[[UIScreen mainScreen] bounds].size.height - (double) 568) < DBL_EPSILON) 
5

在此基礎上question您可以將您的使用 本該模型確定的答案:

NSString *deviceModel = [UIDevice currentDevice].model; 

結果可以是值之一:iPod的觸摸,iPhone,iPad的,iPhone模擬器,iPad的模擬器

您正在使用可能會創建這樣的UIDevice.h類類別來確定哪個版本的具體型號爲:

的UIDevice + Utilities.h

#import <UIKit/UIKit.h> 

@interface UIDevice (Utilities) 

- (CGFloat)deviceModelVersion; 

@end 

的UIDevice + Utilities.m

#import "UIDevice+Utilities.h" 
#include <sys/types.h> 
#include <sys/sysctl.h> 

@implementation UIDevice (Utilities) 

- (CGFloat)deviceModelVersion 
{ 
    size_t size; 
    sysctlbyname("hw.machine", NULL, &size, NULL, 0); 
    char *machine = malloc(size); 
    sysctlbyname("hw.machine", machine, &size, NULL, 0); 
    NSString *platform = [NSString stringWithUTF8String:machine]; 
    free(machine); 

    if ([platform rangeOfString:@"iPhone"].location != NSNotFound) 
    { 
     return [[[platform stringByReplacingOccurrencesOfString:@"iPhone" withString:@""] stringByReplacingOccurrencesOfString:@"," withString:@"."] floatValue]; 
    } 
    else if ([platform rangeOfString:@"iPad"].location != NSNotFound) 
    { 
     return [[[platform stringByReplacingOccurrencesOfString:@"iPad" withString:@""] stringByReplacingOccurrencesOfString:@"," withString:@"."] floatValue]; 
    } 
    else if ([platform rangeOfString:@"iPod"].location != NSNotFound) 
    { 
     return [[[platform stringByReplacingOccurrencesOfString:@"iPod" withString:@""] stringByReplacingOccurrencesOfString:@"," withString:@"."] floatValue]; 
    } 
    else if ([platform rangeOfString:@"i386"].location != NSNotFound || [platform rangeOfString:@"x86_64"].location != NSNotFound) 
    { 
     return -1.0; //Currently it is not possible (or maybe it is, but I do not know) 
        //which type of simulator device model version your app is running 
        //so I am returning -1.0 device model version for all simulators types 
    } 

    return 0.0; 
} 

@end 

實施例如何調用函數deviceModelVersion

CGFloat deviceModelVersion = [UIDevice currentDevice].deviceModelVersion; 

可能的結果可以1.0,1.1,...,2.0,2.1,.. 3.0,3.1,...,4.0,4.1,...,5.0,5.1,...

要確定它是否是iPhone 5,你應該有

deviceModel : "iPhone" 

deviceModelVersion : >=5.0 and < 6.0 
+0

偉大的答案。這使得檢查範圍非常簡單,或者具有比可以用SYSTEM_VERSION_GREATER_THAN(..)完成的邏輯更類似的邏輯。愛它! – Marchy

0
#define IS_IPAD (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) 
#define IS_IPHONE (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) 
#define IS_IPHONE_4 (IS_IPHONE && [[UIScreen mainScreen] bounds].size.height == 480.0f) 
#define IS_IPHONE_5 (IS_IPHONE && [[UIScreen mainScreen] bounds].size.height == 568.0f) 
#define IS_IPHONE_6 (IS_IPHONE && [[UIScreen mainScreen] bounds].size.height == 667.0f) 
#define IS_IPHONE_6PLUS (IS_IPHONE && [[UIScreen mainScreen] nativeScale] == 3.0f) 
#define IS_IPHONE_6_PLUS (IS_IPHONE && [[UIScreen mainScreen] bounds].size.height == 736.0f) 
#define IS_RETINA ([[UIScreen mainScreen] scale] == 2.0f)