タッチIDの成功/失敗に基づいて真または偽を返そうとしています。しかし、成功すると条件が呼び出されるまでに、関数は偽の値で終了します。私は、iOS開発には新しいです。私は何らかの補完ハンドラを使用するはずだと信じていますが、この具体的なケースではどのようにすればよいかわかりません。タッチID成功時に変数を設定する
func authenticateUser(reasonString: String) -> Bool {
// Get the local authentication context.
let context = LAContext()
// Declare a NSError variable.
var error: NSError?
// Current authorization status of user
var isAuthorized = false
// Check if the device can evaluate the policy.
if context.canEvaluatePolicy(LAPolicy.DeviceOwnerAuthenticationWithBiometrics, error: &error) {
[context .evaluatePolicy(LAPolicy.DeviceOwnerAuthenticationWithBiometrics, localizedReason: reasonString, reply: { (success: Bool, evalPolicyError: NSError?) -> Void in
if success {
isAuthorized = true
}
else{
// If authentication failed then show a message to the console with a short description.
// In case that the error is a user fallback, then show the password alert view.
print(evalPolicyError?.localizedDescription)
switch evalPolicyError!.code {
case LAError.SystemCancel.rawValue:
print("Authentication was cancelled by the system")
case LAError.UserCancel.rawValue:
print("Authentication was cancelled by the user")
case LAError.UserFallback.rawValue:
print("User selected to enter custom password")
default:
print("Authentication failed")
}
}
})]
}
else{
// If the security policy cannot be evaluated then show a short message depending on the error.
switch error!.code{
case LAError.TouchIDNotEnrolled.rawValue:
print("TouchID is not enrolled")
case LAError.PasscodeNotSet.rawValue:
print("A passcode has not been set")
default:
// The LAError.TouchIDNotAvailable case.
print("TouchID not available")
}
// Optionally the error description can be displayed on the console.
print(error?.localizedDescription)
}
return isAuthorized
}
この関数は、touchID部分が実行される前であっても、falseを返します。誰かが案内してくれますか?