IOS代码判断设备类型

判断IOS设备类型一般会使用

//设备名称
return [UIDevice currentDevice].name;

//设备型号,只可得到是何设备,无法得到是第几代设备
return [UIDevice currentDevice].model;

//系统版本型号,如iPhone OS 
return [UIDevice currentDevice].systemVersion;

//系统版本名称,如6.1.3
return [UIDevice currentDevice].systemName;

但是,这样只能区分iPhone,iPad


真正能够实现设备类型区分的方法如下

#import 

NSString*
machineName()
{
    struct utsname systemInfo;
    uname(&systemInfo);

    return [NSString stringWithCString:systemInfo.machine
                              encoding:NSUTF8StringEncoding];
}

结果可能为如下的以逗号分隔的字符串

@"i386"      on the simulator
@"iPod1,1"   on iPod Touch
@"iPod2,1"   on iPod Touch Second Generation
@"iPod3,1"   on iPod Touch Third Generation
@"iPod4,1"   on iPod Touch Fourth Generation
@"iPhone1,1" on iPhone
@"iPhone1,2" on iPhone 3G
@"iPhone2,1" on iPhone 3GS
@"iPad1,1"   on iPad
@"iPad2,1"   on iPad 2
@"iPad3,1"   on 3rd Generation iPad
@"iPhone3,1" on iPhone 4
@"iPhone4,1" on iPhone 4S
@"iPhone5,1" on iPhone 5 (model A1428, AT&T/Canada)
@"iPhone5,2" on iPhone 5 (model A1429, everything else)
@"iPad3,4" on 4th Generation iPad
@"iPad2,5" on iPad Mini
@"iPhone5,3" on iPhone 5c (model A1456, A1532 | GSM)
@"iPhone5,4" on iPhone 5c (model A1507, A1516, A1526 (China), A1529 | Global)
@"iPhone6,1" on iPhone 5s (model A1433, A1533 | GSM)
@"iPhone6,2" on iPhone 5s (model A1457, A1518, A1528 (China), A1530 | Global)
@"iPad4,1" on 5th Generation iPad (iPad Air) - Wifi
@"iPad4,2" on 5th Generation iPad (iPad Air) - Cellular
@"iPad4,4" on 2nd Generation iPad Mini - Wifi
@"iPad4,5" on 2nd Generation iPad Mini - Cellular

为了简化代码,网上找到了如下实现,是通过NSDictionary实现快速查询

#import 

NSString *machineName()
{
    struct utsname systemInfo;
    uname(&systemInfo);

    NSString *iOSDeviceModelsPath = [[NSBundle mainBundle] pathForResource:@"iOSDeviceModelMapping" ofType:@"plist"];
    NSDictionary *iOSDevices = [NSDictionary dictionaryWithContentsOfFile:iOSDeviceModelsPath];

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

    return [iOSDevices valueForKey:deviceModel];
}

在xCode解决方案中添加以 iOSDeviceModelMapping.plist命名的plist文件,内容如下





	x86_64
	Simulator
	i386
	Simulator
	iPod1,1
	iPod Touch 1st Gen
	iPod2,1
	iPod Touch 2nd Gen
	iPod3,1
	iPod Touch 3rd Gen
	iPod4,1
	iPod Touch 4th Gen
	iPod5,1
	iPod Touch 5th Gen
	iPhone1,1
	iPhone
	iPhone1,2
	iPhone 3G
	iPhone2,1
	iPhone 3GS
	iPhone3,1
	iPhone 4
	iPhone3,2
	iPhone 4
	iPhone3,3
	iPhone 4
	iPhone4,1
	iPhone 4S
	iPhone5,1
	iPhone 5 model A1428
	iPhone5,2
	iPhone 5 model A1429
	iPhone5,3
	iPhone 5C
	iPhone5,4
	iPhone 5C
	iPhone6,1
	iPhone 5S
	iPhone6,2
	iPhone 5S
	iPad1,1
	iPad
	iPad2,1
	iPad 2
	iPad2,2
	iPad 2
	iPad2,3
	iPad 2
	iPad2,4
	iPad 2
	iPad3,1
	iPad 3rd Gen
	iPad3,2
	iPad 3rd Gen
	iPad3,3
	iPad 3rd Gen
	iPad3,4
	iPad 4th Gen
	iPad3,5
	iPad 4th Gen
	iPad3,6
	iPad 4th Gen
	iPad4,1
	iPad Air
	iPad4,2
	iPad Air
	iPad2,5
	iPad Mini 1st Gen
	iPad2,6
	iPad Mini 1st Gen
	iPad2,7
	iPad Mini 1st Gen
	iPad4,4
	iPad Mini 2nd Gen
	iPad4,5
	iPad Mini 2nd Gen


当MachineName方法时,NSDictionary通过匹配key,即utsname.machine的值,判断设备类型。


网上找到的类似的解决方法

#import 

- (NSString*) deviceName
{
    struct utsname systemInfo;

    uname(&systemInfo);

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

    static NSDictionary* deviceNamesByCode = nil;

    if (!deviceNamesByCode) {

        deviceNamesByCode = @{@"i386"      :@"Simulator",
                              @"iPod1,1"   :@"iPod Touch",      // (Original)
                              @"iPod2,1"   :@"iPod Touch",      // (Second Generation)
                              @"iPod3,1"   :@"iPod Touch",      // (Third Generation)
                              @"iPod4,1"   :@"iPod Touch",      // (Fourth Generation)
                              @"iPhone1,1" :@"iPhone",          // (Original)
                              @"iPhone1,2" :@"iPhone",          // (3G)
                              @"iPhone2,1" :@"iPhone",          // (3GS)
                              @"iPad1,1"   :@"iPad",            // (Original)
                              @"iPad2,1"   :@"iPad 2",          //
                              @"iPad3,1"   :@"iPad",            // (3rd Generation)
                              @"iPhone3,1" :@"iPhone 4",        //
                              @"iPhone4,1" :@"iPhone 4S",       //
                              @"iPhone5,1" :@"iPhone 5",        // (model A1428, AT&T/Canada)
                              @"iPhone5,2" :@"iPhone 5",        // (model A1429, everything else)
                              @"iPad3,4"   :@"iPad",            // (4th Generation)
                              @"iPad2,5"   :@"iPad Mini",       // (Original)
                              @"iPhone5,3" :@"iPhone 5c",       // (model A1456, A1532 | GSM)
                              @"iPhone5,4" :@"iPhone 5c",       // (model A1507, A1516, A1526 (China), A1529 | Global)
                              @"iPhone6,1" :@"iPhone 5s",       // (model A1433, A1533 | GSM)
                              @"iPhone6,2" :@"iPhone 5s",       // (model A1457, A1518, A1528 (China), A1530 | Global)
                              @"iPad4,1"   :@"iPad Air",        // 5th Generation iPad (iPad Air) - Wifi
                              @"iPad4,2"   :@"iPad Air",        // 5th Generation iPad (iPad Air) - Cellular
                              @"iPad4,4"   :@"iPad Mini",       // (2nd Generation iPad Mini - Wifi)
                              @"iPad4,5"   :@"iPad Mini"        // (2nd Generation iPad Mini - Cellular)
                              };
    }

    NSString* deviceName = [deviceNamesByCode objectForKey:code];

    if (!deviceName) {
        // Not found on database. At least guess main device type from string contents:

        if ([deviceName rangeOfString:@"iPod"].location != NSNotFound) {
            deviceName = @"iPod Touch";
        }
        else if([deviceName rangeOfString:@"iPad"].location != NSNotFound) {
            deviceName = @"iPad";
        }
        else if([deviceName rangeOfString:@"iPhone"].location != NSNotFound){
            deviceName = @"iPhone";
        }
    }

    return deviceName;
}

// (rest of class implementation omitted)

@end

后面加个获取系统版本号的,好记心不如烂笔头

    NSLog(@"System Name: %@", [[UIDevice currentDevice] systemName]);  //"iPhone OS"
    NSLog(@"System Version: %@", [[UIDevice currentDevice] systemVersion]);  //7.0.3


你可能感兴趣的:(IOS)