2014-11-25 82 views

回答

6

试试这个:

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

或类似@rckoenes建议:

- (BOOL)canAuthenticateByTouchId { 
    if ([LAContext class]) { 
     return [[[LAContext alloc] init] canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:nil]; 
    } 
    return NO; 
} 

UPDATE

我忘了,看看这个:How can we programmatically detect which iOS version is device running on?定义SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO

+1

更好地利用'如果([LAContext类]){'它代替检查的系统版本。既然你有兴趣,如果类是可用的,而不是真的在系统版本。您应该避免检查系统版本,请检查基础版本或仅在类或方法可用时。 – rckoenes 2014-11-25 10:21:04

+0

你说得对,我更新了答案。谢谢。 – Mateusz 2014-11-25 10:42:12

+0

只需添加:这不会检测设备是否具有TouchID功能。它仅检查设备设置中是否启用了TouchID。下面的Siti Kamaludin的答案更好。 – GeneCode 2017-07-05 03:20:21

5

你应该考虑触摸身份验证所需的框架。

而参数LAErrorTouchIDNotAvailable会显示是设计支持这个功能。

代码片段:

- (IBAction)authenticateButtonTapped:(id)sender { 
    LAContext *context = [[LAContext alloc] init]; 

    NSError *error = nil; 

    if ([context canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&error]) { 
     // Authenticate User 

    } else { 

     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" 
                 message:@"Your device cannot authenticate using TouchID." 
                 delegate:nil 
               cancelButtonTitle:@"Ok" 
               otherButtonTitles:nil]; 
     [alert show]; 

    } 
} 

尼斯教程来学习这个功能here

0

此功能将有助于利用 -

-(BOOL)doesThisDeviceSupportTouchIdForLocalAuthentication{ 

    //Checking for 64 bit (armv7s) architecture before including the LAContext as it would give error otherwise. 
    #if TARGET_CPU_ARM64 
    LAContext *context = [[LAContext alloc] init]; 

    NSError *error = nil; 

    if ([context canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&error]){ 
     return YES; 
    } 
    return NO; 
    #endif 

    return NO; 
} 
3

您可以检查使用CanEvaluatePolicy错误。如果错误代码是-6,则表示该设备上没有物理Touch ID。你可以从错误描述中知道,它说

生物特征不适用于此设备。

下面是如果你使用C#Xamarin代码:

var context = new LAContext(); 
     NSError AuthError; 
     if (!context.CanEvaluatePolicy(LAPolicy.DeviceOwnerAuthenticationWithBiometrics, out AuthError)) 
     { 
      if (AuthError != null && AuthError.Code == -6) 
      { 
       var alert = new UIAlertView ("Error", "TouchID not available", null, "BOOO!", null); 
       alert.Show(); 
      } 
     } 
0

目标C

@import LocalAuthentication; 
// Get the local authentication context: 
LAContext *context = [[LAContext alloc] init]; 
// Test if fingerprint authentication is available on the device and a fingerprint has been enrolled. 
if ([context canEvaluatePolicy: LAPolicyDeviceOwnerAuthenticationWithBiometrics error:nil]) 
{ 
    NSLog(@"Fingerprint authentication available."); 
} 
+0

欢迎来到Stack Overflow! [如何回答](http://stackoverflow.com/help/how-to-answer)可以帮助你写出将被接受和upvoted的答案。在你的情况下,代码应该放在代码块中。 – zhon 2016-09-04 19:45:15