iOS 8 Touch ID和;密码

时间:2015-01-18 03:26:47

标签: ios7 ios8 keychain biometrics touch-id

我在iOS 8中使用此代码以确保安全性并使用触摸ID

- (IBAction)authenticateButtonTapped{





    LAContext *myContext = [[LAContext alloc] init];
    NSError *authError = nil;
    NSString *myLocalizedReasonString = @"Authenticate using your finger\r Scan Your Finger Now";
    if ([myContext canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&authError]) {

        [myContext evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics
                  localizedReason:myLocalizedReasonString
                            reply:^(BOOL succes, NSError *error) {

                                if (succes) {

                                    [self showMessage:@"Authentication is successful" withTitle:@"Success"];
                                    NSLog(@"User authenticated");





                                } else {

                                    switch (error.code) {
                                        case LAErrorAuthenticationFailed:
                                            [self showMessage:@"Authentication is failed" withTitle:@"Error"];
                                            NSLog(@"Authentication Failed");
                                            break;

                                        case LAErrorUserCancel:
                                            [self showMessage:@"You clicked on Cancel" withTitle:@"Error"];
                                            NSLog(@"User pressed Cancel button");
                                            break;

                                        case LAErrorUserFallback:
                                            [self showMessage:@"You clicked on \"Enter Password\"" withTitle:@"Error"];
                                            NSLog(@"User pressed \"Enter Password\"");

                                            [self copyMatchingAsync];
                                            break;

                                        default:
                                            [self showMessage:@"Touch ID is not configured" withTitle:@"Error"];
                                            NSLog(@"Touch ID is not configured");
                                            break;
                                    }

                                    NSLog(@"Authentication Fails");
                                }
                            }];
    } else {
        NSLog(@"Can not evaluate Touch ID");
        [self showMessage:@"Can not evaluate TouchID" withTitle:@"Error"];

    }







}

使用密码系统后,我从苹果示例中复制此代码

- (void)copyMatchingAsync
{
    NSDictionary *query = @{
                            (__bridge id)kSecClass: (__bridge id)kSecClassGenericPassword,
                            (__bridge id)kSecAttrService: @"SampleService",
                            (__bridge id)kSecReturnData: @YES,
                            (__bridge id)kSecUseOperationPrompt: NSLocalizedString(@"AUTHENTICATE_TO_ACCESS_SERVICE_PASSWORD", nil)
                            };

    dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        CFTypeRef dataTypeRef = NULL;
        NSString *msg;

        OSStatus status = SecItemCopyMatching((__bridge CFDictionaryRef)(query), &dataTypeRef);
        if (status == errSecSuccess)
        {
            NSData *resultData = ( __bridge_transfer NSData *)dataTypeRef;
            NSString * result = [[NSString alloc] initWithData:resultData encoding:NSUTF8StringEncoding];

            msg = [NSString stringWithFormat:NSLocalizedString(@"RESULT", nil), result];
        } else {

        }

    });
}

-(void) showMessage:(NSString*)message withTitle:(NSString *)title
{

    UIAlertController * alert=   [UIAlertController
                                  alertControllerWithTitle:title
                                  message:message
                                  preferredStyle:UIAlertControllerStyleAlert];


    UIAlertAction* cancel = [UIAlertAction
                             actionWithTitle:@"OK"
                             style:UIAlertActionStyleDefault
                             handler:^(UIAlertAction * action)
                             {
                                 [alert dismissViewControllerAnimated:YES completion:nil];

                             }];

    [alert addAction:cancel];
    [self presentViewController:alert animated:YES completion:nil];
}

它对指纹的工作非常快速,但密码系统无法显示并且无法正常工作。我收到“ERROR_ITEM_NOT_FOUND”=“找不到错误项目”;

此苹果链接https://developer.apple.com/library/ios/samplecode/KeychainTouchID/Introduction/Intro.html

但我无法理解

2 个答案:

答案 0 :(得分:0)

很抱歉告诉你,但你做不到。 您正在将访问控制与密钥链访问混合在一起,您无权访问用户密码。

如果您使用相同的属性(“查询”的内容)添加了具有SecItemAdd的资源,则可以使用SecItemCopyMatching

答案 1 :(得分:0)

“ERROR_ITEM_NOT_FOUND”=“找不到错误项目”在钥匙串中没有显示任何项目。

如上所述,我还看到了Apple示例代码“KeychainTouchID”。

iOS8的新功能使开发人员可以使用iPhone用户的密码&触摸ID进行身份验证。

首先必须“添加项目”,然后调用“查询项目”。

如果您想要更方便的解决方案,也许可以使用SmileTouchID

这是一个集成Touch ID& amp;库的库。密码安装到iOS App,甚至支持iOS7和不支持Touch ID的设备。

您只需要几行代码即可获得所需内容。

if ([SmileAuthenticator hasPassword]) {
        [SmileAuthenticator sharedInstance].securityType = INPUT_TOUCHID;
        [[SmileAuthenticator sharedInstance] presentAuthViewController];
    }