目前常用的App支持指纹解锁的还不是很多,如果在你的项目中用一下是不是显得高大上呢?
废话不说多,干货~
1、在工程中添加LocalAuthentication.framework
2、在需要验证的controller引入头文件
#import <LocalAuthentication/LocalAuthentication.h>
3、加入以下代码
1 //验证指纹解锁是否可用 2 - (void)canEvaluatePolicy 3 { 4 LAContext *context = [[LAContext alloc] init]; 5 NSError *error; 6 BOOL success; 7 8 // test if we can evaluate the policy, this test will tell us if Touch ID is available and enrolled 9 success = [context canEvaluatePolicy: LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&error]; 10 if (success) { 11 NSLog(@"指纹解锁可用"); 12 [self evaluatePolicy]; 13 } else { 14 NSLog(@"此设备指纹解锁不可用"); 15 } 16 } 17 18 //指纹验证 19 - (void)evaluatePolicy 20 { 21 LAContext *context = [[LAContext alloc] init]; 22 23 // show the authentication UI with our reason string 24 [context evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason:@"通过home键验证已有手机指纹" reply: 25 ^(BOOL success, NSError *authenticationError) { 26 if (success) { 27 NSLog(@"success"); 28 } else { 29 NSLog(@"failure : %@ ",authenticationError.localizedDescription); 30 } 31 }]; 32 }
效果图:需要真机调试哦,如果你的iPhone支持指纹解锁的话就可以玩玩啦。如果苦于没有开发者账号,请点击博客Xcode7无证书真机调试(不好意思,已经找不到没广告的博客了)