如何检查TouchID是否启用

4

有没有办法检查我的应用程序是否启用了TouchID功能?

我该如何检查我的应用程序是否启用了TouchID?

例如:

Dropbox可以启用指纹传感器。现在有没有方法可以检查基于TouchID启用的应用程序是否显示TouchID屏幕。


你使用的是Swift还是Objective-C? - Kevin Machado
3个回答

6

您不希望检查iOS版本,虽然可能有效,但这是一个不好的做法。相反,应该检查功能是否可用。请查看LAContext是否可用。

if ([LAContext class]) {
    // touch ID is available for the device
    // call canEvaluatePolicy:error to see if the user has set a fingerprint.
}

5

如果你使用Objective-C

首先,添加检查iOS版本的方法。

TouchID需要iOS8及以上版本才能正常工作。

#define SYSTEM_VERSION_EQUAL_TO(v)                  ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedSame)
#define SYSTEM_VERSION_GREATER_THAN(v)              ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedDescending)
#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v)  ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)
#define SYSTEM_VERSION_LESS_THAN(v)                 ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending)
#define SYSTEM_VERSION_LESS_THAN_OR_EQUAL_TO(v)     ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedDescending)

接下来,使用LAContext canEvaluatePolicy:error:来判断是否支持TouchID

预先检测认证策略,判断是否可以成功进行认证

- (BOOL)isTouchIDAvailable {
    if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"8.0")) {
        return [[[LAContext alloc] init] canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:nil];
    }
    return NO;
}

1
假设部署目标为iOS 8+。
    var authError : NSError?
    if LAContext().canEvaluatePolicy(LAPolicy.DeviceOwnerAuthenticationWithBiometrics, error: &authError) {
          // do your thing dependent on touch id being useable on the device
    }

如果您仍需要支持iOS7,请执行额外的步骤。
   if NSClassFromString("LAContext") != nil && LAContext().canEvaluatePolicy(LAPolicy.DeviceOwnerAuthenticationWithBiometrics, error: &authError) {

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接