首先import要用的库<LocalAuthentication/LocalAuthentication.h>
主要认准两个方法,一个是判断是否支持TouchID的方法:
- (BOOL)canEvaluatePolicy:(LAPolicy)policy error:(NSError * __autoreleasing *)error __attribute__((swift_error(none)));
另一个方法是调起TouchID:
- (void)evaluatePolicy:(LAPolicy)policy localizedReason:(NSString *)localizedReason reply:(void(^)(BOOL success, NSError * __nullable error))reply;
只要能看明白这两个方法,基本上就没问题了。
下面看一下怎么用:
LAContext *context = [LAContext new]; context.localizedCancelTitle = @"取消"; context.localizedFallbackTitle = @"返回"; if ([context canEvaluatePolicy:LAPolicyDeviceOwnerAuthentication error:nil]) {//判断是否支持touchID [context evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason:@"我们要验证指纹" reply:^(BOOL success, NSError * _Nullable error) { if (success) { [self performSelectorOnMainThread:@selector(btnTouchIDHide) withObject:nil waitUntilDone:NO]; } else { //do something when failed } }]; } else { //do something when the policy can be evaluated }
文章来源: 接入TouchID