最近项目中需要用到UUID做标识,于是自己就做了一个小例子。如果有什么问题,请大佬评论指出。
1.创建一个NSObject类 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 search dictionary(Attention:the data format)
[keychainQuery setObject:[NSKeyedArchiverarchivedDataWithRootObject:data] forKey:(id)kSecValueData];
//Add item to keychain with the search dictionary
SecItemAdd((CFDictionaryRef)keychainQuery,NULL);
}
+ (id)load:(NSString *)service {
id ret = nil;
NSMutableDictionary *keychainQuery = [selfgetKeychainQuery:service];
//Configure the search setting
//Since in our simple case we are expecting only a single attribute to be returned (the password) we can set the attribute kSecReturnData to kCFBooleanTrue
[keychainQuery setObject:(id)kCFBooleanTrueforKey:(id)kSecReturnData];
[keychainQuery setObject:(id)kSecMatchLimitOneforKey:(id)kSecMatchLimit];
CFDataRef keyData =NULL;
if (SecItemCopyMatching((CFDictionaryRef)keychainQuery, (CFTypeRef *)&keyData) ==noErr) {
@try {
ret = [NSKeyedUnarchiverunarchiveObjectWithData:(__bridgeNSData *)keyData];
} @catch (NSException *e) {
NSLog(@"Unarchive of %@ failed: %@", service, e);
} @finally {
}
}
if (keyData)
CFRelease(keyData);
return ret;
}
+ (void)deleteKeyData:(NSString *)service {
NSMutableDictionary *keychainQuery = [selfgetKeychainQuery:service];
SecItemDelete((CFDictionaryRef)keychainQuery);
}
2.在另一个类中 进行读取和存储
+(NSString *)getUUID
{
NSArray *paths =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *documentsDirectory = [pathsobjectAtIndex:0];
NSFileManager *fileManager = [NSFileManagerdefaultManager];
NSString *filePath = [documentsDirectorystringByAppendingPathComponent:@"text.plist"];
NSString * strUUID = (NSString *)[KeyChainStoreload:@"com.company.app.usernamepassword"];
if(![fileManagerfileExistsAtPath:filePath]) //如果不存在
{
//首次执行该方法时,uuid为空
if ([strUUID isEqualToString:@""] || !strUUID)
{
//生成一个uuid的方法
CFUUIDRef uuidRef =CFUUIDCreate(kCFAllocatorDefault);
strUUID = (NSString *)CFBridgingRelease(CFUUIDCreateString (kCFAllocatorDefault,uuidRef));
//将该uuid保存到keychain
[KeyChainStoresave:KEY_USERNAME_PASSWORDdata:strUUID];
//写入数据到plist文件
NSMutableDictionary *dataDic = [NSMutableDictionarydictionary];
[dataDic setObject:strUUIDforKey:KEY_USERNAME_PASSWORD];
//写入plist里面
if([dataDicwriteToFile:filePath atomically:YES]){
NSLog(@"su");
};
CFRelease(uuidRef);
}
}else{
//读取
NSMutableDictionary *dataDictionary = [[NSMutableDictionaryalloc] initWithContentsOfFile:filePath];
NSLog(@"---plist一开始保存时候的内容---%@",dataDictionary);
if(dataDictionary)
{
strUUID= [dataDictionary objectForKey:KEY_USERNAME_PASSWORD];
}
}
return strUUID;
}