從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一個封裝,更加利於我們的調用。