生物认证 3.1.3

生物认证 3.1.3

Rushi Sangani 维护。




  • 作者:
  • Rushi Sangani

生物认证

使用BiometricAuthentication在您的应用程序中使用Apple FaceID或TouchID认证。它非常简单易用,根据设备处理Touch ID和Face ID认证。

注意: - Face ID认证需要在info.plist中添加用户权限。

<key>NSFaceIDUsageDescription</key>
<string>This app requires Face ID permission to authenticate using Face recognition.</string>

版本3.1的新功能

  • 更新到Swift 5.0
  • 将(ctx)类型实现为完成回调

版本2.2

  • 设置(ctx)(以秒为单位),当用户用生物特征解锁设备时自动认证。
  • 当应用回到前台或用户刚刚解锁设备且您希望用生物特征进行认证时,这非常有用。
  • 如果您不想重用最近使用的认证,请简单地跳过此步骤。
// set this before calling authenticateWithBioMetrics method (optional)
BioMetricAuthenticator.shared.allowableReuseDuration = 60

版本2.1

  • 检查iOS设备是否支持身份验证。

Alt text Alt text Alt text

功能

  • 与支持苹果Face ID(iPhone X, Xs, XR, XsMax)和其他具有Touch ID功能的设备配合使用。
  • 识别失败时的预定义错误处理。
  • 多次失败尝试后,自动使用设备密码进行认证。

要求

  • iOS 12.0+
  • Xcode 10+
  • Swift 3.0+

安装

CocoaPods

pod 'BiometricAuthentication'

Carthage

github "rushisangani/BiometricAuthentication"

使用说明

使用生物识别进行认证

BioMetricAuthenticator.authenticateWithBioMetrics(reason: "") { (result) in

    switch result {
    case .success( _):
        print("Authentication Successful")
    case .failure(let error):
        print("Authentication Failed")
    }
}
  • 当指定原因留空时,将根据设备使用默认值。例如,对于iPhone X - "确认脸部以进行身份验证"。 对于其他设备 - "确认指纹以进行身份验证"。

可通过生物识别进行身份验证

  • 您还可以在身份验证之前检查如下。这将检查设备是否支持Touch ID或Face ID身份验证,以及您的应用目前是否可以使用该功能。
if BioMetricAuthenticator.canAuthenticate() {

    BioMetricAuthenticator.authenticateWithBioMetrics(reason: "") { (result) in
        // check result -> success or failure
    }
}

检查Face ID

  • 检查设备是否支持面部识别。
if BioMetricAuthenticator.shared.faceIDAvailable() {
    // device supports face id recognition.
}

检查Touch ID

  • 检查设备是否支持Touch ID身份验证。
if BioMetricAuthenticator.shared.touchIDAvailable() {
    // device supports touch id authentication
}

备用原因

  • 当第一次尝试身份验证失败时,将显示备用原因标题。
  • 当用户点击备用按钮时,您可以提供替代身份验证选项,例如输入'用户名 & 密码'。
  • 默认原因留空,这将隐藏备用按钮。
BioMetricAuthenticator.authenticateWithBioMetrics(reason: "Biometric Authentication", fallbackTitle: "Enter Credentials") { (result) in

    switch result {
    case .success( _):
        // proceed further

    case .failure(let error):

        switch error {
        case .fallback:

            print("Authentication Failed")

            // show alternatives on fallback button clicked
            // for ex. - enter username/email and password

        default:
            break
        }
    }
}

BiometryLockedout

  • 在连续多次身份验证失败后,生物识别身份验证会被锁定。您可以通过密码身份验证来解锁它。
  • 在此处提供您自己的密码身份验证原因,如果没有提供,则将使用默认值。
BioMetricAuthenticator.authenticateWithPasscode(reason: message) { (result) in
    switch result {
    case .success( _):
        // passcode authentication success
    case .failure(let error):
        print(error.message())
    }
}

错误处理

  • 在生物特征认证失败的各种情况下。
  1. 回退
    • 当用户点击提供的回退按钮时调用。
  2. biometryNotEnrolled
    • 当设备上没有注册指纹或面部时调用。
    • 您可以在此处显示消息以注册新的面部或指纹。
    • 如果没有提供,将显示默认消息。
  3. canceledByUser
    • 当用户取消认证时调用。
  4. canceledBySystem
    • 当应用进入后台或任何其他原因时,系统取消认证时调用。
  5. passcodeNotSet
    • 当设备密码未设置并尝试使用生物特征认证时调用。
    • 我们可以在此处请求用户通过打开设置应用程序来设置设备密码。
  6. failed
    • 当用户多次失败尝试时调用。
    • 您可以在此处向用户显示错误消息。
    • 如果没有提供,可以显示默认消息。
  7. biometryLockedout
    • 当使用生物特征认证失败尝试超过5次时调用。这将锁定您的生物特征系统。
    • 当出现此错误时,您将限制用户。
    • 或者您可以让用户输入设备密码来解锁生物特征。
  8. biometryNotAvailable
    • 当设备不支持面部ID或Touch ID认证时调用。

示例

BioMetricAuthenticator.authenticateWithBioMetrics(reason: "") { [weak self] (result) in

    switch result {
    case .success( _):

        // authentication successful
        self?.showLoginSucessAlert()

    case .failure(let error):

        switch error {

        // device does not support biometric (face id or touch id) authentication
        case .biometryNotAvailable:
            self?.showErrorAlert(message: error.message())

        // No biometry enrolled in this device, ask user to register fingerprint or face
        case .biometryNotEnrolled:
            self?.showGotoSettingsAlert(message: error.message())

        // show alternatives on fallback button clicked
        case .fallback:
            self?.txtUsername.becomeFirstResponder() // enter username password manually

        // Biometry is locked out now, because there were too many failed attempts.
        // Need to enter device passcode to unlock.
        case .biometryLockedout:
            self?.showPasscodeAuthentication(message: error.message())

        // do nothing on canceled by system or user
        case .canceledBySystem, .canceledByUser:
            break

        // show error for any other reason
        default:
            self?.showErrorAlert(message: error.message())
        }
    }
}

查看更多详情,请见 示例

许可证

BiometricAuthentication是在MIT许可证下发布的。请见 LICENSE 了解详情。