如何检查 iOS 中没有为 Touch ID 添加指纹

how to check No Fingerprints added for Touch ID in iOS

我正在将 Touch ID 访问集成到我的一个应用程序中。我已经成功整合了它。这是代码:

    dispatch_queue_t highPriorityQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0);
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.75 * NSEC_PER_SEC), highPriorityQueue, ^{

        dispatch_async(dispatch_get_main_queue(), ^{

            LAContext *context = [[LAContext alloc] init];
            isTouchExists = [context canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:nil];
            if (isTouchExists) {
                NSString * keychainItemIdentifier;

                NSString * keychainItemServiceName;
                keychainItemIdentifier = @"fingerprintKeychainEntry";
                keychainItemServiceName = [[NSBundle mainBundle] bundleIdentifier];
                NSData * pwData = [@"the password itself does not matter" dataUsingEncoding:NSUTF8StringEncoding];
                NSMutableDictionary * attributes = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
                                                    (__bridge id)(kSecClassGenericPassword), kSecClass,
                                                    keychainItemIdentifier, kSecAttrAccount,
                                                    keychainItemServiceName, kSecAttrService, nil];
                CFErrorRef accessControlError = NULL;
                SecAccessControlRef accessControlRef = SecAccessControlCreateWithFlags(
                                                                                       kCFAllocatorDefault,
                                                                                       kSecAttrAccessibleWhenUnlockedThisDeviceOnly,
                                                                                       kSecAccessControlUserPresence,
                                                                                       &accessControlError);
                if (accessControlRef == NULL || accessControlError != NULL)
                {
                    NSLog(@"Cannot create SecAccessControlRef to store a password with identifier “%@” in the key chain: %@.", keychainItemIdentifier, accessControlError);
                }

                attributes[(__bridge id)kSecAttrAccessControl] = (__bridge id)accessControlRef;
                attributes[(__bridge id)kSecUseNoAuthenticationUI] = @YES;
                attributes[(__bridge id)kSecValueData] = pwData;

                CFTypeRef result;
                OSStatus osStatus = SecItemAdd((__bridge CFDictionaryRef)attributes, &result);

                if (osStatus != noErr)
                {
                    NSError * error = [[NSError alloc] initWithDomain:NSOSStatusErrorDomain code:osStatus userInfo:nil];
                    NSLog(@"Adding generic password with identifier “%@” to keychain failed with OSError %d: %@.", keychainItemIdentifier, (int)osStatus, error);
                }
                //other my code for success
           }
        });
    });

现在,如果我从 iPhone 中的设置中删除所有指纹,此代码将起作用并要求输入密码。所以我的问题是:

我怎么知道Touch ID没有加指纹?

我不想显示 iOS 设备密码屏幕,因为我已经为我的应用安全构建了密码屏幕。那么是否有任何选项可以检查设备至少有一个指纹可用于 Touch ID 访问?

提前致谢。

======== 编辑 1 ===========

它也在我这边工作。问题是我每次要求 Touch ID 时都需要检查它。每次我想在应用程序中使用 Touch ID 访问时,我都需要在 viewWillAppearapplicationDidBecomeActive 中获取状态,因为我要移除手指 运行 次,它可能不会反映在我的代码中所以我每次都需要取。

如果没有注册指纹,canEvaluatePolicy 应该 return false。

来源:https://developer.apple.com/documentation/localauthentication/lacontext/1514149-canevaluatepolicy?language=objc

canEvaluatePolicy:error: 将是错误:LAErrorTouchIDNotEnrolled

Authentication could not start because Touch ID has no enrolled fingers.

APPLE DOC Ref.

尝试:

LAContext *context = [[LAContext alloc] init];
    NSError *error;
    if ([context canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&error]) {
        [context evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason:@"My Reason" reply:^(BOOL success, NSError * _Nullable error) {

        }];
    }else{
        if (error.code == LAErrorTouchIDNotEnrolled) {
            NSLog(@"Error: %@", error.localizedDescription);
        }
    }