iOS开发:keychain存储UUID
2016-07-05 16:28
344 查看
通常情况下,我们用NSUserDefaults存储数据信息,但是对于一些私密信息,比如密码、证书等等,就需要使用更为安全的keychain了。keychain里保存的信息不会因App被删除而丢失,在用户重新安装App后依然有效,数据还在。
首先需要在TARGETS-Capabilities-Keychain sharing 中将开关打开,打开后会自动生成*.entitleements文件。
1、创建UUID类
UUID.h文件
2、创建KeyChainStore类
KeyChainStore.h文件
pch文件的创建方法可参考:http://blog.csdn.net/huang2009303513/article/details/40375235
你有可能会在填Prefix Header 即pch文件的路径那里报错,最近又学习到一种更好的方式
4、使用
首先需要在TARGETS-Capabilities-Keychain sharing 中将开关打开,打开后会自动生成*.entitleements文件。
1、创建UUID类
UUID.h文件
#import <Foundation/Foundation.h> @interface UUID : NSObject +(NSString *)getUUID; @endUUID.m文件
#import "UUID.h" #import "KeyChainStore.h" @implementation UUID +(NSString *)getUUID { NSString * strUUID = (NSString *)[KeyChainStore load:KEY_USERNAME_PASSWORD]; //首次执行该方法时,uuid为空 if ([strUUID isEqualToString:@""] || !strUUID) { //生成一个uuid的方法 CFUUIDRef uuidRef = CFUUIDCreate(kCFAllocatorDefault); strUUID = (NSString *)CFBridgingRelease(CFUUIDCreateString (kCFAllocatorDefault,uuidRef)); //将该uuid保存到keychain [KeyChainStore save:KEY_USERNAME_PASSWORD data:strUUID]; } return strUUID; } @end
2、创建KeyChainStore类
KeyChainStore.h文件
#import <Foundation/Foundation.h> @interface KeyChainStore : NSObject + (void)save:(NSString *)service data:(id)data; + (id)load:(NSString *)service; + (void)deleteKeyData:(NSString *)service; @endKeyChainStore.m文件
#import "KeyChainStore.h" @implementation KeyChainStore + (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 dictionary NSMutableDictionary *keychainQuery = [self getKeychainQuery:service]; //Delete old item before add new item SecItemDelete((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 dictionary SecItemAdd((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:(__bridge NSData *)keyData]; } @catch (NSException *e) { NSLog(@"Unarchive of %@ failed: %@", service, e); } @finally { } } if (keyData) CFRelease(keyData); return ret; } + (void)deleteKeyData:(NSString *)service { NSMutableDictionary *keychainQuery = [self getKeychainQuery:service]; SecItemDelete((CFDictionaryRef)keychainQuery); } @end3、创建一个pch文件(不是必须);
pch文件的创建方法可参考:http://blog.csdn.net/huang2009303513/article/details/40375235
你有可能会在填Prefix Header 即pch文件的路径那里报错,最近又学习到一种更好的方式
$(SRCROOT)/$(PROJECT_NAME)/PrefixHeader.pch,其中
$(PROJECT_NAME)是相对工程名,比上面的方法更便捷.
#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
4、使用
#import "ViewController.h" #import "UUID.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; NSString * uuid= [UUID getUUID]; NSLog(@"uuid=%@",uuid); //第一次生成的uuid //uuid=A9B9C149-03A9-472E-BF16-75386D491A08 //卸载之后生成的uuid(从钥匙串内取出的) //uuid=A9B9C149-03A9-472E-BF16-75386D491A08 //重置系统之后生成的uuid(重新生成的)-若用户重置了系统的话uuid会重新生成就失去了唯一性。 //uuid=E83ACF06-D9E3-4EC0-A76D-1DE13E793813 }
相关文章推荐
- java 基础:Blocking Queues的源码实现
- easyui学习(1)
- 阿里面经之解答 by cmershen(3)——String/StringBuffer/StringBuilder,Java序列化,线程安全,线程同步,ThreadLocal
- UI碎片控件之Fragment——底部导航栏的实现(方法2)
- Number Sequence(poj 1019)
- DelayQueue
- UUID和UDID
- 关于tensorflow 的数据读取线程管理QueueRunner
- PriorityQueue
- 数据库主键用 int自增序列 还是 GUID
- HTML标签marquee实现滚动效果
- Xcode莫名其妙问题could not build module 'Foundation'
- String StringBuffer StringBuilder区别(Java面试)
- UGUI解决嵌套使用多个ScrollRect时的Drag拖动冲突问题
- SeaJS与RequireJS
- NGUI 事件 (动画播放完毕调用其他方法)
- Increasing Triplet Subsequence
- UIView旋转动画
- UI开发设计
- BlueStacks 设置代理服务器 Proxifier指定任意程序的代理服务器