iOS开发-用keychain替代UDID
从2013-5-1日开始苹果就禁止对UUID的应用的通过了。所以我们需要用一些办法替换,下面我就是用keychain的访问替换掉UUID的。
那么,关于Keychain的应用,Apple提供了一个叫GenericKeychain的例子程序,在这里:http://developer.apple.com/library/ios/#samplecode/GenericKeychain/Listings/Classes_KeychainItemWrapper_h.html#//apple_ref/doc/uid/DTS40007797-Classes_KeychainItemWrapper_h-DontLinkElementID_9,其中封装了一个简化Keychain操作的类:KeychainItemWrapper,可以拿来直接使用。
1、首先应该导入库文件:Security.framework 且需要把文件KeychainItemWrapper.h和KeychainItemWrapper.m导入到工程,并引用头文件:#import"KeychainItemWrapper.h"
2、代码例子:
[plain]
NSString *str =nil;
KeychainItemWrapper *keychainItem = [[KeychainItemWrapper alloc] initWithIdentifier:@"UUID" accessGroup:@"com.xxx.xxx"];
NSString *uuidStr = [keychainItem objectForKey:(id)kSecValueData];
if (uuidStr.length == 0) {
NSString *myUUIDStr = [[[UIDevice currentDevice] identifierForVendor] UUIDString];
[keychainItem setObject:myUUIDStr forKey:(id)kSecValueData];
str = myUUIDStr;
}
else{
str = [keychainItem objectForKey:(id)kSecValueData];
}
NSLog(@"======%@",str);
return str;
NSString *str =nil;
KeychainItemWrapper *keychainItem = [[KeychainItemWrapper alloc] initWithIdentifier:@"UUID" accessGroup:@"com.xxx.xxx"];
NSString *uuidStr = [keychainItem objectForKey:(id)kSecValueData];
if (uuidStr.length == 0) {
NSString *myUUIDStr = [[[UIDevice currentDevice] identifierForVendor] UUIDString];
[keychainItem setObject:myUUIDStr forKey:(id)kSecValueData];
str = myUUIDStr;
}
else{
str = [keychainItem objectForKey:(id)kSecValueData];
}
NSLog(@"======%@",str);
return str;
导入Security.framework是因为KeychainItemWrapper文件只是对Security一个封装,更加利于我们的调用。
补充:移动开发 , IOS ,