鑰匙串升酣,實(shí)際上是一個(gè)加密后的數(shù)據(jù)庫,如下圖所示励两。
即使吧App刪除,鑰匙串里面的數(shù)據(jù)也不會(huì)丟失囊颅。
數(shù)據(jù)都是以 Item 的形式來存儲(chǔ)的当悔,每個(gè) Item 由一個(gè)加密后的 Data 數(shù)據(jù),還有一系列用來描述該 Item 屬性的 Attributes 組成踢代。
由于是數(shù)據(jù)庫盲憎,關(guān)鍵方法只有四種,增刪改查胳挎,對應(yīng)的是
- SecItemAdd
- SecItemDelete
- SecItemUpdate
- SecItemCopyMatching
下面簡單講述一下使用方法
SecItemAdd
CFTypeRef result;
NSDictionary *query = @{
// 一個(gè)典型的新增方法的參數(shù)饼疙,包含三個(gè)部分
// 1.kSecClass key,它用來指定新增對象的類型
(NSString *)kSecClass: (NSString *)kSecClassGenericPassword,
// 2.若干項(xiàng)屬性 key慕爬,例如 kSecAttrAccount窑眯,kSecAttrLabel 等屏积,用來描述新增對象的屬性
(NSString *)kSecAttrAccount: @"uniqueID",
// 3.kSecValueData key,用來設(shè)置新增對象保存的數(shù)據(jù)
(NSString *)kSecValueData: [@"token" dataUsingEncoding:NSUTF8StringEncoding],
// 可選
// 如果需要獲取新增的 Item 對象的屬性磅甩,需要如下屬性肾请,
(NSString *)kSecReturnData: (NSNumber *)kCFBooleanTrue,
(NSString *)kSecReturnAttributes: (NSNumber *)kCFBooleanTrue,
};
OSStatus status = SecItemAdd((CFDictionaryRef)query, &result);
if (result == errSecSuccess) {
// 新增成功
NSDictionary *itemInfo = (__bridge NSDictionary *)result;
NSLog(@"info: %@", itemInfo);
} else {
// 其他錯(cuò)誤
}
result類型判斷方式
kSecReturnData | kSecReturnAttributes | result |
---|---|---|
true | true | NSDictionary(包含屬性與數(shù)據(jù)) |
true | false | NSData(只有數(shù)據(jù)) |
false | true | NSDictionary(只包含屬性) |
false | false | null |
SecItemDelete
NSDictionary *query = @{
// 一個(gè)典型的刪除方法的參數(shù),包含兩個(gè)部分
// 1更胖、kSecClass key,它用來指定刪除對象的類型隔显,必填却妨。
(NSString *)kSecClass: (NSString *)kSecClassGenericPassword,
// 2、若干項(xiàng)屬性 key括眠,可選彪标。
// 例如 kSecAttrAccount,kSecAttrLabel 等掷豺,用來設(shè)置刪除對象的范圍
// 默認(rèn)情況下捞烟,符合條件的全部 Item 都會(huì)被刪除
(NSString *)kSecAttrAccount: @"uniqueID",
};
OSStatus status = SecItemDelete((CFDictionaryRef)query);
if (result == errSecSuccess) {
// 刪除成功
} else {
// 其他錯(cuò)誤
}
SecItemUpdate
// 1、找出需要更新屬性的 Item
// 參數(shù)格式與 SecItemCopyMatching 方法中的參數(shù)格式相同
NSDictionary *query = @{
(NSString *)kSecClass: (NSString *)kSecClassGenericPassword,
(NSString *)kSecAttrAccount: @"uniqueID",
};
// 2当船、需要更新的屬性
// 若干項(xiàng)屬性 key
NSDictionary *update = @{
(NSString *)kSecAttrAccount: @"another uniqueID",
(NSString *)kSecValueData: @"another value",
};
OSStatus status = SecItemUpdate((CFDictionaryRef)query, (CFDictionaryRef)update);
if (result == errSecSuccess) {
// 更新成功
} else {
// 其他錯(cuò)誤
}
SecItemDelete
NSDictionary *query = @{
// 一個(gè)典型的刪除方法的參數(shù)题画,包含兩個(gè)部分
// 1、kSecClass key德频,它用來指定刪除對象的類型苍息,必填。
(NSString *)kSecClass: (NSString *)kSecClassGenericPassword,
// 2壹置、若干項(xiàng)屬性 key竞思,可選。
// 例如 kSecAttrAccount钞护,kSecAttrLabel 等盖喷,用來設(shè)置刪除對象的范圍
// 默認(rèn)情況下,符合條件的全部 Item 都會(huì)被刪除
(NSString *)kSecAttrAccount: @"uniqueID",
};
OSStatus status = SecItemDelete((CFDictionaryRef)query);
if (result == errSecSuccess) {
// 刪除成功
} else {
// 其他錯(cuò)誤
}
SecItemCopyMatching
CFTypeRef result;
NSDictionary *query = @{
// 一個(gè)典型的搜索方法的參數(shù)难咕,包含三個(gè)部分
// 1课梳、kSecClass key(必填),它用來指定搜索對象的類型
(NSString *)kSecClass: (NSString *)kSecClassGenericPassword,
// 2余佃、若干項(xiàng)屬性 key(可選)惦界,例如 kSecAttrAccount,kSecAttrLabel 等咙冗,用來描述搜索對象的屬性
(NSString *)kSecAttrAccount: @"uniqueID",
// 3沾歪、搜索屬性(可選)
// 例如 kSecMatchLimit(搜索一個(gè)還是多個(gè),影響返回結(jié)果類型)
// kSecMatchCaseInsensitive 是否大小寫敏感等
(NSString *)kSecMatchLimit: (NSString *)kSecMatchLimitAll,
(NSString *) kSecMatchCaseInsensitive: (NSNumber *) kCFBooleanTrue,
// (可選)如果需要獲取新增的 Item 對象的屬性雾消,需要如下屬性灾搏,
(NSString *)kSecReturnData: (NSNumber *)kCFBooleanTrue,
(NSString *)kSecReturnAttributes: (NSNumber *)kCFBooleanTrue,
};
OSStatus status = SecItemCopyMatching((CFDictionaryRef)query, &result);
if (result == errSecSuccess) {
// 新增成功
NSDictionary *itemInfo = (__bridge NSDictionary *)result;
NSLog(@"info: %@", itemInfo);
} else {
// 其他錯(cuò)誤
}
result類型判斷方式
kSecReturnData | kSecReturnAttributes | kSecMatchLimit | result |
---|---|---|---|
true | true | kSecMatchLimitAll | NSDictionary(包含屬性與數(shù)據(jù))數(shù)組 |
true | true | kSecMatchLimitOne | NSDictionary(包含屬性與數(shù)據(jù))對象 |
true | false | kSecMatchLimitAll | NSData(只有數(shù)據(jù))數(shù)組 |
true | false | kSecMatchLimitOne | NSData(只有數(shù)據(jù))對象 |
false | true | kSecMatchLimitAll | NSDictionary(只包含屬性)數(shù)組 |
false | true | kSecMatchLimitOne | NSDictionary(只包含屬性)對象 |
false | false | kSecMatchLimitAll | 空數(shù)組 |
false | false | kSecMatchLimitOne | null |
參考文檔
https://developer.apple.com/documentation/security/keychain_services/keychain_items?language=occ
Demo 地址
https://github.com/huangrrui/Key-chain