1 、Keychain 是一個很是好用的本地存儲工具 徹底能夠替代NSuserDefault 來使用而且比NSuserDefault更安全 。數據庫
2 、Keychain相對於 NSuserDefault最主要的一點就是 NSuserDefault 只能項目內訪問 Keychain 說是全部項目公用的 能夠作到跨項目讀取數據。安全
三、Keychain常常用來保存用的的帳戶 密碼信息 Keychain在內部實現上實際仍是用的數據庫 來存儲的這裏很少作解釋工具
四、用法 這裏寫死了數據 具體能夠根據實際狀況 和NSuserDefault 用法同樣.net
[ZFYKeychain savePassWord:@"aaa1" forKey:@"aaa"];
NSLog(@"%@", [ZFYKeychain readPassWordObjectForKey:@"aaa"]);
[ZFYKeychain savePassWord:@"bbb1" forKey:@"bbb"];
NSLog(@"%@", [ZFYKeychain readPassWordObjectForKey:@"bbb"]);
[ZFYKeychain deletePassWordObjectForKey:@"aaa"];orm
NSLog(@"%@", [ZFYKeychain readPassWordObjectForKey:@"aaa"]);
NSLog(@"%@", [ZFYKeychain readPassWordObjectForKey:@"bbb"]);blog
五、設置跨APP 共享數據 get
第一種方法:經過plist方式進行it
1>建立一個新文件,選擇Property list文件。io
2>建立一個新的子節點,取名爲keychain-access-groupstable
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裏邊存儲的數據
#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