2017-03-03 4 views
8

私はiOSアプリケーションでローカル認証セキュリティを実装したいと思いますが、私は です。エラーが発生している理由を知ることができません。iOS 10でTouchIDを使用する方法

私はiPhone 5を使用しています。それは重要ですか?

コード:

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、Error?) - > Void)'に強制的に '((Bool、NSError?) - > Void)'を実行すると、例外が発生します。 NSErrorとErrorが関連しているだけで、異なるシグネチャが関連したクロージャは作成されません。 – Paulw11

関連する問題