Touch ID for iOS login

I am making an iOS app (Obj-c) with a login form. I am trying to figure out if there is a way to use Touch ID to sign in. This will be a great feature for my application, but I cannot find a way to do it. In the latest PayPal update, they include the Touch ID name , so there is a way to do it.

EDIT: I know how to get if the user enters the correct Touch ID, but I don't know what to do after that. What if the user enters their username and then adds Touch ID correctly? How can I tell if this user has this Touch ID?

+3


source to share


2 answers


First, create an action like this:

We need more details hardcoded since I don't know if you mean Obj-C or swift, I'll just post both.

Obj-C

Import the on-premises authentication infrastructure first

#import <LocalAuthentication/LocalAuthentication.h>

Then we create an IBAction and add the following code:

- (IBAction)authenticateButtonTapped:(id)sender {
   LAContext *context = [[LAContext alloc] init];

   NSError *error = nil;
   if ([context canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&error]) {
       [context evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics
               localizedReason:@"Are you the device owner?"
                         reply:^(BOOL success, NSError *error) {

           if (error) {
               UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error"
                                                               message:@"There was a problem verifying your identity."
                                                              delegate:nil
                                                     cancelButtonTitle:@"Ok"
                                                     otherButtonTitles:nil];
               [alert show];
               return;
           }

           if (success) {
               UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Success"
                                                               message:@"You are the device owner!"
                                                              delegate:nil
                                                     cancelButtonTitle:@"Ok"
                                                     otherButtonTitles:nil];
               [alert show];

           } else {
               UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error"
                                                               message:@"You are not the device owner."
                                                              delegate:nil
                                                     cancelButtonTitle:@"Ok"
                                                     otherButtonTitles:nil];
               [alert show];
           }

       }];

   } else {

       UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error"
                                                       message:@"Your device cannot authenticate using TouchID."
                                                      delegate:nil
                                             cancelButtonTitle:@"Ok"
                                             otherButtonTitles:nil];
       [alert show];

   }
}

      

Just connect the IBAction to the button that will ask for authentication.

However, in Swift, you have to use:



Add the following structure to your project: Local Authentication Then import it into your swift file:

import LocalAuthentication

      

Then create the following method, which prompts you to use the touch ID:

func authenticateUser() {
        // Get the local authentication context.
        let context = LAContext()

        // Declare a NSError variable.
        var error: NSError?

        // Set the reason string that will appear on the authentication alert.
        var reasonString = "Authentication is needed to access your notes."

        // Check if the device can evaluate the policy.
        if context.canEvaluatePolicy(LAPolicy.DeviceOwnerAuthenticationWithBiometrics, error: &error) {
            [context .evaluatePolicy(LAPolicy.DeviceOwnerAuthenticationWithBiometrics, localizedReason: reasonString, reply: { (success: Bool, evalPolicyError: NSError?) -> Void in

                if success {

                }
                else{
                    // If authentication failed then show a message to the console with a short description.
                    // In case that the error is a user fallback, then show the password alert view.
                    println(evalPolicyError?.localizedDescription)

                    switch evalPolicyError!.code {

                    case LAError.SystemCancel.rawValue():
                        println("Authentication was cancelled by the system")

                    case LAError.UserCancel.rawValue():
                        println("Authentication was cancelled by the user")

                    case LAError.UserFallback.rawValue():
                        println("User selected to enter custom password")
                        self.showPasswordAlert()

                    default:
                        println("Authentication failed")
                        self.showPasswordAlert()
                    }
                }

            })]
        }
        else{
            // If the security policy cannot be evaluated then show a short message depending on the error.
            switch error!.code{

            case LAError.TouchIDNotEnrolled.rawValue():
                println("TouchID is not enrolled")

            case LAError.PasscodeNotSet.rawValue():
                println("A passcode has not been set")

            default:
                // The LAError.TouchIDNotAvailable case.
                println("TouchID not available")
            }

            // Optionally the error description can be displayed on the console.
            println(error?.localizedDescription)

            // Show the custom alert view to allow users to enter the password.
            self.showPasswordAlert()
        }
    }

      

Finally, from func viewDidLoad, call the function like this:authenticateUser()

Hope it helps. Continue coding.

Sources: Swift: App Coda iOS 8 Touch ID Api

Objective-C: tutPlus iOS Touch ID

Thanks to Matt Logan for the updated quick code.

+5


source


I think I understand what I have to do! After the user logs in for the first time, I will save his password (e.g. in NSUserdefaults).

If the user wants to use his Touch ID at the next login - I will ask him for the Touch ID and if he fixes it, I will let him with the password saved at the beginning.



Thanks to Julian :)

+1


source







All Articles