1 、Keychain 是一個非常好用的本機存放區工具 完全可以替代NSuserDefault 來使用並且比NSuserDefault更安全 。
2 、Keychain相對於 NSuserDefault最主要的一點就是 NSuserDefault 只能項目內訪問 Keychain 說是所有項目公用的 可以做到跨項目讀取資料。
3、Keychain經常用來儲存用的的賬戶 密碼資訊 Keychain在內部實現上實際還是用的資料庫 來儲存的這裡不多做解釋
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裡邊儲存的資料
#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);
}