2017-03-03 24 views
8

iOS uygulamasında yerel kimlik doğrulama güvenliği uygulamak istiyorum ancak bir hata alıyorum ve bunu neden aldığımı anlayamıyorum.iOS'taki TouchID nasıl kullanılır? 10

iPhone 5'leri kullanıyorum. Bu önemli mi?

Kodu:

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

Hata: önceden

enter image description here

teşekkürler.

cevap

5

tiplemeleri olmadan Bu kod, bu sorunu gidermeye yönelik neden

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

açıklayınız çalışması gerekir: Bir kapatma döküm tipi olamaz da ondan, o anlamsız beri. –

+0

Hata ile yapılan NSError'ın dökümünü değil, çünkü bu, cast ile yaptığınız şey değil. Bir kapanış, bir tür ve ilişkisiz bir türe göre aşağı doğru bir kuvvet her zaman başarısız olur. 12 olarak! String bir istisna atar. Benzer bir şekilde, ((Bool, NSError?) -> Void) 'inin ilgisiz tipine ('Bool, Error?) -> Void') indirgenmesini zorladığınızda bir istisna alırsınız. NSError ve Error ile ilgili olduğu için ilgili farklı imzalarla kapanmaz. – Paulw11