如何将指纹登录添加到iOS应用程序

时间:2017-08-30 04:23:08

标签: ios objective-c

在我的iOS应用程序中,有一个登录页面可以访问这些功能。我想直接使用我的注册指纹登录。如果有人知道这个功能,请帮帮我。

2 个答案:

答案 0 :(得分:1)

我在github上添加了一个库https://github.com/sumitjain7/AppleTouchId/blob/master/appleTouchId/Classes/AppleAuthenticator.swift

目前用于此的播放器不起作用。你可以直接使用这个类。 你只需要这样做:

AppleAuthenticator.sharedInstance.authenticateWithSuccess({

            let vc = self.storyboard?.instantiateViewController(withIdentifier: "YOUR_VC_IDENTIFIER")
            //do your stuff here
        }, failure:{ errorCode in
            var authErrorString : NSString
            switch (errorCode) {
            case LAError.systemCancel.rawValue:
                authErrorString = "System canceled auth request due to app coming to foreground or background.";
                break;
            case LAError.authenticationFailed.rawValue:
                authErrorString = "User failed after a few attempts.";
                break;
            case LAError.userCancel.rawValue:
                authErrorString = "User cancelled.";
                break;

            case LAError.userFallback.rawValue:
                authErrorString = "Fallback auth method should be implemented here.";
                break;
            case LAError.touchIDNotEnrolled.rawValue:
                authErrorString = "No Touch ID fingers enrolled.";
                break;
            case LAError.touchIDNotAvailable.rawValue:
                authErrorString = "Touch ID not available on your device.";
                break;
            case LAError.passcodeNotSet.rawValue:
                authErrorString = "Need a passcode set to use Touch ID.";
                break;
            default:
                authErrorString = "Check your Touch ID Settings.";
                break;
            }
          //  self.presentAlertControllerWithMessage(authErrorString)
        })

答案 1 :(得分:1)

首先导入,

#import <LocalAuthentication/LocalAuthentication.h>

然后在.m

中编写代码
 - (IBAction)authenicateButtonTapped:(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) {
                           dispatch_async(dispatch_get_main_queue(), ^{
                          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];

}
}