您的位置:首页 > 产品设计 > UI/UE

iOS开发:keychain存储UUID

2016-07-05 16:28 344 查看
通常情况下,我们用NSUserDefaults存储数据信息,但是对于一些私密信息,比如密码、证书等等,就需要使用更为安全的keychain了。keychain里保存的信息不会因App被删除而丢失,在用户重新安装App后依然有效,数据还在。

首先需要在TARGETS-Capabilities-Keychain sharing 中将开关打开,打开后会自动生成*.entitleements文件。

1、创建UUID类

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 *)[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;
@end
KeyChainStore.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);
}
@end
3、创建一个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
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: