【问题标题】:save user credentials for swiftUI - password ActionSheet call保存 swiftUI 的用户凭据 - 密码 ActionSheet 调用
【发布时间】:2021-06-30 12:03:51
【问题描述】:

我有一个 SwiftUI 应用程序,其中包含两个文本字段和一个用于登录的登录按钮:

    VStack {
            ZStack {
                        Liquid()
                            .frame(width: 240, height: 240)
                            .foregroundColor(.blue)
                            .opacity(0.3)


                        Liquid()
                            .frame(width: 220, height: 220)
                            .foregroundColor(.blue)
                            .opacity(0.6)

                        Liquid(samples: 2)
                            .frame(width: 200, height: 200)
                            .foregroundColor(.blue)
                        
                        Text("Refinance").font(.largeTitle).foregroundColor(.white)
                    }
            VStack {
                HStack{
                    Image(systemName: "envelope")
                        .foregroundColor(.gray)
                    TextField("Email", text: $email)
                        .textContentType(.username)
                }
                .padding()
                .background(RoundedRectangle(cornerRadius: 8).fill(textFieldColor))
                .padding(.vertical, 10)
                HStack{
                    Image(systemName: "lock")
                        .foregroundColor(.gray)
                    SecureField("Password", text: $password)
                        .textContentType(.password)
                }
                .padding()
                .background(RoundedRectangle(cornerRadius: 8).fill(textFieldColor))
                .padding(.vertical, 10)
                Button(action: { login() }) {
                    Text("Sign in")
                        .font(.headline)
                        .foregroundColor(.white)
                        .padding()
                        .frame(width: 180, height: 40)
                        .background(Color.blue)
                        .cornerRadius(15.0)
                        
                }

您可以在代码中看到我为每一个都分配了 textContextType。

我已经创建了 apple-app-site-association 文件,并将它上传到我的服务器,(并使用 branch.io AASA 检查)以确保它被正确调用。 我还将此 Web 凭据域添加到我的应用权利中。

当我构建我的应用程序时,我可以选择从钥匙串中检索用户名和密码,但是我无法保存密码。如果我卸载应用程序并首次启动,系统不会提示我保存密码。

我的登录功能将我的用户名和密码发送到 firebase 登录,因此当前没有代码通知钥匙串在按下登录按钮时执行某些操作。

我找不到任何与使用 SwiftUI 执行此操作相关的内容。然而,迅速找到了这个文档: https://developer.apple.com/documentation/security/keychain_services/keychain_items/adding_a_password_to_the_keychain

有人可以确认此方法是否最适合使用 swiftUI 保存用户凭据,或者是否还有其他我应该参考的内容。

【问题讨论】:

  • Keychain 是 Apple 用于安全存储凭据的解决方案。根据文档。
  • 我想让我感到困惑的是我不明白 webcredentials 和钥匙串之间的联系。使用钥匙串保存用户凭据是一回事,但我看不出钥匙串如何知道密码是用于此应用程序的。还是在保存钥匙串项时与后台域相关联?
  • 你必须把这一切都说出来,然后去做。看文档,看有没有wwdc视频,找教程。

标签: authentication swiftui save credentials


【解决方案1】:

您可以使用AppStorage 来保存用户凭据、用户首选项、用户设置等小数据。

它非常易于使用。这是演示:

@AppStorage("username") var username: String = "dc7"

【讨论】:

  • UserDefaults/AppStorage 被认为是最不安全的存储形式之一。你不应该在那里存储密码。默认系统的目的是让应用程序自定义其行为以匹配用户的偏好。不是凭据。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-19
  • 2023-04-04
  • 2015-07-19
  • 2014-11-04
  • 1970-01-01
  • 2021-06-12
相关资源
最近更新 更多