2017-03-03 29 views
8

Tôi muốn triển khai bảo mật xác thực cục bộ trong ứng dụng iOS của mình nhưng tôi bị lỗi và không thể tìm ra lý do tại sao tôi nhận được điều này.Cách sử dụng TouchID trong iOS 10

Tôi đang sử dụng iPhone 5s. Điều đó có quan trọng không?

Code:

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) 
     } 
    } 
} 

Lỗi:

enter image description here

Cảm ơn trước.

Trả lời

5

Mã này mà không typecasting nên làm việc

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

Xin giải thích tại sao điều này sửa chữa vấn đề: Đó là bởi vì bạn không thể gõ bỏ đóng cửa, vì đó là vô nghĩa. –

+0

Nó không phải là việc tạo ra lỗi NSError, vì đó không phải là những gì bạn đang làm với dàn diễn viên. Một đóng cửa là một loại và một lực lượng downcast đến một loại không liên quan sẽ luôn luôn thất bại. '12 như! Chuỗi' sẽ ném một ngoại lệ. Tương tự như vậy khi bạn ép downcast '((Bool, NSError?) -> Void)' thành kiểu không liên quan '((Bool, Error?) -> Void)' bạn nhận được một ngoại lệ. Chỉ vì NSError và Error có liên quan không đóng cửa với các chữ ký khác nhau liên quan. – Paulw11

Các vấn đề liên quan