FaceID 面容识别

1、指纹 和 面容 识别的使用

  • Swift

    1
    import LocalAuthentication
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    // 判读设备是否支持生物识别(指纹 和 面容)
    func isDeviceSupportBiometricsAuthentication() -> Bool {

    if isDeviceSupportTouchID() == true || isDeviceSupportFaceID() == true {
    return true
    }
    return false
    }

    // 判读设备是否支持指纹识别
    func isDeviceSupportTouchID() -> Bool {

    // 系统版本是否大于等于 8.0
    if Q_SystemVersionIsGreaterThanOrEqualTo_80() == false {
    return false
    }

    let context = LAContext()
    if context.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: nil) {

    // 是否模拟器
    if Q_DeviceIsSimulator() {
    if Q_SystemVersionIsGreaterThanOrEqualTo_110() == true {
    if Q_DeviceIsiPhoneXSeries() == true {
    return false
    }
    return true
    }
    } else {
    if #available(iOS 11.0, *) {
    if context.biometryType == .touchID {
    return true
    }
    return false
    }
    }
    return true
    }
    return false
    }

    // 判读设备是否支持面容识别
    func isDeviceSupportFaceID() -> Bool {

    // 系统版本是否大于等于 11.0
    if Q_SystemVersionIsGreaterThanOrEqualTo_110() == false {
    return false
    }

    let context = LAContext()
    if context.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: nil) ||
    context.canEvaluatePolicy(.deviceOwnerAuthentication, error: nil) {

    // 是否模拟器
    if Q_DeviceIsSimulator() {
    if Q_SystemVersionIsGreaterThanOrEqualTo_110() == true {
    if Q_DeviceIsiPhoneXSeries() == true {
    return true
    }
    return false
    }
    } else {
    if #available(iOS 11.0, *) {
    if context.biometryType == .faceID {
    return true
    }
    return false
    }
    }
    return false
    }
    return false
    }
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    var reason: String!

    if AuthenticationSecurityService().isDeviceSupportTouchID() {
    LOG("loginViaTouchID")
    reason = Q_LocalizedString("Auth.login.viaBiometricsReason.fingerprint")
    } else {
    LOG("loginViaFaceID")
    reason = Q_LocalizedString("Auth.login.viaBiometricsReason.face")
    }

    AuthenticationSecurityService()
    .evaluateBiometrics(withLocalizedReason: reason, success: { (success: Bool) in

    LOG("evaluateBiometrics success", "\(success)")

    }, failure: { (error: Error?) in

    LOG("evaluateBiometrics failure", "\(String(describing: error))")
    })
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    // 进行生物识别验证
    func evaluateBiometrics(withLocalizedReason reason: String!,
    success successBlock: ((_ success: Bool) -> Void)?,
    failure failureBlock: ((_ error: Error?) -> Void)?) {

    let policy: LAPolicy!

    let context = LAContext()

    if context.canEvaluatePolicy(.deviceOwnerAuthentication, error: nil) {
    policy = .deviceOwnerAuthentication
    } else if context.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: nil) {
    policy = .deviceOwnerAuthenticationWithBiometrics
    } else {
    if failureBlock != nil {
    failureBlock!(kLAErrorTouchIDNotAvailable as? Error)
    }
    return
    }

    context.evaluatePolicy(policy, localizedReason: reason) { (success: Bool, error: Error?) in

    if success == true {
    if successBlock != nil {
    successBlock!(true)
    }
    } else {
    if failureBlock != nil {
    failureBlock!(error)
    }
    }
    }
    }
文章目录
  1. 1. 1、指纹 和 面容 识别的使用
隐藏目录