首先

加入 Library LocalAuthentication.framework


再來在程式內

#import <LocalAuthentication/LocalAuthentication.h>


接下來就是使用TouchID 的API方法

首先確定是否可以使用TouchID


- (BOOL)canEvaluatePolicy:(LAPolicy)policy
                    error:(NSError * _Nullable * _Nullable)error


可以的話再使用

- (void)evaluatePolicy:(LAPolicy)policy
       localizedReason:(NSString * _Nonnull)localizedReason
                 reply:(void (^ _Nonnull)(BOOL success,

                                 NSError * _Nullable error))reply

完整的程式碼

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

    }


回傳錯誤的格式有這些

typedef NS_ENUM (NSInteger,

LAError )

{

LAErrorAuthenticationFailed = kLAErrorAuthenticationFailed,

LAErrorUserCancel           = kLAErrorUserCancel,

LAErrorUserFallback         = kLAErrorUserFallback,

LAErrorSystemCancel         = kLAErrorSystemCancel,

LAErrorPasscodeNotSet       = kLAErrorPasscodeNotSet,

LAErrorTouchIDNotAvailable  = kLAErrorTouchIDNotAvailable,

LAErrorTouchIDNotEnrolled   = kLAErrorTouchIDNotEnrolled,

};


自己試試看吧~

arrow
arrow
    文章標籤
    ios TouchID 指紋辨識
    全站熱搜
    創作者介紹
    創作者 Mr.Lin 的頭像
    Mr.Lin

    Mr.Lin

    Mr.Lin 發表在 痞客邦 留言(0) 人氣()