標籤:
如下代碼是實現調用5S以上裝置指紋識別的代碼,按照官網給出的代碼,會出現識別成功後很久才執行成功後調用的代碼,逛了很久的,才知道要將其放入主線程實現才可以。具體代碼如下。
1、引入類 import LocalAuthentication
//調用指紋識別函數 func loginWithTouchID() { if((UIDevice.currentDevice().systemVersionasNSString).floatValue >= 8.0) { // Get the local authentication context. let context = LAContext() // Declare a NSError variable. var error: NSError? // Set the reason string that will appear on the authentication alert. var reasonString = "彈出指紋識別框時提示使用者的字串" // Check if the device can evaluate the policy. if context.canEvaluatePolicy(LAPolicy.DeviceOwnerAuthenticationWithBiometrics, error: &error) { context.evaluatePolicy(LAPolicy.DeviceOwnerAuthenticationWithBiometrics, localizedReason: reasonString, reply: { (success: Bool, evalPolicyError: NSError?) -> Voidin dispatch_async(dispatch_get_main_queue(), { () -> Voidin //放到主線程執行,這裡特別重要 if success { //調用成功後你想做的事情 } else { // If authentication failed then show a message to the console with a short description. // In case that the error is a user fallback, then show the password alert view. println(evalPolicyError?.localizedDescription) } }) }) } else { // If the security policy cannot be evaluated then show a short message depending on the error. switch error!.code { caseLAError.TouchIDNotEnrolled.rawValue: println("您還沒有儲存TouchID指紋") caseLAError.PasscodeNotSet.rawValue: println("您還沒有設定密碼") default: // The LAError.TouchIDNotAvailable case. println("TouchID不可用") } // Optionally the error description can be displayed on the console. println(error?.localizedDescription) // Show the custom alert view to allow users to enter the password. } } }
著作權聲明:本文為博主原創文章,未經博主允許不得轉載。
Swift下調用Touch ID實現指紋識別