iPhone5以上設備不支持Touch ID,iPhone5s并且iOS 8 SDK開始支持Touch ID。
配置
framework中引入LocalAuthentication, 該framework實際上只有三個頭文件:
LAContext.h
LAError.h
LAPublicDefines.h
主要使用方法有兩個:
// 用來判斷設備是否支持Touch ID
-(BOOL)canEvaluatePolicy:(LAPolicy)policy error:(NSError * __autoreleasing )error;
// 真正驗證身份
-(void)evaluatePolicy:(LAPolicy)policy localizedReason:(NSString)localizedReason reply:(void(^)(BOOL success, NSError *error))reply;
認證失敗錯誤類型
1.認證是不成功的铸敏,因為用戶無法提供有效的指紋祟霍。
LAErrorAuthenticationFailed = kLAErrorAuthenticationFailed
2.認證取消了用戶(例如:取消按鈕)歪泳。
LAErrorUserCancel = kLAErrorUserCancel
3.認證被取消了锉屈,因為用戶點擊回退按鈕(輸入密碼)。
LAErrorUserFallback = kLAErrorUserFallback
4.認證取消了系統(tǒng)(例如办龄,另一個應用程序到前臺烘绽,用戶切到了其他程序)。
LAErrorSystemCancel = kLAErrorSystemCancel
5.認證無法啟動俐填,因為密碼沒有在設備上設置安接。
LAErrorPasscodeNotSet = kLAErrorPasscodeNotSet
6.無法啟動身份驗證,因為touch id 在此臺設備上是無效的玷禽。
LAErrorTouchIDNotAvailable = kLAErrorTouchIDNotAvailable
7.認證不能啟動赫段,因為 touch id 沒有錄入指紋。
LAErrorTouchIDNotEnrolled = kLAErrorTouchIDNotEnrolled
8.認證是不成功的矢赁,因為有太多的失敗的觸摸使touchID被鎖了。(需要解鎖Touch ID贬丛,例如評估lapolicydeviceownerauthenticationwithbiometrics會要求密碼為前提撩银。)
LAErrorTouchIDLockout = kLAErrorTouchIDLockout
9.認證被取消的應用(如電話應用進入前臺當前軟件被動掛起,用戶不能控制的掛起)豺憔。
LAErrorAppCancel = kLAErrorAppCancel
10.LAContext就是授權過程中LAContext對象被釋放掉了额获,造成的授權失敗够庙。
LAErrorInvalidContext = kLAErrorInvalidContext
備注
1.在 iPhone5 上的時候,LAContext *myContext = [[LAContext alloc] init]; 初始化的對象為 nil。
2.在5s 上沒有設置指紋密碼時, error = -7 (LAErrorTouchIDNotEnrolled)抄邀。
3.輸入錯誤指紋時.若一共有三次機會,三次全部錯誤后, error = -1 (LAErrorAuthenticationFailed)
4.myContext.localizedFallbackTitle耘眨,修改指紋驗證彈框右側輸入密碼按鈕文案屬性。
5.myContext.localizedCancelTitle境肾,修改指紋驗證彈框左側取消按鈕文案屬性剔难。
6.myContext.maxBiometryFailures,修改指紋驗證最大次數(shù)屬性奥喻。
7.認證失敗錯誤類型7偶宫,8,9三項是iOS9加入的三種新錯誤類型环鲤。
代碼
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"" message:@"" delegate:self cancelButtonTitle:@"Okay" otherButtonTitles:nil, nil];
LAContext *myContext = [[LAContext alloc] init];
NSError *authError = nil;
NSString *myLocalizedReasonString = @"Restricted Area!";
//檢查Touch ID是否可用
if ([myContext canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&authError]) {
//獲得指紋驗證結果
[myContext evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics
localizedReason:myLocalizedReasonString
reply:^(BOOL success, NSError *error) {
if (success) {
// User authenticated successfully, take appropriate action
alert.title = @"Success";
alert.message = @"You have access to private content!";
[alert show];
} else {
// User did not authenticate successfully, look at error and take appropriate action
alert.title = @"Fail";
switch (error.code) {
case LAErrorUserCancel:
alert.message = @"Authentication Cancelled";
break;
case LAErrorAuthenticationFailed:
alert.message = @"Authentication Failed";
break;
case LAErrorPasscodeNotSet:
alert.message = @"Passcode is not set";
break;
case LAErrorSystemCancel:
alert.message = @"System cancelled authentication";
break;
case LAErrorUserFallback:
alert.message = @"You chosed to try password";
break;
default:
alert.message = @"You cannot access to private content!";
break;
}
[alert show];
}
}];
} else {
// Could not evaluate policy; look at authError and present an appropriate message to user
alert.title = @"Warning";
if(authError.code == LAErrorTouchIDNotEnrolled) {
alert.message = @"You do not have any fingerprints enrolled!";
}else if(authError.code == LAErrorTouchIDNotAvailable) {
alert.message = @"Your device does not support TouchID authentication!";
}else if(authError.code == LAErrorPasscodeNotSet){
alert.message = @"Your passcode has not been set";
}
[alert show];
}