跟想象的一样,iOS 8中的指纹识别使用起来还是很方便的,只需要一个接口就能搞定,屏幕上弹出一个模态的框,跟app store上的一样。
直接上代码吧,下面代码拷贝自Apple的官方文档【点击】
需要添加LocalAuthentication.framework库,注意只有真机才有这个库,模拟器没有
#import "LocalAuthentication/LAContext.h"
LAContext *myContext = [[LAContext alloc] init];
NSError *authError = nil;
NSString *myLocalizedReasonString = @"请输入指纹";
if ([myContext canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&authError]) {
[myContext evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics
localizedReason:myLocalizedReasonString
reply:^(BOOL success, NSError *error) {
if (success) {
// User authenticated successfully, take appropriate action
} else {
// User did not authenticate successfully, look at error and take appropriate action
}
}];
} else {
// Could not evaluate policy; look at authError and present an appropriate message to user
}
效果如图所示:MyAddressBook 是我的app名字,“请输入指纹”是代码中的字符串
还是很简单的。