iOS8新增了LocalAuthentication框架,用於TouchID的授權使用。親測,目前需要用戶的設備支持指紋識別並已設置鎖屏,而且實際測試過程中反饋比較慢。不能直接跟第三方賬號密碼綁定,如果需要實現第三方應用直接指紋識別登錄,需要在本地存儲賬號信息,指紋識別通過之後再從本地讀取賬號信息登錄。總之,目前的指紋識別是跟設備、設備鎖屏密碼綁定的。測試代碼:
///使用TouchID
-(void)usingTouchID
{
LAContext *myContext = [[[LAContext alloc] init] autorelease];
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
NSLog(@"成功");
UIAlertView *alertView = [[[UIAlertView alloc] initWithTitle:@"Suceess" message:@"已通過指紋識別!" delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil] autorelease];
[alertView show];
} else {
// User did not authenticate successfully, look at error and take appropriate action
NSLog(@"fail with error:%@",error);
UIAlertView *alertView = [[[UIAlertView alloc] initWithTitle:@"Failed" message:[error localizedDescription] delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil] autorelease];
[alertView show];
}
}];
} else {
// Could not evaluate policy; look at authError and present an appropriate message to user
NSLog(@"auth with error:%@",authError);
UIAlertView *alertView = [[[UIAlertView alloc] initWithTitle:@"Failed" message:[authError localizedDescription] delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil] autorelease];
[alertView show];
}
}