用于登录iOS的Touch ID

Tab*_*kov 5 iphone ios touch-id

我正在使用登录表单制作iOS应用程序(Obj-c).我想弄清楚是否有办法使用Touch ID登录.这将是我的应用程序的一个惊人的功能,但我找不到办法做到这一点.在上一次PayPal更新中,它们包含Touch ID登录 - 因此有一种方法可以执行此操作.

编辑: 我知道如果用户输入正确的Touch ID,但我不知道该怎么做.如果用户输入其用户名然后正确添加Touch ID,该怎么办?我怎么知道这个用户有这个Touch ID?

Jul*_* E. 7

好的,首先创建一个这样的动作:

我们需要更多细节,因为我不知道你的意思是Obj-C还是swift,我只会发布两者.

OBJ-C

首先导入本地身份验证框架

#import <LocalAuthentication/LocalAuthentication.h>

然后我们创建一个IBAction并添加以下代码:

- (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];

   }
}
Run Code Online (Sandbox Code Playgroud)

只需将您的IBAction连接到一个提示身份验证的按钮即可.

但是在Swift中你会使用:

将以下框架添加到您的项目:LocalAuthentication 然后将其导入您的swift文件:

import LocalAuthentication
Run Code Online (Sandbox Code Playgroud)

然后创建以下方法,提示您使用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()
        }
    }
Run Code Online (Sandbox Code Playgroud)

最后从func viewDidLoad调用函数如下:authenticateUser()

希望有所帮助.继续编码.

来源:Swift: App Coda iOS 8 Touch ID Api

Objective-C: tutPlus iOS Touch ID

感谢Matt Logan提供更新的swift代码.

  • touch id与用户名无关,它与您的设备相关联并配置了触摸ID.配置的触摸ID可以通过您的登录检查 (3认同)
  • 这段代码不是你自己的.你应该提供一个指向你所在位置的链接,这样你就可以给予原作者信用.这是礼仪.但是,如果您要将其作为自己发布,至少应该使用最新的Swift版本进行更新.方法.toRaw()不再可用,并且已被替换为.rawValue. (2认同)