iOS Touch ID 身份認(rèn)證
iOS 8 及以后錄了指紋的設(shè)備可以使用 touch ID 進(jìn)行身份認(rèn)證,指紋符合錄入的指紋才能認(rèn)證成功。
步驟
調(diào)用 LAContext 對(duì)象的 canEvaluatePolicy 和 evaluatePolicy 方法都要傳入 LAPolicy 枚舉類型的值,目前有兩種取值:deviceOwnerAuthenticationWithBiometrics 和 deviceOwnerAuthentication。前一種 deviceOwnerAuthenticationWithBiometrics 是進(jìn)行指紋認(rèn)證。后一種 deviceOwnerAuthentication 是 iOS 9.0 及以后才能使用,先進(jìn)行指紋認(rèn)證,如果指紋認(rèn)證失敗可以通過輸入密碼進(jìn)行認(rèn)證。
調(diào)用 LAContext 對(duì)象的 evaluatePolicy 方法會(huì)彈出指紋認(rèn)證對(duì)話框。對(duì)話框會(huì)顯示需要進(jìn)行認(rèn)證的原因(String),就是 localizedReason 參數(shù)的值。對(duì)話框有取消按鈕,iOS 10.0 及以后可以設(shè)置 LAContext 對(duì)象的 localizedCancelTitle 的值來改變?nèi)∠粹o顯示的字。如果指紋認(rèn)證失敗,對(duì)話框還會(huì)顯示 fallback 按鈕,可以設(shè)置 LAContext 對(duì)象的 localizedFallbackTitle 的值來改變 fallback 按鈕顯示的字。
需要注意,evaluatePolicy 方法的 reply 回調(diào)不在主線程。如果需要更新 UI 的話,要調(diào)用主線程再更新。
代碼示例
代碼已上傳GitHub:https://github.com/Silence-GitHub/TouchIDDemo
在控制器中放置一個(gè) label 顯示認(rèn)證返回結(jié)果。
指紋認(rèn)證代碼
let context = LAContext()context.localizedFallbackTitle = "Fall back button"if #available(iOS 10.0, *) { context.localizedCancelTitle = "Cancel button"}var authError: NSError?if context.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: &authError) { context.evaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, localizedReason: "Localized reason for authentication with biometrics", reply: { (success, evaluateError) in // NOT in main thread DispatchQueue.main.async { if success { self.label.text = "Success" // Do something success } else if let error = evaluateError { self.label.text = error.localizedDescription // Deal with error if let code = LAError.Code(rawValue: (error as NSError).code) { switch code { case .userFallback: print("fall back button clicked") default: break } } } } })} else if let error = authError { label.text = error.localizedDescription // Deal with error}
指紋和密碼認(rèn)證代碼
if #available(iOS 9.0, *) { let context = LAContext() context.localizedFallbackTitle = "Fall back button" if #available(iOS 10.0, *) { context.localizedCancelTitle = "Cancel button" } var authError: NSError? if context.canEvaluatePolicy(.deviceOwnerAuthentication, error: &authError) { context.evaluatePolicy(.deviceOwnerAuthentication, localizedReason: "Localized reason for authentication", reply: { (success, evaluateError) in // NOT in main thread DispatchQueue.main.async { if success { self.label.text = "Success" // Do something success } else if let error = evaluateError { self.label.text = error.localizedDescription // When fall back button clicked, user is required to enter PIN. Error code will not be "userFallback" // Deal with error } } }) } else if let error = authError { label.text = error.localizedDescription // Deal with error }} else { let alert = UIAlertController(title: nil, message: "Authentication is available on iOS 9.0 or later", preferredStyle: .alert) alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil)) present(alert, animated: true, completion: nil)}
以上就是本文的全部?jī)?nèi)容,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來一定的幫助,同時(shí)也希望多多支持武林網(wǎng)!
新聞熱點(diǎn)
疑難解答
圖片精選