Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use TouchID in iOS 10

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.

like image 366
Sankalp Gupta Avatar asked Mar 03 '17 12:03

Sankalp Gupta


People also ask

Does iPhone 10 have Touch ID?

Touch ID on iPhone X does not exist. Although Touch ID, as a feature, is only a few years old, it has already been scrapped on the latest version of the iPhone. You still have Touch ID on iPhone 8/8 Plus but not on iPhone X. That's the bad news.

How do I use fingerprint instead of passcode?

Tap the icon for your account or collection at the top right and choose Settings > Security. If you're using a tablet, tap your account or collection at the top of the sidebar. Tap to turn on biometric unlock, then place your finger on the fingerprint sensor, or let your device scan your face or eyes.

How do I enable Touch ID for IOS apps?

Open the Settings app on your iPhone or iPad. Scroll down to the Touch ID & Passcode option. Enable the iTunes & App Store toggle. You might need to sign into your iTunes or App Store account to do this.


1 Answers

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")
            }
        }
        )
    }
}
like image 133
Andrew Avatar answered Oct 04 '22 11:10

Andrew