1、问题引入
在我开发的APP中,需要做设备绑定功能,单用户更换新的设备或者异常登录时,需要后台的配合,做验证码处理。
此处Android的同事的处理方式是获取手机的IMEI
码,再用户每次打开APP或者登录成功后做一次验证。Apple考虑用户的隐私问题,iOS 5之后就已经不能获取手机的IMEI
码了。虽然有私有API
可以获取到IMEI
,但是问题来了,即使通过私有API
获取到了也是不能上App Store
。
针对这种情况,我使用了Apple为开发者提供KeyChain
。由于其安全性和可靠性,即使在APP被卸载后,依然不会被删除数据。基于这些特点完全满足了我的功能需求,所以我会手动生成一个唯一标识码并保存到KeyChain
中。在用户登录成功之后或者每次打开APP时和服务器做一次验证。
2、如何生成唯一的设备码
其实设备码就是一段字符串,那么该如何保证我所生成的是唯一的呢?此处跟后台的同事沟通后,后台的同事表示,每个用户的ID
在后台生成的时候就是唯一的,我的方案是:
// 时间戳 + 设备型号 + 用户ID + 一个随机数
// MARK: - 获取当前时间戳
- (NSString *)currentTimeStr
{
//获取当前时间0秒后的时间
NSDate* date = [NSDate dateWithTimeIntervalSinceNow:0];
// 精确到秒
NSTimeInterval time = [date timeIntervalSince1970];
NSString *timeString = [NSString stringWithFormat:@"%.0f", time];
return timeString;
}
//MARK: - 生成随机数
-(int)GetRandomNumber:(int)from tonumber:(int)tonumber
{
return (from + (arc4random() % (tonumber - from + 1)));
}
//MARK: - 获取设备型号
- (NSString*)deviceModelName
{
struct utsname systemInfo;
uname(&systemInfo);
NSString *deviceModel = [NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding];
//iPhone 系列
if ([deviceModel isEqualToString:@"iPhone1,1"]) return @"iPhone 1G";
if ([deviceModel isEqualToString:@"iPhone1,2"]) return @"iPhone 3G";
if ([deviceModel isEqualToString:@"iPhone2,1"]) return @"iPhone 3GS";
if ([deviceModel isEqualToString:@"iPhone3,1"]) return @"iPhone 4";
if ([deviceModel isEqualToString:@"iPhone3,2"]) return @"Verizon iPhone 4";
if ([deviceModel isEqualToString:@"iPhone4,1"]) return @"iPhone 4S";
if ([deviceModel isEqualToString:@"iPhone5,1"]) return @"iPhone 5";
if ([deviceModel isEqualToString:@"iPhone5,2"]) return @"iPhone 5";
if ([deviceModel isEqualToString:@"iPhone5,3"]) return @"iPhone 5C";
if ([deviceModel isEqualToString:@"iPhone5,4"]) return @"iPhone 5C";
if ([deviceModel isEqualToString:@"iPhone6,1"]) return @"iPhone 5S";
if ([deviceModel isEqualToString:@"iPhone6,2"]) return @"iPhone 5S";
if ([deviceModel isEqualToString:@"iPhone7,1"]) return @"iPhone 6 Plus";
if ([deviceModel isEqualToString:@"iPhone7,2"]) return @"iPhone 6";
if ([deviceModel isEqualToString:@"iPhone8,1"]) return @"iPhone 6s";
if ([deviceModel isEqualToString:@"iPhone8,2"]) return @"iPhone 6s Plus";
if ([deviceModel isEqualToString:@"iPhone9,1"]) return @"iPhone 7 (CDMA)";
if ([deviceModel isEqualToString:@"iPhone9,3"]) return @"iPhone 7 (GSM)";
if ([deviceModel isEqualToString:@"iPhone9,2"]) return @"iPhone 7 Plus (CDMA)";
if ([deviceModel isEqualToString:@"iPhone9,4"]) return @"iPhone 7 Plus (GSM)";
if ([deviceModel isEqualToString:@"iPhone10,1"]) return @"国行(A1863)、日行(A1906)iPhone 8";
if ([deviceModel isEqualToString:@"iPhone10,4"]) return @"美版(Global/A1905)iPhone 8";
if ([deviceModel isEqualToString:@"iPhone10,2"]) return @"国行(A1864)、日行(A1898)iPhone 8 Plus";
if ([deviceModel isEqualToString:@"iPhone10,5"]) return @"美版(Global/A1897)iPhone 8 Plus";
if ([deviceModel isEqualToString:@"iPhone10,3"]) return @"iPhone X";
if ([deviceModel isEqualToString:@"iPhone10,6"]) return @"iPhone X";
//iPod 系列
if ([deviceModel isEqualToString:@"iPod1,1"]) return @"iPod Touch 1G";
if ([deviceModel isEqualToString:@"iPod2,1"]) return @"iPod Touch 2G";
if ([deviceModel isEqualToString:@"iPod3,1"]) return @"iPod Touch 3G";
if ([deviceModel isEqualToString:@"iPod4,1"]) return @"iPod Touch 4G";
if ([deviceModel isEqualToString:@"iPod5,1"]) return @"iPod Touch 5G";
//iPad 系列
if ([deviceModel isEqualToString:@"iPad1,1"]) return @"iPad";
if ([deviceModel isEqualToString:@"iPad2,1"]) return @"iPad 2 (WiFi)";
if ([deviceModel isEqualToString:@"iPad2,2"]) return @"iPad 2 (GSM)";
if ([deviceModel isEqualToString:@"iPad2,3"]) return @"iPad 2 (CDMA)";
if ([deviceModel isEqualToString:@"iPad2,4"]) return @"iPad 2 (32nm)";
if ([deviceModel isEqualToString:@"iPad2,5"]) return @"iPad mini (WiFi)";
if ([deviceModel isEqualToString:@"iPad2,6"]) return @"iPad mini (GSM)";
if ([deviceModel isEqualToString:@"iPad2,7"]) return @"iPad mini (CDMA)";
if ([deviceModel isEqualToString:@"iPad3,1"]) return @"iPad 3(WiFi)";
if ([deviceModel isEqualToString:@"iPad3,2"]) return @"iPad 3(CDMA)";
if ([deviceModel isEqualToString:@"iPad3,3"]) return @"iPad 3(4G)";
if ([deviceModel isEqualToString:@"iPad3,4"]) return @"iPad 4 (WiFi)";
if ([deviceModel isEqualToString:@"iPad3,5"]) return @"iPad 4 (4G)";
if ([deviceModel isEqualToString:@"iPad3,6"]) return @"iPad 4 (CDMA)";
if ([deviceModel isEqualToString:@"iPad4,1"]) return @"iPad Air";
if ([deviceModel isEqualToString:@"iPad4,2"]) return @"iPad Air";
if ([deviceModel isEqualToString:@"iPad4,3"]) return @"iPad Air";
if ([deviceModel isEqualToString:@"iPad5,3"]) return @"iPad Air 2";
if ([deviceModel isEqualToString:@"iPad5,4"]) return @"iPad Air 2";
if ([deviceModel isEqualToString:@"i386"]) return @"Simulator";
if ([deviceModel isEqualToString:@"x86_64"]) return @"Simulator";
if ([deviceModel isEqualToString:@"iPad4,4"]
||[deviceModel isEqualToString:@"iPad4,5"]
||[deviceModel isEqualToString:@"iPad4,6"]) return @"iPad mini 2";
if ([deviceModel isEqualToString:@"iPad4,7"]
||[deviceModel isEqualToString:@"iPad4,8"]
||[deviceModel isEqualToString:@"iPad4,9"]) return @"iPad mini 3";
return deviceModel;
}
3、使用KeyChain保存生成的设备码
+ (NSMutableDictionary *)getKeychainQuery:(NSString *)service {
return [NSMutableDictionary dictionaryWithObjectsAndKeys:
(id)kSecClassGenericPassword,(id)kSecClass,
service, (id)kSecAttrService,
service, (id)kSecAttrAccount,
(id)kSecAttrAccessibleAfterFirstUnlock,(id)kSecAttrAccessible,
nil];
}
+ (void)save:(NSString *)service data:(id)data {
NSMutableDictionary *keychainQuery = [self getKeychainQuery:service];
SecItemDelete((CFDictionaryRef)keychainQuery);
[keychainQuery setObject:[NSKeyedArchiver archivedDataWithRootObject:data] forKey:(id)kSecValueData];
SecItemAdd((CFDictionaryRef)keychainQuery, NULL);
}
保存成功之后,再下次要用到时直接从KeyChain
中查找;
+ (id)load:(NSString *)service {
id ret = nil;
NSMutableDictionary *keychainQuery = [self getKeychainQuery:service];
[keychainQuery setObject:(id)kCFBooleanTrue forKey:(id)kSecReturnData];
[keychainQuery setObject:(id)kSecMatchLimitOne forKey:(id)kSecMatchLimit];
CFDataRef keyData = NULL;
if (SecItemCopyMatching((CFDictionaryRef)keychainQuery, (CFTypeRef *)&keyData) == noErr) {
@try {
ret = [NSKeyedUnarchiver unarchiveObjectWithData:(__bridge NSData *)keyData];
} @catch (NSException *e) {
NSLog(@"Unarchive of %@ failed: %@", service, e);
} @finally {
}
}
if (keyData)
CFRelease(keyData);
return ret;
}
当然,这只是我个人在开发中的一个思路,如果有不同解决方案,欢迎提出。
本文Demo请戳这里.
网友评论