【问题标题】:iOS, using biometric authentication only for a subset of an apps functionalityiOS,仅对应用程序功能的子集使用生物特征身份验证
【发布时间】:2018-11-20 19:33:09
【问题描述】:

我正在开发一个应用程序,其中用户只需要对某些功能进行身份验证,但他们应该对其余功能保持“匿名”。当用户尝试访问特定功能时,有没有办法在应用程序中实现生物特征认证?

例如,让我们说,要参与应用程序内的公开讨论论坛,他们不需要进行身份验证,但一旦他们尝试发送直接消息,他们将需要使用 Touch ID 或 Face ID 进行身份验证。可以这样做,还是只能在用户打开应用时使用生物特征认证?

【问题讨论】:

    标签: ios swift


    【解决方案1】:

    是的,您可以在任何您想使用的地方使用它。只需请求身份验证,然后在身份验证成功完成后,显示另一个 ViewController。

    【讨论】:

      【解决方案2】:

      您可以在任何地方进行生物特征验证,您可以尝试以下示例:

      func authenticationWithTouchID(completion: (Bool) -> ()) {
          let localAuthenticationContext = LAContext()
          localAuthenticationContext.localizedFallbackTitle = "Use Passcode"
      
          var authError: NSError?
          let reasonString = "To access the secure data"
      
          if localAuthenticationContext.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: &authError) {
      
              localAuthenticationContext.evaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, localizedReason: reasonString) { success, evaluateError in
                  if success {
                      completion(true)
                  } else {
                      //TODO: User did not authenticate successfully, look at the error and take appropriate action
                      guard let error = evaluateError else { return }
                      print(self.evaluateAuthenticationPolicyMessageForLA(errorCode: error._code))
                      //TODO: If you have choosen the 'Fallback authentication mechanism selected' (LAError.userFallback). Handle gracefully
                      completion(false)
                  }
              }
          } else {
              guard let error = authError else { return }
              //TODO: Show appropriate alert if biometry/TouchID/FaceID is lockout or not enrolled
              print(self.evaluateAuthenticationPolicyMessageForLA(errorCode: error.code))
          }
      }
      
      func evaluatePolicyFailErrorMessageForLA(errorCode: Int) -> String {
          var message = ""
          if #available(iOS 11.0, macOS 10.13, *) {
              switch errorCode {
                  case LAError.biometryNotAvailable.rawValue:
                      message = "Authentication could not start because the device does not support biometric authentication."
      
                  case LAError.biometryLockout.rawValue:
                      message = "Authentication could not continue because the user has been locked out of biometric authentication, due to failing authentication too many times."
      
                  case LAError.biometryNotEnrolled.rawValue:
                      message = "Authentication could not start because the user has not enrolled in biometric authentication."
      
                  default:
                      message = "Did not find error code on LAError object"
              }
          } else {
              switch errorCode {
                  case LAError.touchIDLockout.rawValue:
                      message = "Too many failed attempts."
      
                  case LAError.touchIDNotAvailable.rawValue:
                      message = "TouchID is not available on the device"
      
                  case LAError.touchIDNotEnrolled.rawValue:
                      message = "TouchID is not enrolled on the device"
      
                  default:
                      message = "Did not find error code on LAError object"
              }
          }
          return message;
      }
      
      func evaluateAuthenticationPolicyMessageForLA(errorCode: Int) -> String {
          var message = ""
          switch errorCode {
      
          case LAError.authenticationFailed.rawValue:
              message = "The user failed to provide valid credentials"
      
          case LAError.appCancel.rawValue:
              message = "Authentication was cancelled by application"
      
          case LAError.invalidContext.rawValue:
              message = "The context is invalid"
      
          case LAError.notInteractive.rawValue:
              message = "Not interactive"
      
          case LAError.passcodeNotSet.rawValue:
              message = "Passcode is not set on the device"
      
          case LAError.systemCancel.rawValue:
              message = "Authentication was cancelled by the system"
      
          case LAError.userCancel.rawValue:
              message = "The user did cancel"
      
          case LAError.userFallback.rawValue:
              message = "The user chose to use the fallback"
      
          default:
              message = evaluatePolicyFailErrorMessageForLA(errorCode: errorCode)
          }
          return message
      }
      

      现在您可以在任何地方使用此功能

      authenticationWithTouchID(completion: {(success) in
          if success {
               //TODO: User authenticated successfully, take appropriate action     
          } else {
               //TODO: User authenticateion failed, take appropriate action
          }
      })
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-08-16
        • 2015-06-10
        • 2018-05-18
        • 2015-12-21
        • 2011-02-12
        • 1970-01-01
        • 2020-09-18
        相关资源
        最近更新 更多