1. 程式人生 > >Swift下調用Touch ID實現指紋識別

Swift下調用Touch ID實現指紋識別

ren isp password case ios開發 available show use back

iOS開發交流群:484884085

歡迎大家增加。


例如以下代碼是實現調用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實現指紋識別