本文转载至 http://blog.csdn.net/jinkaiouyang/article/details/35555123xcode
IOS8将指纹识别技术开放出来了。咱们可以利用用户设置的touch ID来进行用户鉴权。
TouchID的API主要集成在LocalAuthentication.framework中。将改framework加入到工程中,而且须要iPhone5S和IOS8系统的支持,就能使用TouchID的APIapp
了。oop

TouchID的API很是简单:ui
一、使用以前,须要先判断TouchID是否支持lua
- - (BOOL)canEvaluatePolicy:(LAPolicy)policy error:(NSError * __autoreleasing *)error;
二、调用TouchID识别接口
- - (void)evaluatePolicy:(LAPolicy)policy localizedReason:(NSString *)localizedReason reply:(void(^)(BOOL success, NSError *error))reply;
policy枚举,表示须要使用到的权限,目前只有一个Biometrics,意思指的就是TouchID
- {
-
-
- LAPolicyDeviceOwnerAuthenticationWithBiometrics = kLAPolicyDeviceOwnerAuthenticationWithBiometrics
- } NS_ENUM_AVAILABLE(10_10, 8_0);
localizedReasonspa
使用TouchID的理由,会出如今弹框中,提示用户。这个字段必须填写且不为nil或空值,不然会抛NSInvalidArgumentException异常,
(void(^)(BOOL success,NSError *error)).net
TouchID输入的结果回调。使用block方式。
示例代码:code
- LAContext *context = [[LAContext alloc] init];
- NSError *contextError = nil;
- NSString *localizedReasonString = @"Need Authorize";
-
- if ([context canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&contextError]) {
- [context evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics
- localizedReason:localizedReasonString
- reply:^(BOOL success, NSError *error) {
- NSString *title, *message;
-
- if (success) {
- title = @"Authorize Success";
- message = nil;
- } else {
- title = @"Authorize Faied";
- message = error.localizedFailureReason;
- }
-
- UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:title
- message:message
- delegate:nil
- cancelButtonTitle:@"OK"
- otherButtonTitles:nil];
- [alertView show];
- }];
- } else {
- UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Context Error"
- message:contextError.localizedFailureReason
- delegate:nil
- cancelButtonTitle:@"OK"
- otherButtonTitles:nil];
- [alertView show];