前言
封丘ssl適用于網(wǎng)站、小程序/APP、API接口等需要進(jìn)行數(shù)據(jù)傳輸應(yīng)用場(chǎng)景,ssl證書未來市場(chǎng)廣闊!成為成都創(chuàng)新互聯(lián)公司的ssl證書銷售渠道,可以享受市場(chǎng)價(jià)格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:13518219792(備注:SSL證書合作)期待與您的合作!
說到保存用戶名和密碼,以前有用過本地的數(shù)據(jù)庫來保存,也接觸過用userdefault來保存,后來在一個(gè)項(xiàng)目中發(fā)現(xiàn)了一個(gè)新的方法——用Keychain來保存。下面話不多說了,直接通過示例代碼來介紹吧。
方法示例
一、新建一個(gè)LYKeychainTool類,導(dǎo)入系統(tǒng)Security框架 ,LYKeychainTool.h文件實(shí)現(xiàn)如下:
// // LYKeychainTool.h // keyChainTest // // Created by Liyu on 2017/6/2. // Copyright © 2017年 liyu. All rights reserved. // #import#import @interface LYKeychainTool : NSObject /** * 儲(chǔ)存字符串到鑰匙串 * @param sValue 對(duì)應(yīng)的Value * @param sKey 對(duì)應(yīng)的Key */ + (void)saveKeychainValue:(NSString *)sValue key:(NSString *)sKey; /** * 從鑰匙串獲取字符串 * @param sKey 對(duì)應(yīng)的Key * @return 返回儲(chǔ)存的Value */ + (NSString *)readKeychainValue:(NSString *)sKey; /** * 從鑰匙串刪除字符串 * @param sKey 對(duì)應(yīng)的Key */ + (void)deleteKeychainValue:(NSString *)sKey; @end
二、LYKeychainTool.m文件實(shí)現(xiàn)如下:
// // LYKeychainTool.m // keyChainTest // // Created by Liyu on 2017/6/2. // Copyright © 2017年 liyu. All rights reserved. // #import "LYKeychainTool.h" @implementation LYKeychainTool + (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)saveKeychainValue:(NSString *)sValue key:(NSString *)sKey { NSMutableDictionary * keychainQuery = [self getKeychainQuery:sKey]; SecItemDelete((__bridge_retained CFDictionaryRef)keychainQuery); [keychainQuery setObject:[NSKeyedArchiver archivedDataWithRootObject:sValue] forKey:(__bridge_transfer id)kSecValueData]; SecItemAdd((__bridge_retained CFDictionaryRef)keychainQuery, NULL); } + (NSString *)readKeychainValue:(NSString *)sKey { NSString *ret = nil; NSMutableDictionary *keychainQuery = [self getKeychainQuery:sKey]; [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 CFDictionaryRef)keychainQuery, (CFTypeRef *)&keyData) == noErr) { @try { ret = (NSString *)[NSKeyedUnarchiver unarchiveObjectWithData:(__bridge NSData *)keyData]; } @catch (NSException *e) { NSLog(@"Unarchive of %@ failed: %@", sKey, e); } @finally { } } if (keyData) CFRelease(keyData); return ret; } + (void)deleteKeychainValue:(NSString *)sKey { NSMutableDictionary *keychainQuery = [self getKeychainQuery:sKey]; SecItemDelete((__bridge CFDictionaryRef)keychainQuery); } @end
三、ViewController 調(diào)用
// // ViewController.m // keyChainTest // // Created by Liyu on 2017/6/2. // Copyright © 2017年 liyu. All rights reserved. // #import "ViewController.h" #import "LYKeychainTool.h" @interface ViewController () @property (weak, nonatomic) IBOutlet UITextField *userNameTextField; @property (weak, nonatomic) IBOutlet UITextField *passwordTextField; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; } - (IBAction)saveBtn:(id)sender { [LYKeychainTool saveKeychainValue:self.userNameTextField.text key:@"userName"]; [LYKeychainTool saveKeychainValue:self.passwordTextField.text key:@"password"]; } - (IBAction)readeBtn:(id)sender { self.userNameTextField.text = [NSString stringWithFormat:@"讀取到用戶名:%@",[LYKeychainTool readKeychainValue:@"userName"]]; self.passwordTextField.text = [NSString stringWithFormat:@"讀取到用戶密碼:%@",[LYKeychainTool readKeychainValue:@"password"]]; } - (IBAction)deleteBtn:(id)sender { [LYKeychainTool deleteKeychainValue:@"userName"]; [LYKeychainTool deleteKeychainValue:@"password"]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; } @end
四、效果如下圖:
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對(duì)創(chuàng)新互聯(lián)的支持。