2014-11-25 84 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