【IOS开发基础系列】指纹识别专题

1 开发步骤

1.1 效果图

        本功能实现起来是很简单的,因为苹果都已经帮我们封装好了,只需要实现几个方法就可以了。

实现效果图


1.2 实现过程

1.首先引入指纹解锁必须的头文件

LocalAuthentication/LocalAuthentication.h

2.了解下主要的两个方法

        这个方法是判断设备是否支持TouchID的。

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

error __attribute__((swift_error(none)));

这个是用来验证TouchID的,会有弹出框出来。

- (void)evaluatePolicy:(LAPolicy)policy localizedReason:(NSString *)localizedReason reply:(void(^)(BOOLsuccess, NSError * __nullable error))reply;

3.新建LAContext对象

    主要的属性设置

      localizedFallbackTitle:用于设置左边的按钮的名称,默认是Enter Password.

      localizedReason:用于设置提示语,表示为什么要使用Touch ID

解锁失败界面

 //创建LAContext

 LAContext *context = [LAContext new]; //这个属性是设置指纹输入失败之后的弹出框的选项

 context.localizedFallbackTitle = @"没有忘记密码";

4.主要回调方法,包括成功以及失败的

NSError *error = nil;

    if([context canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics

error:&error]) {

        NSLog(@"支持指纹识别");

        [context evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics

        localizedReason:@"指纹解锁" reply:^(BOOLsuccess, NSError * _Nullable error) {

            if(success) {

                NSLog(@"验证成功 刷新主界面");

            }else{

                NSLog(@"%@",error.localizedDescription);

                switch(error.code) {

                    caseLAErrorSystemCancel:

                    {

                        NSLog(@"系统取消授权,如其他APP切入");

                        break;

                    }

                    caseLAErrorUserCancel:

                    {

                        NSLog(@"用户取消验证Touch ID");

                        break;

                    }    

                    caseLAErrorAuthenticationFailed:

                    {

                        NSLog(@"授权失败");

                        break;

                    }

                    caseLAErrorPasscodeNotSet:

                    {

                        NSLog(@"系统未设置密码");

                        break;

                    }

                    caseLAErrorTouchIDNotAvailable:

                    {

                        NSLog(@"设备Touch ID不可用,例如未打开");

                        break;

                    }

                    caseLAErrorTouchIDNotEnrolled:

                    {

                        NSLog(@"设备Touch ID不可用,用户未录入");

                        break;

                    }

                    caseLAErrorUserFallback:

                    {

                        [[NSOperationQueue mainQueue] addOperationWithBlock:^{

                            NSLog(@"用户选择输入密码,切换主线程处理");

                    }];

                        break;

                    }

                    default:

                    {

                        [[NSOperationQueue mainQueue] addOperationWithBlock:^{

                            NSLog(@"其他情况,切换主线程处理");

                        }];

                        break;

                    }

                }    

            }

    }];

    }else{

        NSLog(@"不支持指纹识别");

        switch(error.code) {

            caseLAErrorTouchIDNotEnrolled:

            {

                NSLog(@"TouchID is not enrolled");

                break;

            }

            caseLAErrorPasscodeNotSet:

            {

                NSLog(@"A passcode has not been set");

                break;

            }

            default:

            {

                NSLog(@"TouchID not available");

                break;

            }

        }

        NSLog(@"%@",error.localizedDescription);

    }


2 参考链接

iOS开发实现TouchID指纹解锁

http://www.cocoachina.com/ios/20160831/17439.html


iOS开发之指纹解锁

http://blog.csdn.net/hongfengkt/article/details/49868073

你可能感兴趣的:(【IOS开发基础系列】指纹识别专题)