首页 > 代码库 > IOS8 TouchID使用介绍
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是否支持
[objc] view plaincopy
- - (BOOL)canEvaluatePolicy:(LAPolicy)policy error:(NSError * __autoreleasing *)error;
2、调用TouchID识别接口
[objc] view plaincopy
- - (void)evaluatePolicy:(LAPolicy)policy localizedReason:(NSString *)localizedReason reply:(void(^)(BOOL success, NSError *error))reply;
policy枚举,表示需要使用到的权限,目前只有一个Biometrics,意思指的就是TouchID
[objc] view plaincopy
- {
- /// 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方式。
示例代码:
[objc] view plaincopy
- 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];
IOS8 TouchID使用介绍
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。