2017-03-03 32 views
8

我想在我的iOS應用程序中實現本地身份驗證安全性,但我是 出現錯誤,無法弄清楚爲什麼我得到這個。如何在iOS 10中使用TouchID

我正在使用iPhone 5s。這很重要嗎?

碼:

import UIKit 
import LocalAuthentication 

class ViewController: UIViewController { 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     // Do any additional setup after loading the view, typically from a nib. 
    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 

    @IBAction func action(_ sender: Any) { 
     authenticateUser() 
    } 

    func authenticateUser() { 
     let authContext : LAContext = LAContext() 
     var error: NSError? 

     if authContext.canEvaluatePolicy(LAPolicy.deviceOwnerAuthenticationWithBiometrics, error: &error){ 
      authContext.evaluatePolicy(LAPolicy.deviceOwnerAuthenticationWithBiometrics, localizedReason: "Biometric Check for application", reply: {(successful: Bool, error: NSError?) -> Void in 
       if successful{ 
        print("TouchID Yes") 
       } 
       else{ 
        print("TouchID No") 
       } 
       } as! (Bool, Error?) -> Void) 
     } 
     else{ 
      authContext.evaluatePolicy(LAPolicy.deviceOwnerAuthentication, localizedReason: "Enter your Passcode", reply: { 
       (successful: Bool, error: NSError?) in 
       if successful{ 
        print("PassCode Yes") 
       } 
       else{ 
        print("PassCode No") 
       } 
       } as! (Bool, Error?) -> Void) 
     } 
    } 
} 

錯誤:

enter image description here

在此先感謝。

回答

5

沒有強制類型轉換此代碼應該工作

func authenticateUser() { 
    let authContext : LAContext = LAContext() 
    var error: NSError? 

    if authContext.canEvaluatePolicy(LAPolicy.deviceOwnerAuthenticationWithBiometrics, error: &error){ 
     authContext.evaluatePolicy(LAPolicy.deviceOwnerAuthenticationWithBiometrics, localizedReason: "Biometric Check for application", reply: {successful, error -> Void in 
      if successful{ 
       print("TouchID Yes") 
      } 
      else{ 
       print("TouchID No") 
      } 
     } 
     ) 
    } 
    else{ 
     authContext.evaluatePolicy(LAPolicy.deviceOwnerAuthentication, localizedReason: "Enter your Passcode", reply: { 
      successful,error in 
      if successful{ 
       print("PassCode Yes") 
      } 
      else{ 
       print("PassCode No") 
      } 
     } 
     ) 
    } 
} 
+0

請解釋爲什麼這能解決問題:這是因爲你不能類型轉換封閉,因爲這是毫無意義的。 –

+0

這不是將NSError轉換爲錯誤,因爲這不是你在演員時所做的。封閉是一種類型,而向不相關類型傾倒的力量總是會失敗。 '12! String'會拋出一個異常。類似地,當你強迫將((Bool,NSError?) - > Void)'向下變爲無關類型'((Bool,Error?) - > Void)'時,你會得到一個異常。僅僅因爲NSError和Error是相關的,並不會使與不同簽名相關的閉包。 – Paulw11

相關問題