9

I want to implement local authentication security in my iOS app but I'm getting an error and not able to figure out why I'm getting this.

I'm using iPhone 5s. Is that matters?

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

Error:

enter image description here

Thanks in advance.

Sankalp Gupta
  • 129
  • 1
  • 12

1 Answers1

5

This code without typecasting should work

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")
            }
        }
        )
    }
}
0x384c0
  • 2,256
  • 1
  • 17
  • 14
  • Please explain why this fixes the problem: It's because you can't type cast a closure, since that's meaningless. – Bruno Philipe Mar 06 '17 at 20:54
  • It's not the casting of NSError to Error, since that is not what you are doing with the cast. A closure is a type and a force downcast to an unrelated type will always fail. `12 as! String` will throw an exception. Similarly when you force downcast `((Bool, NSError?) -> Void)` to the unrelated type `((Bool,Error?) -> Void)` you get an exception. Just because NSError and Error are related doesn't make closures with different signatures related. – Paulw11 Mar 12 '17 at 00:38