进入正题,我之后又试了下自己写的方法,发现用模拟器可以,但是真机不可以。
真机卸载后和之前获取的uuid不同,究其原因就是之前生成的uuid并没有被keychain成功存储。
所以问题在keychain.我之前给大家的KeychainItemWrapper类是苹果官方的,但是有些朋友反映运行时会崩溃。
除此之外,Code SigningEntitlements的创建方法也不够严谨。
1.新建一个工程,看一下自己的BundleId.这个BundleId 要和你用真机测试时的证书上面的Bundle Id相匹配。
2.Target - Capabilities - KeychainSharing - ON
这步主要目的是打开KeychainSharing,将它由灰色状态的OFF改为蓝色状态的ON。
打开之后的变化如下:
左侧的目录会自动生成Entitlements文件,不需要自己创建了。
也就是说,BundleIdentifier、Keychain Sharing的Keychain Groups、Entitlements文件的KeychainAccess Groups的第一个元素,它们要保持上图所示的一致性。
设置好了以后可以运行下程序,没问题可以进行下一步。
3.传说中的uuid类和keychain类来啦
既然苹果的keychain方法会崩溃而且有些复杂,我们只保存一个uuid的话可以用下面的简单方法:
(这也是我自己百度的keychain拷贝别人的,然后改改)
UUID.h
#import 尖括号(Foundation/Foundation.h)
@interface UUID: NSObject
+(NSString*)getUUID;
@end
UUID.m
#import "UUID.h"
#import"KeyChainStore.h"
@implementation UUID
+(NSString*)getUUID
{
NSString*strUUID = (NSString*)[KeyChainStoreload:@"com.company.app.usernamepassword"];
//首次执行该方法时,uuid为空
if([strUUIDisEqualToString:@""]|| !strUUID)
{
//生成一个uuid的方法
CFUUIDRef uuidRef= CFUUIDCreate(kCFAllocatorDefault);
strUUID = (NSString*)CFBridgingRelease(CFUUIDCreateString(kCFAllocatorDefault,uuidRef));
//将该uuid保存到keychain
[KeyChainStoresave:KEY_USERNAME_PASSWORDdata:strUUID];
}
returnstrUUID;
}
@end
KeyChainStore.h
#import尖括号(Foundation/Foundation.h)
@interfaceKeyChainStore :NSObject
+ (void)save:(NSString*)service data:(id)data;
+ (id)load:(NSString*)service;
+ (void)deleteKeyData:(NSString*)service;
@end
KeyChainStore.m
#import"KeyChainStore.h"
@implementation KeyChainStore
+ (NSMutableDictionary*)getKeychainQuery:(NSString*)service {
return[NSMutableDictionarydictionaryWithObjectsAndKeys:
(id)kSecClassGenericPassword,(id)kSecClass,
service,(id)kSecAttrService,
service,(id)kSecAttrAccount,
(id)kSecAttrAccessibleAfterFirstUnlock,(id)kSecAttrAccessible,
nil];
}
+ (void)save:(NSString*)service data:(id)data{
//Get search dictionary
NSMutableDictionary*keychainQuery = [selfgetKeychainQuery:service];
//Delete old item before add new item
SecItemDelete((CFDictionaryRef)keychainQuery);
//Add new object to searchdictionary(Attention:the data format)
[keychainQuery setObject:[NSKeyedArchiverarchivedDataWithRootObject:data]forKey:(id)kSecValueData];
//Add item to keychain with the searchdictionary
SecItemAdd((CFDictionaryRef)keychainQuery,NULL);
}
+ (id)load:(NSString*)service {
idret =nil;
NSMutableDictionary*keychainQuery = [selfgetKeychainQuery:service];
//Configure the search setting
//Since in our simple case we areexpecting only a single attribute to be returned (the password) wecan set the attribute kSecReturnData to kCFBooleanTrue
[keychainQuerysetObject:(id)kCFBooleanTrueforKey:(id)kSecReturnData];
[keychainQuery setObject:(id)kSecMatchLimitOneforKey:(id)kSecMatchLimit];
CFDataRefkeyData =NULL;
if(SecItemCopyMatching((CFDictionaryRef)keychainQuery,(CFTypeRef*)&keyData) ==noErr){
@try{
ret =[NSKeyedUnarchiverunarchiveObjectWithData:(__bridgeNSData*)keyData];
}@catch(NSException *e) {
NSLog(@"Unarchiveof %@ failed: %@",service, e);
}@finally{
}
}
if(keyData)
CFRelease(keyData);
returnret;
}
+ (void)deleteKeyData:(NSString*)service {
NSMutableDictionary*keychainQuery = [selfgetKeychainQuery:service];
SecItemDelete((CFDictionaryRef)keychainQuery);
}
@end
#ifndef PrefixHeader_pch
#define PrefixHeader_pch
#define KEY_USERNAME_PASSWORD@"com.company.app.usernamepassword"
#define KEY_USERNAME @"com.company.app.username"
#define KEY_PASSWORD @"com.company.app.password"
#endif
$(SRCROOT)/$(PROJECT_NAME)/PrefixHeader.pch
,其中$(PROJECT_NAME)
是相对工程名,比上面的方法更便捷.
5.在viewcontroller.m里面执行如下代码
NSString * uuid= [UUIDgetUUID];
NSLog(@"uuid=%@",uuid);