? ? 在涉及記住app狀態(tài)中吏奸,比如是否是第一次使用app;很多初學(xué)者選擇使用NSUserDefaults
NSUserDefaults使用方法也很簡單煞茫;
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
存值
[defaults?setObject:@"123"?forKey:@"phoneNum"];
defaults setObject:@"111" forKey:@"password"];
[defaults?synchronize];
取值
NSString *phoneNum = [defaults objectForKey:@"phoneNum"];
NSString *passwordStr = [defaults objectForKey:@"password"];
? ? ?但是定硝,使用NSUserDefaults 其實(shí)是一個(gè) plist 文件证芭,并不安全跷车;只適合存儲(chǔ)少量并不重要的數(shù)據(jù)牵现;在用戶名和密碼存儲(chǔ)在客戶端上,我們可以選擇放到系統(tǒng)的KeyChain里面比較安全灶搜;
使用KeyChain鑰匙串存儲(chǔ)密碼
從github下載github.com/baptistefetet/KeychainItemWrapper,導(dǎo)入KeychainItemWrapper.h祟蚀,KeychainItemWrapper.m文件,進(jìn)行編譯割卖,導(dǎo)入Security.framework庫前酿;
保存
KeychainItemWrapper *keyWrapper=[[KeychainItemWrapper alloc] initWithIdentifier:@"Keychain" accessGroup:nil];
[keyWrapper setObject:@"myChainValues" forKey:(id)kSecAttrService];
[keyWrapper setObject:@"123" forKey:(id)kSecAttrAccount];// 上面兩行用來標(biāo)識(shí)一個(gè)Item
[keyWrapper setObject:@"111" forKey:(id)kSecValueData];
讀取
KeychainItemWrapper *keyWrapper=[[KeychainItemWrapper alloc] initWithIdentifier:@"Keychain" accessGroup:nil];
NSString *name =[keyWrapper? objectForKey:(id)kSecAttrAccount];
NSString *password=[keyWrapper objectForKey:(id)kSecValueData];
清空數(shù)據(jù)保存
[wrapper?resetKeychainItem];
? ? ?當(dāng)然,Keychain也不是絕對(duì)安全的鹏溯,而且即使用戶卸載了應(yīng)用罢维,下次再重裝的時(shí)候,還是會(huì)能讀取到之前保存的密碼丙挽。
指紋識(shí)別登錄
? 指紋識(shí)別其實(shí)是調(diào)用手機(jī)系統(tǒng)里面的指紋肺孵,官方集成了調(diào)用方法匀借,使用也非常簡單;具體實(shí)現(xiàn)如下平窘;
1吓肋、導(dǎo)入系統(tǒng)頭文件LocalAuthentication/LocalAuthentication.h
2、判斷是否支持touchID;
? ? ?- (BOOL)canEvaluatePolicy:(LAPolicy)policy error:(NSError * __autoreleasing *)
error__attribute__((swift_error(none)));
3瑰艘、調(diào)用方法是鬼,實(shí)現(xiàn)指紋登錄
LAContext *context = [LAContext new]; //這個(gè)屬性是設(shè)置指紋輸入失敗之后的彈出框的選項(xiàng)
context.localizedFallbackTitle = @"再試一次";
NSError *error = nil;
if([context canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics
error:&error])?{
NSLog(@"支持指紋識(shí)別");
[context?evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics
localizedReason:@"按Home鍵進(jìn)行指紋登錄"reply:^(BOOLsuccess, NSError * _Nullable error) {
if(success)?{
NSLog(@"驗(yàn)證成功?刷新主界面");
//這里寫登錄成功的方法,在這里可以調(diào)用keychin 紫新,來獲取密碼等數(shù)據(jù)
KeychainItemWrapper *keyWrapper=[[KeychainItemWrapper alloc] initWithIdentifier:@"Keychain" accessGroup:nil];
NSString? *passwordStr=[keyWrapper objectForKey:(id)kSecValueData];//獲取指紋保存的密碼
.................//具體實(shí)現(xiàn)登錄的代碼
}else{
NSLog(@"%@",error.localizedDescription);
switch(error.code)?{
caseLAErrorSystemCancel:
{
NSLog(@"系統(tǒng)取消授權(quán)屑咳,如其他APP切入");
break;
}
caseLAErrorUserCancel:
{
NSLog(@"取消驗(yàn)證Touch?ID");
break;
}
caseLAErrorAuthenticationFailed:
{
NSLog(@"授權(quán)失敗");
break;
}
caseLAErrorPasscodeNotSet:
{
NSLog(@"系統(tǒng)未設(shè)置密碼");
break;
}
caseLAErrorTouchIDNotAvailable:
{
NSLog(@"設(shè)備Touch?ID不可用,例如未打開");
break;
}
caseLAErrorTouchIDNotEnrolled:
{
NSLog(@"設(shè)備Touch?ID不可用弊琴,用戶未錄入");
break;
}
caseLAErrorUserFallback:
{
[[NSOperationQueue?mainQueue]?addOperationWithBlock:^{
NSLog(@"用戶選擇輸入密碼,切換主線程處理");
}];
break;
}
default:
{
[[NSOperationQueue?mainQueue]?addOperationWithBlock:^{
NSLog(@"其他情況杖爽,切換主線程處理");
}];
break;
}
}
}
}];
}else{
NSLog(@"不支持指紋識(shí)別");
switch(error.code)?{
caseLAErrorTouchIDNotEnrolled:
{
NSLog(@"TouchID?is?not?enrolled");
break;
}
caseLAErrorPasscodeNotSet:
{
NSLog(@"A?passcode?has?not?been?set");
break;
}
default:
{
NSLog(@"TouchID?not?available");
break;
}
}
NSLog(@"%@",error.localizedDescription);
}
? ?好了敲董,keychain的使用和指紋登錄基本就實(shí)現(xiàn)了,方法寫得很簡陋慰安,請(qǐng)大家見諒腋寨!