神刀安全网

每日一博 | iOS KeyChian (钥匙串)封装

1 、KeyChian 是一个非常好用的本地存储工具 完全可以替代NSuserDefault 来使用并且比NSuserDefault更安全  。

2 、KeyChian相对于 NSuserDefault最主要的一点就是 NSuserDefault 只能项目内访问  KeyChian 说是所有项目公用的  可以做到跨项目读取数据。

3、KeyChian经常用来保存用的的账户 密码信息  KeyChian在内部实现上实际还是用的数据库 来存储的这里不多做解释

4、用法 这里写死了数据 具体可以根据实际情况 和NSuserDefault 用法一样

[ZFYKeychain savePassWord:@"aaa1" forKey:@"aaa"];

NSLog(@"%@", [ZFYKeychain readPassWordObjectForKey:@"aaa"]);

[ZFYKeychain savePassWord:@"bbb1" forKey:@"bbb"];

NSLog(@"%@", [ZFYKeychain readPassWordObjectForKey:@"bbb"]);

[ZFYKeychain deletePassWordObjectForKey:@"aaa"];

NSLog(@"%@", [ZFYKeychain readPassWordObjectForKey:@"aaa"]);NSLog(@"%@", [ZFYKeychain readPassWordObjectForKey:@"bbb"]);

5、设置跨APP 共享数据

第一种方法:通过plist方式进行

1>创建一个新文件,选择Property list文件。

2>创建一个新的子节点,取名为keychain-access-groups

3>修改节点keychain-access-groups的类型为Array

4>在keychain-access-groups节点下添加子节点,填入你的主KEY名称

第二种方式:直接通过XCODE特性来配置(非常简单)

1>选择工程配置中的tages>capabilities页面

2>打开KeyChain Sharing开关,设置你的主KEY的名称。

LSF.iceTearsTest  是另外一个APP的entitlements file

LSF.Bluetooth  是当前项目的 entitlements file

相互关联之后就可以通过不同的APP来 改变KeyChain里边存储的数据

每日一博 | iOS KeyChian (钥匙串)封装 #import <Foundation/Foundation.h>

@interface ZFYKeychain : NSObject

/**

*    @brief    存储密码

*

*    @param     password     密码内容

*

*    @param     key     键

*/

+(void)savePassWord:(NSString *)password forKey:(NSString *)key;

/**

*    @brief    读取密码

*

*    @return    密码内容

*

*    @param     key     键

*/

+(id)readPassWordObjectForKey:(NSString *)key;

/**

*    @brief    删除密码数据

*

*    @param     key     键

*/

+(void)deletePassWordObjectForKey:(NSString *)key;

@end

#import "ZFYKeychain.h"

@implementation ZFYKeychain

static NSString * const KEY_IN_KEYCHAIN = @"com.ZFYKeychain";//key 用来作为存取的标示

+(void)savePassWord:(NSString *)password forKey:(NSString *)key

{

NSMutableDictionary *usernamepasswordKVPairs = [NSMutableDictionary dictionary];

[usernamepasswordKVPairs setObject:password forKey:key];

[self save:KEY_IN_KEYCHAIN data:usernamepasswordKVPairs];

}

+(id)readPassWordObjectForKey:(NSString *)key

{

NSMutableDictionary *usernamepasswordKVPair = (NSMutableDictionary *)[self load:KEY_IN_KEYCHAIN];

return [usernamepasswordKVPair objectForKey:key];

}

+(void)deletePassWordObjectForKey:(NSString *)key

{

[self delete:key];

}

+ (NSMutableDictionary *)getKeychainQuery:(NSString *)service {

return [NSMutableDictionary dictionaryWithObjectsAndKeys:

(__bridge_transfer id)kSecClassGenericPassword,(__bridge_transfer id)kSecClass,

service, (__bridge_transfer id)kSecAttrService,

service, (__bridge_transfer id)kSecAttrAccount,

(__bridge_transfer id)kSecAttrAccessibleAfterFirstUnlock,(__bridge_transfer 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((__bridge_retained CFDictionaryRef)keychainQuery);

//Add new object to search dictionary(Attention:the data format)

[keychainQuery setObject:[NSKeyedArchiver archivedDataWithRootObject:data] forKey:(__bridge_transfer id)kSecValueData];

//Add item to keychain with the search dictionary

SecItemAdd((__bridge_retained CFDictionaryRef)keychainQuery, NULL);

}

+ (id)load:(NSString *)service {

id ret = nil;

NSMutableDictionary *keychainQuery = [self getKeychainQuery:service];

//Configure the search setting

[keychainQuery setObject:(id)kCFBooleanTrue forKey:(__bridge_transfer id)kSecReturnData];

[keychainQuery setObject:(__bridge_transfer id)kSecMatchLimitOne forKey:(__bridge_transfer id)kSecMatchLimit];

CFDataRef keyData = NULL;

if (SecItemCopyMatching((__bridge_retained CFDictionaryRef)keychainQuery, (CFTypeRef *)&keyData) == noErr) {

@try {

ret = [NSKeyedUnarchiver unarchiveObjectWithData:(__bridge_transfer NSData *)keyData];

} @catch (NSException *e) {

NSLog(@"Unarchive of %@ failed: %@", service, e);

} @finally {

}

}

return ret;

}

+ (void)delete:(NSString *)service {

NSMutableDictionary *keychainQuery = [self getKeychainQuery:service];

SecItemDelete((__bridge_retained CFDictionaryRef)keychainQuery);

}

喜欢请关注我

转载请注明出处谢谢:http://my.oschina.net/u/2523541/blog/676745

转载本站任何文章请注明:转载至神刀安全网,谢谢神刀安全网 » 每日一博 | iOS KeyChian (钥匙串)封装

分享到:更多 ()

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址