要回答这两个问题,是的,您可以使用 Touch ID 提高支付的安全性,这是我在 Apple 的示例代码中实现它的方法,我在 SendPaymentIntentHandler.swift 中添加了以下函数:
func authenticate(successAuth: @escaping () -> Void, failure: @escaping (NSError?) -> Void) {
// 1. Create a authentication context
let authenticationContext = LAContext()
var error:NSError?
guard authenticationContext.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: &error) else {
failure(error)
return
}
// 3. Check the fingerprint
authenticationContext.evaluatePolicy(
.deviceOwnerAuthenticationWithBiometrics,
localizedReason: "Unlock to send the money",
reply: { [unowned self] (success, error) -> Void in
if( success ) {
successAuth()
}else {
let message = self.errorMessageForLAErrorCode(errorCode: (error! as NSError).code)
print(message)
failure(error! as NSError)
}
})
}
func errorMessageForLAErrorCode( errorCode:Int ) -> String{
var message = ""
switch errorCode {
case LAError.appCancel.rawValue:
message = "Authentication was cancelled by application"
case LAError.authenticationFailed.rawValue:
message = "The user failed to provide valid credentials"
case LAError.invalidContext.rawValue:
message = "The context is invalid"
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.touchIDLockout.rawValue:
message = "Too many failed attempts."
case LAError.touchIDNotAvailable.rawValue:
message = "TouchID is not available on the device"
case LAError.userCancel.rawValue:
message = "The user did cancel"
case LAError.userFallback.rawValue:
message = "The user chose to use the fallback"
default:
message = "Did not find error code on LAError object"
}
return message
}
然后在handle方法中调用函数authenticate,结果是我的app在确认支付后要求进行Touch ID认证,然后用户自己认证后,支付成功。