2017-10-23 23 views

Trả lời

9

Với Xcode 9, hãy xem LocalAuthentication -> LAContext -> LABiometryType.

LABiometryType là một enum với các giá trị như trong hình ảnh gắn liền

enter image description here

Bạn có thể kiểm tra kiểu xác thực được hỗ trợ bởi thiết bị giữa ID Touch và FaceID hay không.

Edit:

của Apple đã cập nhật các giá trị cho điều này enum LABiometryType. không được sử dụng bây giờ.

enter image description here

+0

Trong vừa phát hành Xcode 9.2 beta, với iOS 11.2, giá trị 'enum LABiometryType' đã thay đổi cho' faceID' và 'touchID'. – tfrank377

+1

@ tfrank377 Cảm ơn bạn đã nhắc tôi. Tôi đã cập nhật câu trả lời. – Surjeet

9

Tôi đã đấu tranh nhận này để làm việc và thấy rằng tôi cần thiết để sử dụng một trường hợp duy nhất của LAContext và cần thiết để gọi LAContextInstance .canEvaluatePolicy (.deviceOwnerAuthenticationWithBiometrics, lỗi: không) trước khi lấy biometryType. Đây là mã cuối cùng của tôi với sự hỗ trợ cho các phiên bản iOS cũ hơn:

static func biometricType() -> BiometricType { 
    let authContext = LAContext() 
    if #available(iOS 11, *) { 
     let _ = authContext.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: nil) 
     switch(authContext.biometryType) { 
     case .none: 
      return .none 
     case .touchID: 
      return .touch 
     case .faceID: 
      return .face 
     } 
    } else { 
     return authContext.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: nil) ? .touch : .none 
    } 
} 

enum BiometricType { 
    case none 
    case touch 
    case face 
} 
+1

Cảm ơn vì điều này. Chính xác những gì tôi đang tìm kiếm :) – Sethmr

+3

Tài liệu cho các trạng thái 'biometryType':" Thuộc tính này được thiết lập chỉ khi canEvaluatePolicy thành công cho một chính sách sinh trắc học ". Vì vậy, nếu canEvaluatePolicy không thành công (ví dụ: touchid/faceid bị hủy kích hoạt ở tất cả hoặc cho mỗi ứng dụng) 'biometricType()' trả về '.none'. Vì vậy, 'biometricType()' kiểm tra không có sẵn phần cứng nhưng nếu phần cứng có thể được truy cập bởi ứng dụng. –

+0

@ValeriyVan Bạn đã tìm ra cách kiểm tra tính khả dụng của phần cứng trên thiết bị chưa? Có vẻ như mọi người đều đưa biometricType làm câu trả lời, nhưng, như bạn đã nói, đó thực sự là câu trả lời sai nếu bạn chỉ đang cố gắng trình bày cho người dùng nút có nội dung "Face ID" hoặc "Touch ID" để người dùng có thể ủy quyền một hoặc khác * khi thiết bị không thể được xác thực bằng sinh trắc học *. – SAHM

3

Đây là một cách khác thông qua thuộc tính (ví dụ, trong trường hợp truy cập của bạn).

import LocalAuthentication 


enum BiometricType { 
    case none 
    case touchID 
    case faceID 
} 

var biometricType: BiometricType { 
    get { 
     let context = LAContext() 
     var error: NSError? 

     guard context.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: &error) else { 
      print(error?.localizedDescription ?? "") 
      return .none 
     } 

     if #available(iOS 11.0, *) { 
      switch context.biometryType { 
      case .none: 
       return .none 
      case .typeTouchID: 
       return .touchID 
      case .typeFaceID: 
       return .faceID 
      } 
     } else { 
      return .touchID 
     } 
    } 
} 
0

Objective C :)

/** Only interesting devices are enumerated here. To change view constraints depending 
on screen height. Or the top notch for iPhone X 
*/ 
typedef NS_ENUM(NSUInteger, BPDeviceType) { 
    BPDeviceTypeUnknown, 
    BPDeviceTypeiPhone4, 
    BPDeviceTypeiPhone5, 
    BPDeviceTypeiPhone6, 
    BPDeviceTypeiPhone6Plus, 
    BPDeviceTypeiPhone7, 
    BPDeviceTypeiPhone7Plus, 
    BPDeviceTypeiPhoneX, 
    BPDeviceTypeiPad 
}; 

+ (BPDeviceType)getDeviceType { 
    double screenHeight = [[UIScreen mainScreen] bounds].size.height; 
    if(UI_USER_INTERFACE_IDIOM()==UIUserInterfaceIdiomPad) 
    { 
     return BPDeviceTypeiPad; 

    } else if (UI_USER_INTERFACE_IDIOM()== UIUserInterfaceIdiomPhone) 
    { 
     if (@available(iOS 11, *)) { 
      UIEdgeInsets insets = [UIApplication sharedApplication].delegate.window.safeAreaInsets; 
      if (insets.top > 0) { 
       return BPDeviceTypeiPhoneX; 
      } 
     } 

     if(screenHeight == 480) { 
      return BPDeviceTypeiPhone4; 
     } else if (screenHeight == 568) { 
      return BPDeviceTypeiPhone5; 
     } else if (screenHeight == 667) { 
      return BPDeviceTypeiPhone6; 
     } else if (screenHeight == 736) { 
      return BPDeviceTypeiPhone6Plus; 
     } 
    } 
    return BPDeviceTypeUnknown; 
} 

+ (BOOL) isBiometricIDAvailable { 
    if (![LAContext class]) return NO; 

    LAContext *myContext = [[LAContext alloc] init]; 
    NSError *authError = nil; 
    if (![myContext canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&authError]) { 
     NSLog(@"%@", [authError localizedDescription]); 
     return NO; 
    } 
    return YES; 
} 

+ (BOOL) isTouchIDAvailable { 
    if (![LAContext class]) return NO; 

    LAContext *myContext = [[LAContext alloc] init]; 
    NSError *authError = nil; 
    if (![myContext canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&authError]) { 
     NSLog(@"%@", [authError localizedDescription]); 
     return NO; 
     // if (authError.code == LAErrorTouchIDNotAvailable) {} 
    } 

    if (@available(iOS 11.0, *)) { 
     if (myContext.biometryType == LABiometryTypeTouchID){ 
      return YES; 
     } else { 
      return NO; 
     } 
    } else { 
     return YES; 
    } 
} 

+ (BOOL) supportFaceID { 
    return [BPDeviceInfo getDeviceType] == BPDeviceTypeiPhoneX; 
} 

+ (BOOL) isFaceIDAvailable { 
    if (![LAContext class]) return NO; 

    LAContext *myContext = [[LAContext alloc] init]; 
    NSError *authError = nil; 
    if (![myContext canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&authError]) { 
     NSLog(@"%@", [authError localizedDescription]); 
     return NO; 
    } 

    if (@available(iOS 11.0, *)) { 
     if (myContext.biometryType == LABiometryTypeFaceID){ 
      return YES; 
     } else { 
      return NO; 
     } 
    } else { 
     return NO; 
    } 
} 
Các vấn đề liên quan