IOS8 TouchID使用介绍
本文转载至 http://blog.csdn.net/jinkaiouyang/article/details/35555123
IOS8将指纹识别技术开放出来了。我们能够利用用户设置的touch ID来进行用户鉴权。
TouchID的API主要集成在LocalAuthentication.framework中。将改framework加入到工程中,并且需要iPhone5S和IOS8系统的支持,就能使用TouchID的API
了。
TouchID的API非常简单:
1、使用之前,需要先判断TouchID是否支持
- - (BOOL)canEvaluatePolicy:(LAPolicy)policy error:(NSError * __autoreleasing *)error;
2、调用TouchID识别接口
- - (void)evaluatePolicy:(LAPolicy)policy localizedReason:(NSString *)localizedReason reply:(void(^)(BOOL success, NSError *error))reply;
policy枚举,表示需要使用到的权限,目前只有一个Biometrics,意思指的就是TouchID
- {
- /// Device owner was authenticated using a biometric method. Biometrics (Touch ID) is required.
- /// If Touch ID is not enabled, policy evaluation fails.
- LAPolicyDeviceOwnerAuthenticationWithBiometrics = kLAPolicyDeviceOwnerAuthenticationWithBiometrics
- } NS_ENUM_AVAILABLE(10_10, 8_0);
localizedReason
使用TouchID的理由,会出现在弹框中,提示用户。这个字段必须填写且不为nil或空值,否则会抛NSInvalidArgumentException异常,
(void(^)(BOOL success,NSError *error))
TouchID输入的结果回调。使用block方式。
示例代码:
- 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];