我知道iPhone 5S上的Touch ID不能通过SDK被其他应用程序使用,但我想看看是否有可能让应用程序至少查看设备上是否启用了Touch ID。这可以作为应用程序的额外安全因素,以查看Touch ID是否已在iPhone 5S上启用。我知道MDM产品可以做到这一点,但应用程序需要使用特殊的API来确定此信息吗?
LAContext
的canEvaluatePolicy:error:
。以下情况下它将返回NO:
更多信息请参见:本地身份验证框架参考
- (BOOL) isTouchIDAvailable {
LAContext *myContext = [[LAContext alloc] init];
NSError *authError = nil;
if (![myContext canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&authError]) {
NSLog(@"%@", [authError localizedDescription]);
return NO;
}
return YES;
}
由于TouchID仅出现在iPhone 5s中,因此您可以使用以下代码来确定硬件型号:
- (NSString *) checkiPhone5s {
// Gets a string with the device model
size_t size;
sysctlbyname("hw.machine", NULL, &size, NULL, 0);
char *machine = malloc(size);
sysctlbyname("hw.machine", machine, &size, NULL, 0);
NSString *platform = [NSString stringWithCString:machine encoding:NSUTF8StringEncoding];
free(machine);
if ([platform isEqualToString:@"iPhone6,1"]) {
// it is iPhone 5s !!!
}
}
iOS < 8.0:
self.touchIdAvailable = NO;
Then check if device is touchId available.
- (BOOL)canEvaluatePolicy:(LAPolicy)policy error:(NSError **)error
if ("canEvaluatePolicy return YES"):{
self.touchIdAvailable = YES;
self.osTouchIdStatus = TouchIdSetOK;
}
check why this device is not touchID enable in error block
case LAErrorPasscodeNotSet:
case LAErrorTouchIDNotEnrolled:
self.touchIdAvailable = YES;
self.osTouchIdStatus = TouchIdNotSet;
break;
default:
self.touchIdAvailable = NO;
self.osTouchIdStatus = TouchIdNotSupported;
break;
开发者文档中似乎没有提到如何检测设备是否支持Touch ID。确实有一个私有API可以提供这些信息,但使用它可能会导致苹果不批准您的应用。