UUID解决方案

文章目录
  1. 1. 简介
  2. 2. 创建keychain
  3. 3. 自定义存储类
  4. 4. 自定义一个类来获取UUID

在项目中用到了UUID,需要在用户第一次安装以后再次安装时UUID不会再变化,作为一个唯一的标识,就这个问题找了一些资料,记录下来以便下次使用。

简介

参考来源 http://www.tuicool.com/articles/Ir63UbI

UUID解决方案主要采用了keychain存储的解决方案,我们知道如果使用NSUserDefaults来存储的话当卸载以后数据也将会一并删除,目前最好的解决方案就是使用keychain存储。

创建keychain

创建keychain流程
创建完以后会自动生成一个entitlements文件
生成的entitlements文件

自定义存储类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
  #import "INKeyChainStore.h"
@implementation INKeyChainStore
+ (NSMutableDictionary *)getKeychainQuery:(NSString *)service {
return [NSMutableDictionary dictionaryWithObjectsAndKeys:
(id)kSecClassGenericPassword,(id)kSecClass,
service, (id)kSecAttrService,
service, (id)kSecAttrAccount,
(id)kSecAttrAccessibleAfterFirstUnlock,(id)kSecAttrAccessible,
nil];
}
// 将值保存在keychain中
+ (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);
}
// 获取存进入的值
+ (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) {

} @finally {

}
}
if (keyData)
CFRelease(keyData);
return ret;
}
// 删除值
+ (void)deleteKeyData:(NSString *)service {
NSMutableDictionary *keychainQuery = [self getKeychainQuery:service];
SecItemDelete((CFDictionaryRef)keychainQuery);
}

@end

自定义一个类来获取UUID

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
//  获取设备UUID
+ (NSString *)getUUID
{
NSString * strUUID = (NSString *)[INKeyChainStore load:@"uuid.yaowen"];

//首次执行该方法时,uuid为空
if ([strUUID isEqualToString:@""] || !strUUID)
{
// 生成一个uuid的方法
CFUUIDRef uuidRef = CFUUIDCreate(kCFAllocatorDefault);
strUUID = (NSString *)CFBridgingRelease(CFUUIDCreateString (kCFAllocatorDefault,uuidRef));
strUUID = [strUUID stringByReplacingOccurrencesOfString:@"-" withString:@""];
// 将该uuid保存到keychain
[INKeyChainStore save:@"uuid.yaowen" data:strUUID];
}
return strUUID;
}
分享到 评论