IOS開發中的KeyChain訪問。

來源:互聯網
上載者:User

標籤:ios開發   蘋果ios   xcode   plist   keychain   

現在正在接觸蘋果IOS開發的項目,使用到如何通過KeyChain實現相同簽名下發布的應用之間的資料共用功能。現分享下心得。

項目是在XCODE6.2開發環境進行的。

IOS中訪問KeyChain有兩種方法,且這兩種方法不能混用,不然會出現無法共用的問題。不過擷取的方法可以通用,以下先給出Keychain訪問的函數原型。

KeyChain.h

+ (void)save:(NSString *)service data:(id)data;+ (id)load:(NSString *)service;+ (void)delete:(NSString *)service;


KeyChain.m
//私人方法,搜尋KeyChain資料集。
+ (NSMutableDictionary *)getKeychainQuery:(NSString *)service {    return [NSMutableDictionary dictionaryWithObjectsAndKeys:            (__bridge id)kSecClassGenericPassword,(__bridge id)kSecClass,            service, (__bridge id)kSecAttrService,            service, (__bridge id)kSecAttrAccount,            (__bridge id)kSecAttrAccessibleAfterFirstUnlock,(__bridge id)kSecAttrAccessible,            nil];}
//儲存KeyChain資料,data裡傳遞的<span style="font-family: Arial, Helvetica, sans-serif;">NSMutableDictionary類型的對象,對象裡的KEY為你需要共用資料的子KEY。</span>+ (void)save:(NSString *)service data:(id)data {    //Get search dictionary    NSMutableDictionary *keychainQuery = [self getKeychainQuery:service];    //Delete old item before add new item    SecItemDelete((__bridge CFDictionaryRef)keychainQuery);    //Add new object to search dictionary(Attention:the data format)    [keychainQuery setObject:[NSKeyedArchiver archivedDataWithRootObject:data] forKey:(__bridge id)kSecValueData];    //Add item to keychain with the search dictionary    SecItemAdd((__bridge CFDictionaryRef)keychainQuery, NULL);}
//載入KeyChain資料,返回的是一個NSMutableDictionary類型的對象,通過NSMutableDictionary中objectForKey的方法,傳遞子KEY來擷取對應共用的資料。+ (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:(__bridge id)kSecReturnData];    [keychainQuery setObject:(__bridge id)kSecMatchLimitOne forKey:(__bridge id)kSecMatchLimit];    CFDataRef keyData = NULL;    if (SecItemCopyMatching((__bridge 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;}
//刪除KeyChain資料,這裡的service傳入的是主Key,所以刪除的是所有的資料。+ (void)delete:(NSString *)service {    NSMutableDictionary *keychainQuery = [self getKeychainQuery:service];    SecItemDelete((__bridge CFDictionaryRef)keychainQuery);}

下面來說如果配置KeyChain。

第一種方法:通過plist方式進行

1、建立一個新檔案,選擇Property list檔案。


2、建立一個新的子節點,取名為keychain-access-groups


3、修改節點keychain-access-groups的類型為Array


4、在keychain-access-groups節點下添加子節點,填入你的主KEY名稱



第二種方式:直接通過XCODE特性來配置(非常簡單)

1、選擇工程配置中的capabilities頁面


2、開啟KeyChain Sharing開關,設定你的主KEY的名稱。



註:以上兩種不能混用,應用會優先使用第一種方法來處理KeyChain共用。

IOS開發中的KeyChain訪問。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.