在進行簡單的登陸註冊介面設計中往往都會遇到使用者名稱和密碼的存取設計問題,在此暫不討論其他解決方案,現在介紹一種相對簡單的方式--keychain。
蘋果已經有現成的類封裝好了keychain,KeychainItemWrapper.h和KeychainItemWrapper.m檔案,可以在GenericKeychain執行個體裡找到。
但是現在只是進行簡單的存取使用者名稱和密碼操作,只需要自己寫一個類;代碼如下(來自網上)
CHkeychain.h :
#import <Foundation/Foundation.h>#import <Security/Security.h>@interface CHKeychain : NSObject+ (void)save:(NSString *)service data:(id)data;+ (id)load:(NSString *)service;+ (void)delete:(NSString *)service;@end
CHkeychain.m
#import "CHKeychain.h"@implementation CHKeychain+ (NSMutableDictionary *)getKeychainQuery:(NSString *)service { return [NSMutableDictionary dictionaryWithObjectsAndKeys: (id)kSecClassGenericPassword,(id)kSecClass, service, (id)kSecAttrService, service, (id)kSecAttrAccount, (id)kSecAttrAccessibleAfterFirstUnlock,(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((CFDictionaryRef)keychainQuery); //Add new object to search dictionary(Attention:the data format) [keychainQuery setObject:[NSKeyedArchiver archivedDataWithRootObject:data] forKey:(id)kSecValueData]; //Add item to keychain with the search dictionary SecItemAdd((CFDictionaryRef)keychainQuery, NULL);}+ (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:(id)kSecReturnData]; [keychainQuery setObject:(id)kSecMatchLimitOne forKey:(id)kSecMatchLimit]; CFDataRef keyData = NULL; if (SecItemCopyMatching((CFDictionaryRef)keychainQuery, (CFTypeRef *)&keyData) == noErr) { @try { ret = [NSKeyedUnarchiver unarchiveObjectWithData:(NSData *)keyData]; } @catch (NSException *e) { NSLog(@"Unarchive of %@ failed: %@", service, e); } @finally { } } if (keyData) CFRelease(keyData); return ret;}+ (void)delete:(NSString *)service { NSMutableDictionary *keychainQuery = [self getKeychainQuery:service]; SecItemDelete((CFDictionaryRef)keychainQuery);}@end
注意事項:
一、還要引入幾個字串來做可以
我這裡弄了一個類keychainfile:
keychainfile.h檔案如下:
//使用者名稱和密碼儲存的key值,使用者名稱和密碼儲存在keychain中。extern NSString * const KEY_USERNAME_PASSWORD;extern NSString * const KEY_USERNAME;extern NSString * const KEY_PASSWORD;
keychainkey.m檔案如下:
#import "keychainfile.h"NSString * const KEY_USERNAME_PASSWORD = @"com.company.app.usernamepassword";NSString * const KEY_USERNAME = @"com.company.app.username";NSString * const KEY_PASSWORD = @"com.company.app.password";
二、注意要在CHkeychain.h檔案中匯入#import <Security/Security.h> 而且要在target中添加這個Security.framework
三、在使用了ARC的工程中直接添加以上檔案,可能會出現ARC報錯,原因就是CHkeychain.m檔案沒有ARC特性,那麼就對CHkeychain.m添加ARC。具體就是在target中選中右側的“Build Phases”,向下就找到“Compile Sources”了。雙擊要添加arc特性的檔案,添加:-fno-objc-arc參數。
(附:在未使用arc的工程中引入一個使用了arc特性的檔案:對相應的檔案添加:-fobjc-arc參數)
用法:
1、定義一個使用者
NSMutableDictionary *usernamepasswordKVPairs = (NSMutableDictionary *)[CHKeychain load:KEY_USERNAME_PASSWORD];
2、儲存
[usernamepasswordKVPairs setObject:userName.text forKey:KEY_USERNAME];[usernamepasswordKVPairs setObject:passWord.text forKey:KEY_PASSWORD];[CHKeychain save:KEY_USERNAME_PASSWORD data:usernamepasswordKVPairs];
3、使用
usernamepasswordKVPairs = (NSMutableDictionary *)[CHKeychain load:KEY_USERNAME_PASSWORD];
4、刪除
[CHKeychain delete:KEY_USERNAME_PASSWORD];
Ok,介紹完畢。