简体   繁体   English

如何自定义 AWS Amplify iOS 身份验证警报?

[英]How to customize AWS Amplify iOS Authentication alerts?

As someone new to iOS Swift development, I was wondering if someone could help me understand the proper way to customize client-side alert messages for authentication errors such as incorrect username and password.作为 iOS Swift 开发的新手,我想知道是否有人可以帮助我理解为身份验证错误(例如不正确的用户名和密码)定制客户端警报消息的正确方法。 Below is the default alert message, which has the exception name as the title, which is not good user experience.下面是默认的告警信息,标题是异常名称,用户体验不好。 How would I go about displaying an alert popup with another title and message?我将如何显示带有另一个标题和消息的警报弹出窗口?

AWS 身份验证错误警报

I think you are looking for UIAlertController .我认为您正在寻找UIAlertController

Swift sample :斯威夫特示例:

let alert = UIAlertController(title: "Hello!", message: "Message", preferredStyle: UIAlertController.Style.alert)
let alertAction = UIAlertAction(title: "OK!", style: UIAlertAction.Style.default)
        {
            (UIAlertAction) -> Void in
        }
        alert.addAction(alertAction)
        present(alert, animated: true)
        {
            () -> Void in
        }

Hope it helps!希望能帮助到你!

Handle your error response to define custom title & message like below.处理您的错误响应以定义如下所示的自定义标题和消息。

import AWSCognitoIdentityProvider

extension Error {
    var customizedDescription: String {
        let nsError = self as NSError
        if nsError.domain == AWSCognitoIdentityProviderErrorDomain, let code = AWSCognitoIdentityProviderErrorType(rawValue: nsError.code) {
            switch code {
            case .expiredCode: return "Verification code has expired. Please try again"
            case .codeMismatch: return  "Incorrect verification code. Please enter the correct code to continue."
            case .notAuthorized: return "Authentication failed. Please enter the correct credentials to continue."
            case .usernameExists: return "Username already exists."
            case .invalidPassword: return "Invalid password. Please try again."
            case .userNotConfirmed: return "User not confirmed. Please verify your account."
            default: return nsError.userInfo["message"] as? String ?? "AWS cognito failed with error code: \(nsError.code)"
            }
        }
        return localizedDescription
    }
}

https://gist.github.com/Catherine-K-George/11a94271bd0cff282e019cd2613d089b https://gist.github.com/Catherine-K-George/11a94271bd0cff282e019cd2613d089b

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM