前言
昨天有个小伙伴问我怎么去做设备的唯一标示码,就像之前的糯米一样,不论卸载了还是怎么的,都会记住你这个手机。给他说用uuid去做,结果人家还是没做成。给他的一个Demo,在这也放上,给有同样疑惑的伙伴。
Demo
正文
其实我做的这一种原理很简单,uuid大家知道是设备对应用的唯一标示,但是有一个问题是当应用卸载了之后,再次下载对应应用的uuid就会改变。这就绝壁不行呀。所以怎么办呢?我们可以把这个uuid存储在本地,然后在应用再次安装打开的时候去判断是否有这个uuid数据,不就知道之前有没有安装过这个应用了。思路知道了,就改撸码了。选用的存储方式是keyChain,如果你不知道为什么选用这个方式去存储的话,度娘一下(这个东西很好用,你需要知道)。
核心代码:
//根据service去获取他对应的在文件里面的数据字典
+ (NSMutableDictionary *)keyChainQueryDictionaryWithService:(NSString *)service{
NSMutableDictionary *keyChainQueryDictaionary = [[NSMutableDictionary alloc]init];
[keyChainQueryDictaionary setObject:(id)kSecClassGenericPassword forKey:(id)kSecClass];
[keyChainQueryDictaionary setObject:service forKey:(id)kSecAttrService];
[keyChainQueryDictaionary setObject:service forKey:(id)kSecAttrAccount];
return keyChainQueryDictaionary;
}
//往本地数据字典里面去添加自己的数据
+ (BOOL)addData:(id)data forService:(NSString *)service{
NSMutableDictionary *keychainQuery = [self keyChainQueryDictionaryWithService:service];
SecItemDelete((CFDictionaryRef)keychainQuery);
[keychainQuery setObject:[NSKeyedArchiver archivedDataWithRootObject:data] forKey:(id)kSecValueData];
OSStatus status= SecItemAdd((CFDictionaryRef)keychainQuery, NULL);
if (status == noErr) {
return YES;
}
return NO;
}
//获取对应的数据
+ (id)queryDataWithService:(NSString *)service {
id result;
NSMutableDictionary *keyChainQuery = [self keyChainQueryDictionaryWithService: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 {
result = [NSKeyedUnarchiver unarchiveObjectWithData:(__bridge NSData *)keyData];
}
@catch (NSException *exception) {
NSLog(@"不存在数据");
}
@finally {
}
}
if (keyData) {
CFRelease(keyData);
}
return result;
}
//更新对应的service 的数据
+ (BOOL)updateData:(id)data forService:(NSString *)service{
NSMutableDictionary *searchDictionary = [self keyChainQueryDictionaryWithService:service];
if (!searchDictionary) {
return NO;
}
NSMutableDictionary *updateDictionary = [[NSMutableDictionary alloc] init];
[updateDictionary setObject:[NSKeyedArchiver archivedDataWithRootObject:data] forKey:(id)kSecValueData];
OSStatus status = SecItemUpdate((CFDictionaryRef)searchDictionary,
(CFDictionaryRef)updateDictionary);
if (status == errSecSuccess) {
return YES;
}
return NO;
}
//删除不用的本地数据
+ (BOOL)deleteDataWithService:(NSString *)service{
NSMutableDictionary *keyChainDictionary = [self keyChainQueryDictionaryWithService:service];
OSStatus status = SecItemDelete((CFDictionaryRef)keyChainDictionary);
if (status == noErr) {
return YES;
}
return NO;
}
然后来一个管理类去调用他:
//增加一个
+ (void)addDataToKeyChain:(NSString *)chainKey dataString:(id)dataString{
[SYFKeyChain addData:dataString forService:chainKey];
}
//搜索一个
+ (NSString *)queryDataToKeyChain:(NSString *)chainKey{
return (NSString *)[SYFKeyChain queryDataWithService:chainKey];
}
//更新一个
+ (void)updateDatatKeyChain:(NSString *)chainKey dataString:(id)dataString{
[SYFKeyChain updateData:dataString forService:chainKey];
}
//删除一个
+ (void)deleteDataKeyChain:(NSString *)chainKey{
[SYFKeyChain deleteDataWithService:chainKey];
}
最后就是使用他了:
//首先去获取本地数据uuid,看是否存在
NSString *uuid = [SYFManagerKeyChain queryDataToKeyChain:UUID_KEY];
if ([uuid isKindOfClass:[NSNull class]] || uuid == nil || [uuid isEqualToString:@""]) {
//说明本地没有存储uuid
self.tinLabel.text = @"这是第一次安装本应用,😄欢迎使用";
//获取到系统的uuid
uuid = [[NSUUID UUID] UUIDString];
//把uuid保存到本地数据
[SYFManagerKeyChain addDataToKeyChain:UUID_KEY dataString:uuid];
}else{
//说明本地存储着uuid
self.tinLabel.text = @"👏欢迎您回来继续使用,😄😄😄";
}
//显示uuid
self.uuidLabel.text = uuid;
显示效果:
结语
很简单的一个东西,个人感觉没有必要太多介绍。可以去下载Demo看一下,一运行,基本畅通。