简单地判断运行的设备的类型
/**
* 判断是否是iphone
*
* @return YES 是
*/
+ (BOOL)isIphone
{
NSString* deviceType = [UIDevice currentDevice].model;
return [deviceType rangeOfString:@"iPhone"].length > 0;
}
/**
* 判断是否是ipod
*
* @return YES 是
*/
+ (BOOL)isIpod
{
NSString* deviceType = [UIDevice currentDevice].model;
return [deviceType rangeOfString:@"iPod"].length > 0;
}
当然上边的只是简单的判断 更详细的判断可以用下边的方法,可以判断详细的设备信息,当然我的信息可能不是很全,每年都会新增很多不同的版本具体的可以自己扩展
//手机型号(这跟系统获取的不一样,这是获取iPhone5,4,6之类的)要导入
- (NSString*)deviceString
{
// 可能需要更新
// 需要#import "sys/utsname.h"
struct utsname systemInfo;
uname(&systemInfo);
NSString *deviceString = [NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding];
if ([deviceString isEqualToString:@"iPhone1,1"]) return @"iPhone 1G";
if ([deviceString isEqualToString:@"iPhone1,2"]) return @"iPhone 3G";
if ([deviceString isEqualToString:@"iPhone2,1"]) return @"iPhone 3GS";
if ([deviceString isEqualToString:@"iPhone3,1"]) return @"iPhone 4";
if ([deviceString isEqualToString:@"iPhone4,1"]) return @"iPhone 4S";
if ([deviceString isEqualToString:@"iPhone5,2"]) return @"iPhone 5";
if ([deviceString isEqualToString:@"iPhone3,2"]) return @"Verizon iPhone 4";
if ([deviceString isEqualToString:@"iPod1,1"]) return @"iPod Touch 1G";
if ([deviceString isEqualToString:@"iPod2,1"]) return @"iPod Touch 2G";
if ([deviceString isEqualToString:@"iPod3,1"]) return @"iPod Touch 3G";
if ([deviceString isEqualToString:@"iPod4,1"]) return @"iPod Touch 4G";
if ([deviceString isEqualToString:@"iPad1,1"]) return @"iPad";
if ([deviceString isEqualToString:@"iPad2,1"]) return @"iPad 2 (WiFi)";
if ([deviceString isEqualToString:@"iPad2,2"]) return @"iPad 2 (GSM)";
if ([deviceString isEqualToString:@"iPad2,3"]) return @"iPad 2 (CDMA)";
if ([deviceString isEqualToString:@"i386"]) return @"Simulator";
if ([deviceString isEqualToString:@"x86_64"]) return @"Simulator";
NSLog(@"NOTE: Unknown device type: %@", deviceString);
return deviceString;
}
/**
* 判断是否是ios8
*
* @return YES 是
*/
+ (BOOL)isIos8
{
return [[UIDevice currentDevice].systemVersion floatValue] >= 8.0;
}
虽然上边可以把相应地方法写成类方法,方便大家快速地调用,但是还是没有抽成宏的方便
#define iPhone ([[UIDevice currentDevice].model rangeOfString:@"iPhone"].length > 0)
#define iPod ([[UIDevice currentDevice].model rangeOfString:@"iPod"].length > 0)
#define ios8 ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0)
这样的话就更加的方便了