iOS UUID配合keychain的替换方案实现
生活随笔
收集整理的這篇文章主要介紹了
iOS UUID配合keychain的替换方案实现
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
iOS的keychain服務提供了一種安全的保存私密信息(密碼,序列號,證書等)的方式,每個iOS程序都有一個獨立的keychain存儲。相對于NSUserDefaults、文件保存等一般方式,keychain保存更為安全,而且keychain里保存的信息不會因App被刪除而丟失,所以在重裝App后,keychain里的數據還能使用。 在應用里使用使用keyChain,我們需要導入Security.framework
要先聲明公共區的名稱,官方文檔管這個名稱叫“keychain access group”,聲明的方法是新建一個plist文件,名字隨便起,內容如下:
“yourAppID.com.yourCompany.whatever”就是你要起的公共區名稱
?
獲取UUID 并保存到keychain中:
#pragma mark--獲取設備UUID -(NSString*) uuid {if ([CHKeychain load:UUIDKEY]) {NSString *result = [CHKeychain load:UUIDKEY];return result;}else{CFUUIDRef puuid = CFUUIDCreate( nil );CFStringRef uuidString = CFUUIDCreateString( nil, puuid );NSString * result = (NSString *)CFStringCreateCopy( NULL, uuidString);CFRelease(puuid);CFRelease(uuidString);[CHKeychain save:UUIDKEY data:result];return [result autorelease];}return nil; }
CHKeychain 的實現代碼(需要導入Security.framework):
CHKeychain.h
#import <Foundation/Foundation.h>@interface CHKeychain : NSObject+ (void)save:(NSString *)service data:(id)data; + (id)load:(NSString *)service; + (void)deleteData:(NSString *)service;@endCHKeychain.m
#import "CHKeychain.h"@implementation CHKeychain + (NSMutableDictionary *)getKeychainQuery:(NSString *)service {return [NSMutableDictionary dictionaryWithObjectsAndKeys:(id)kSecClassGenericPassword,(id)kSecClass,service, (id)kSecAttrService,service, (id)kSecAttrAccount,(id)kSecAttrAccessibleAfterFirstUnlock,(id)kSecAttrAccessible,nil]; }+ (void)save:(NSString *)service data:(id)data {//Get search dictionaryNSMutableDictionary *keychainQuery = [self getKeychainQuery:service];//Delete old item before add new itemSecItemDelete((CFDictionaryRef)keychainQuery);//Add new object to search dictionary(Attention:the data format)[keychainQuery setObject:[NSKeyedArchiver archivedDataWithRootObject:data] forKey:(id)kSecValueData];//Add item to keychain with the search dictionarySecItemAdd((CFDictionaryRef)keychainQuery, NULL); }+ (id)load:(NSString *)service {id ret = nil;NSMutableDictionary *keychainQuery = [self getKeychainQuery: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)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:(NSData *)keyData];} @catch (NSException *e) {NSLog(@"Unarchive of %@ failed: %@", service, e);} @finally {}}if (keyData)CFRelease(keyData);return ret; }+ (void)delete:(NSString *)service {NSMutableDictionary *keychainQuery = [self getKeychainQuery:service];SecItemDelete((CFDictionaryRef)keychainQuery); }@end
?參考:http://blog.csdn.net/u011439689/article/details/18707387
轉載于:https://www.cnblogs.com/samniu/p/3673781.html
總結
以上是生活随笔為你收集整理的iOS UUID配合keychain的替换方案实现的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: SpringMvc多视图整合(jsp、v
- 下一篇: 这又是起点