See if Touch ID is enabled on iPhone 5s

2019-02-14 19:56发布

问题:

I know the Touch ID on the iPhone 5S cannot be consumed by any other apps through the SDK, but I wanted to see if it was possible for an app to at least see if the Touch ID is enabled on the device. This can act as an additional security factor for an app to see if the Touch ID has been enabled on an iPhone 5S. I know MDM products can do this, is there a special API an app would need to use to determine this information?

回答1:

The method you are looking for is LAContext's canEvaluatePolicy:error:. It will return NO in the following cases :

  • Touch ID is not available on the device.
  • A passcode is not set on the device.
  • Touch ID has no enrolled fingers.

More infos here : Local Authentication Framework Reference

- (BOOL) isTouchIDAvailable {
    LAContext *myContext = [[LAContext alloc] init];
    NSError *authError = nil;

    if (![myContext canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&authError]) {

        NSLog(@"%@", [authError localizedDescription]);
        return NO;
    }
    return YES;
}


回答2:

Since TouchID is only appeared in iPhone 5s, you can use the following code to determine hardware model:

- (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 !!!
  }
}

Note that iPhone 5s may have new model, so that the platform string may be "iPhone6,2" , "iPhone6,3" in the future.

Alternatively, UIDeviceHardware class may provide useful information if that class is updated for iPhone 5s. Currently it shows "Unknown iPhone" for iPhone 5.



回答3:

I check this by below stages.

  1. iOS < 8.0:

    self.touchIdAvailable = NO;
    
  2. Then check if device is touchId available.

    - (BOOL)canEvaluatePolicy:(LAPolicy)policy error:(NSError **)error
    
        if ("canEvaluatePolicy return YES"):{
            self.touchIdAvailable = YES;
            self.osTouchIdStatus = TouchIdSetOK;
        }
    
  3. 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;
    


回答4:

There doesn't appear to be anything mentioned in the developer documentation about detecting if Touch ID is present on the device. There most definitely is a private API that can give you that information but using it would be grounds for Apple not approving your app.