标签:
iOS获取设备型号、设备类型等信息
关于设备标识,历史上盛行过很多英雄,比如UDID、Mac地址、OpenUDID等,然而他们都陆陆续续倒在了苹果的门下。苹果目前提供了2个方法供App获取设备标识:idfa和idfv
idfa:全称advertisingIdentifier
,官方解释是广告标识,适用于广告推广,这个建议不要轻易使用,如果用了,则App里必须提供广告功能,否则很有可能会在AppStore审核时被拒。而且idfa是可以被用户关闭的(设置-》隐私),一旦被关闭,就获取不到了。
idfv:全称identifierForVendor,按照字面意思理解,相同的Vendor应该有相同的identifier。Vendor是根据app的bundleid的前两位来界定的,比如对于com.abc.app1和com.abc.app2这2个bundleid的app来说,他们就属于同一个vendor。idfv是一定可以获取到的。需要注意的一点是,当用户将某个vendor下的所有app都卸载掉时,idfv就会被重置,再次安装时,获取到的idfv会跟之前的不一样。所以我用的方案是:idfv+keychain。当然如果用户选择恢复出厂设置,那么还是会出现不一样,这种情况就没法处理了。
1 +(NSString *)identifier 2 { 3 NSString *key = @"com.app.keychain.uuid"; 4 KeychainItemWrapper *keychainItem = [[KeychainItemWrapper alloc] initWithIdentifier:key accessGroup:nil]; 5 6 NSString *strUUID = [keychainItem objectForKey:(__bridge id)kSecValueData]; 7 8 if (strUUID.length <= 0) { 9 strUUID = [[[UIDevice currentDevice] identifierForVendor] UUIDString]; 10 11 [keychainItem setObject:@"uuid" forKey:(__bridge id)kSecAttrAccount]; 12 [keychainItem setObject:strUUID forKey:(__bridge id)kSecValueData]; 13 } 14 15 return strUUID; 16 }
手机别名
1 +(NSString *)phoneName 2 { 3 return [[UIDevice currentDevice] name]; 4 }
1 /** 2 * 手机系统版本 3 * 4 * @return e.g. 8.0 5 */ 6 +(NSString *)phoneVersion{ 7 return [[UIDevice currentDevice] systemVersion]; 8 }
这个方法只能获取到iPhone、iPad这种信息,无法获取到是iPhone 4、iPhpone5这种具体的型号。
1 /** 2 * 手机型号 3 * 4 * @return e.g. iPhone 5 */ 6 +(NSString *)phoneModel{ 7 return [[UIDevice currentDevice] model]; 8 }
这个代码可以获取到具体的设备版本(已更新到iPhone 6s、iPhone 6s Plus),缺点是:采用的硬编码。具体的对应关系可以参考:https://www.theiphonewiki.com/wiki/Models
这个方法可以通过AppStore的审核,放心用吧。
1 /** 2 * 设备版本 3 * 4 * @return e.g. iPhone 5S 5 */ 6 + (NSString*)deviceVersion 7 { 8 // 需要#import "sys/utsname.h" 9 struct utsname systemInfo; 10 uname(&systemInfo); 11 NSString *deviceString = [NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding]; 12 13 //iPhone 14 if ([deviceString isEqualToString:@"iPhone1,1"]) return @"iPhone 1G"; 15 if ([deviceString isEqualToString:@"iPhone1,2"]) return @"iPhone 3G"; 16 if ([deviceString isEqualToString:@"iPhone2,1"]) return @"iPhone 3GS"; 17 if ([deviceString isEqualToString:@"iPhone3,1"]) return @"iPhone 4"; 18 if ([deviceString isEqualToString:@"iPhone3,2"]) return @"Verizon iPhone 4"; 19 if ([deviceString isEqualToString:@"iPhone4,1"]) return @"iPhone 4S"; 20 if ([deviceString isEqualToString:@"iPhone5,1"]) return @"iPhone 5"; 21 if ([deviceString isEqualToString:@"iPhone5,2"]) return @"iPhone 5"; 22 if ([deviceString isEqualToString:@"iPhone5,3"]) return @"iPhone 5C"; 23 if ([deviceString isEqualToString:@"iPhone5,4"]) return @"iPhone 5C"; 24 if ([deviceString isEqualToString:@"iPhone6,1"]) return @"iPhone 5S"; 25 if ([deviceString isEqualToString:@"iPhone6,2"]) return @"iPhone 5S"; 26 if ([deviceString isEqualToString:@"iPhone7,1"]) return @"iPhone 6 Plus"; 27 if ([deviceString isEqualToString:@"iPhone7,2"]) return @"iPhone 6"; 28 if ([deviceString isEqualToString:@"iPhone8,1"]) return @"iPhone 6s"; 29 if ([deviceString isEqualToString:@"iPhone8,2"]) return @"iPhone 6s Plus"; 30 31 //iPod 32 if ([deviceString isEqualToString:@"iPod1,1"]) return @"iPod Touch 1G"; 33 if ([deviceString isEqualToString:@"iPod2,1"]) return @"iPod Touch 2G"; 34 if ([deviceString isEqualToString:@"iPod3,1"]) return @"iPod Touch 3G"; 35 if ([deviceString isEqualToString:@"iPod4,1"]) return @"iPod Touch 4G"; 36 if ([deviceString isEqualToString:@"iPod5,1"]) return @"iPod Touch 5G"; 37 38 //iPad 39 if ([deviceString isEqualToString:@"iPad1,1"]) return @"iPad"; 40 if ([deviceString isEqualToString:@"iPad2,1"]) return @"iPad 2 (WiFi)"; 41 if ([deviceString isEqualToString:@"iPad2,2"]) return @"iPad 2 (GSM)"; 42 if ([deviceString isEqualToString:@"iPad2,3"]) return @"iPad 2 (CDMA)"; 43 if ([deviceString isEqualToString:@"iPad2,4"]) return @"iPad 2 (32nm)"; 44 if ([deviceString isEqualToString:@"iPad2,5"]) return @"iPad mini (WiFi)"; 45 if ([deviceString isEqualToString:@"iPad2,6"]) return @"iPad mini (GSM)"; 46 if ([deviceString isEqualToString:@"iPad2,7"]) return @"iPad mini (CDMA)"; 47 48 if ([deviceString isEqualToString:@"iPad3,1"]) return @"iPad 3(WiFi)"; 49 if ([deviceString isEqualToString:@"iPad3,2"]) return @"iPad 3(CDMA)"; 50 if ([deviceString isEqualToString:@"iPad3,3"]) return @"iPad 3(4G)"; 51 if ([deviceString isEqualToString:@"iPad3,4"]) return @"iPad 4 (WiFi)"; 52 if ([deviceString isEqualToString:@"iPad3,5"]) return @"iPad 4 (4G)"; 53 if ([deviceString isEqualToString:@"iPad3,6"]) return @"iPad 4 (CDMA)"; 54 55 if ([deviceString isEqualToString:@"iPad4,1"]) return @"iPad Air"; 56 if ([deviceString isEqualToString:@"iPad4,2"]) return @"iPad Air"; 57 if ([deviceString isEqualToString:@"iPad4,3"]) return @"iPad Air"; 58 if ([deviceString isEqualToString:@"iPad5,3"]) return @"iPad Air 2"; 59 if ([deviceString isEqualToString:@"iPad5,4"]) return @"iPad Air 2"; 60 if ([deviceString isEqualToString:@"i386"]) return @"Simulator"; 61 if ([deviceString isEqualToString:@"x86_64"]) return @"Simulator"; 62 63 if ([deviceString isEqualToString:@"iPad4,4"] 64 ||[deviceString isEqualToString:@"iPad4,5"] 65 ||[deviceString isEqualToString:@"iPad4,6"]) return @"iPad mini 2"; 66 67 if ([deviceString isEqualToString:@"iPad4,7"] 68 ||[deviceString isEqualToString:@"iPad4,8"] 69 ||[deviceString isEqualToString:@"iPad4,9"]) return @"iPad mini 3"; 70 71 return deviceString; 72 }
标签:
原文地址:http://www.cnblogs.com/yipingios/p/5714395.html