获取iOS设备的型号(iPhone4,iphone5,iPhone5s......)

  做自定义键盘,首先要适配所有的设备,所以在这里就要知道用户使用的是哪一款手机,下面总结了几种获取设备型号的方法;

  PS:这些也是我在网上自己找的,以防不时之需。

  方法一:

 1 //***宏定义

 2 /*[UIScreen instancesRespondToSelector:@selector(currentMode)];

 3 判断UIScreen 类中是否包含currentMode这个方法,

 4 若存在则调用CGSizeEqualToSize比较2个size大小是否相同,

 5 若相同返回yes,否则返回no。

 6 */

 7 #define iPhone5 ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(640, 1136), [[UIScreen mainScreen] currentMode].size) : NO)

 8 然后:

 9 if(iPhone5){

10 //***具体操作

11 }else{

12 

13 //***具体操作

14 

15 }

  方法二:

1 #define Screen_height   [[UIScreen mainScreen] bounds].size.height

2 #define Screen_width    [[UIScreen mainScreen] bounds].size.width 

  方法三:

  PS:注意,需要#import "sys/utsname.h" 

 1  + (NSString*)deviceString  

 2 {  

 3     // 需要#import "sys/utsname.h"  

 4     struct utsname systemInfo;  

 5     uname(&systemInfo);  

 6     NSString *deviceString = [NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding];  

 7        

 8     if ([deviceString isEqualToString:@"iPhone1,1"])    return @"iPhone 1G";  

 9     if ([deviceString isEqualToString:@"iPhone1,2"])    return @"iPhone 3G";  

10     if ([deviceString isEqualToString:@"iPhone2,1"])    return @"iPhone 3GS";  

11     if ([deviceString isEqualToString:@"iPhone3,1"])    return @"iPhone 4";  

12     if ([deviceString isEqualToString:@"iPhone4,1"])    return @"iPhone 4S";  

13     if ([deviceString isEqualToString:@"iPhone5,2"])    return @"iPhone 5";  

14     if ([deviceString isEqualToString:@"iPhone3,2"])    return @"Verizon iPhone 4";  

15     if ([deviceString isEqualToString:@"iPod1,1"])      return @"iPod Touch 1G";  

16     if ([deviceString isEqualToString:@"iPod2,1"])      return @"iPod Touch 2G";  

17     if ([deviceString isEqualToString:@"iPod3,1"])      return @"iPod Touch 3G";  

18     if ([deviceString isEqualToString:@"iPod4,1"])      return @"iPod Touch 4G";  

19     if ([deviceString isEqualToString:@"iPad1,1"])      return @"iPad";  

20     if ([deviceString isEqualToString:@"iPad2,1"])      return @"iPad 2 (WiFi)";  

21     if ([deviceString isEqualToString:@"iPad2,2"])      return @"iPad 2 (GSM)";  

22     if ([deviceString isEqualToString:@"iPad2,3"])      return @"iPad 2 (CDMA)";  

23     if ([deviceString isEqualToString:@"i386"])         return @"Simulator";  

24     if ([deviceString isEqualToString:@"x86_64"])       return @"Simulator";  

25     NSLog(@"NOTE: Unknown device type: %@", deviceString);  

26     return deviceString;  

27 }  
 1  + (NSString*)deviceString  

 2  2 {  

 3  3     // 需要#import "sys/utsname.h"  

 4  4     struct utsname systemInfo;  

 5  5     uname(&systemInfo);  

 6  6     NSString *deviceString = [NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding];  

 7  7        

 8  8     if ([deviceString isEqualToString:@"iPhone1,1"])    return @"iPhone 1G";  

 9  9     if ([deviceString isEqualToString:@"iPhone1,2"])    return @"iPhone 3G";  

10 10     if ([deviceString isEqualToString:@"iPhone2,1"])    return @"iPhone 3GS";  

11 11     if ([deviceString isEqualToString:@"iPhone3,1"])    return @"iPhone 4";  

12 12     if ([deviceString isEqualToString:@"iPhone4,1"])    return @"iPhone 4S";  

13 13     if ([deviceString isEqualToString:@"iPhone5,2"])    return @"iPhone 5";  

14 14     if ([deviceString isEqualToString:@"iPhone3,2"])    return @"Verizon iPhone 4";  

15 15     if ([deviceString isEqualToString:@"iPod1,1"])      return @"iPod Touch 1G";  

16 16     if ([deviceString isEqualToString:@"iPod2,1"])      return @"iPod Touch 2G";  

17 17     if ([deviceString isEqualToString:@"iPod3,1"])      return @"iPod Touch 3G";  

18 18     if ([deviceString isEqualToString:@"iPod4,1"])      return @"iPod Touch 4G";  

19 19     if ([deviceString isEqualToString:@"iPad1,1"])      return @"iPad";  

20 20     if ([deviceString isEqualToString:@"iPad2,1"])      return @"iPad 2 (WiFi)";  

21 21     if ([deviceString isEqualToString:@"iPad2,2"])      return @"iPad 2 (GSM)";  

22 22     if ([deviceString isEqualToString:@"iPad2,3"])      return @"iPad 2 (CDMA)";  

23 23     if ([deviceString isEqualToString:@"i386"])         return @"Simulator";  

24 24     if ([deviceString isEqualToString:@"x86_64"])       return @"Simulator";  

25 25     NSLog(@"NOTE: Unknown device type: %@", deviceString);  

26 26     return deviceString;  

27 27 }  
View Code

 

你可能感兴趣的:(iPhone)