项目经验iOS BlogiOS超神之路

iOS:Touch ID简易开发教程-仿alipay

2016-02-25  本文已影响1048人  Monkey_ALin

效果图

touch_ID效果图

前言

2013年9月,苹果为当时发布的最新iPhone产品配备了一系列硬件升级方案。在iPhone 5s当中,最具创新特性的机制无疑要数围绕Home按钮设计的超薄金属圈,也就是被称为Touch ID的指纹传感器。这套Local Authentication框架能够轻松实现用户身份验证,大家可以利用它来完成应用程序的登录机制或者通过它保护应用程序当中的敏感数据。

教程

1.导入对应的框架头文件

刚才我们说到,Touch ID指纹传感器所属Local Authentication框架.所以,第一步,我们需要导入头文件

#import <LocalAuthentication/LocalAuthentication.h>

2.判断设置是否支持Touch ID 或者 本机是否已经录入指纹

这里我们需要使用到LAContext类,LAContext就是Touch ID

    if ([context canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&error]) {
            // 进行指纹验证操作,请看第三步
    }else {
        if (self.isSimulator) { // 判断是否是模拟器Simulator
            [[[UIAlertView alloc] initWithTitle:@"提示" message:@"请用真机测试~" delegate:nil cancelButtonTitle:@"好的" otherButtonTitles:nil, nil] show];
        }else{ // 不支持Touch ID操作
            [[[UIAlertView alloc] initWithTitle:@"提示" message:@"不支持Touch ID~" delegate:nil cancelButtonTitle:@"好的" otherButtonTitles:nil, nil] show];
        }
    }

tip: 判断当前设置是否是模拟器Simulator

- (BOOL)isSimulator{
     struct utsname systemInfo;
     uname(&systemInfo);
     NSString *deviceMachine = [NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding];
    if ([deviceMachine isEqualToString:@"i386"] || [deviceMachine isEqualToString:@"x86_64"])       {
        return YES;
    }
    return NO;
}

3.Touch ID指纹验证

[context evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason:@"请验证已有指纹" reply:^(BOOL success, NSError * _Nullable error) {
            if (error) {
                NSLog(@"验证失败"); // 系统会自动给错误提示
            }else{
                dispatch_async(dispatch_get_main_queue(), ^{
                    // 验证成功,进行相关操作
                });
            }
        }];

PS:如果验证失败的话,系统会给出相应的提示,如图


验证失败

源码下载

<a href="https://github.com/SunLiner/AlipayTouchID">github源码下载</a>

联系我

<a href="https://github.com/SunLiner">github</a>

<a href="http://www.weibo.com/5589163526/profile?rightmod=1&wvr=6&mod=personinfo&is_all=1">微博</a>

<a href="http://www.jianshu.com/users/9723687edfb5/latest_articles">简书</a>

上一篇 下一篇

猜你喜欢

热点阅读