简体   繁体   English

密码自动填充保存密码 SwiftUI

[英]Password AutoFill save password SwiftUI

Good day.再会。

I'm trying to implement a login page in SwiftUI, but I'm unable to figure out how to make the application prompt the user to save their credentials after a successful login.我正在尝试在 SwiftUI 中实现登录页面,但我无法弄清楚如何让应用程序在成功登录后提示用户保存其凭据。

This is the code for the input fields.这是输入字段的代码。

        TextField("Phone number", text: $phoneNumber)
            .keyboardType(.numbersAndPunctuation)
            .textContentType(.username)

        SecureField("Password", text: $password)
            .keyboardType(.asciiCapable)
            .textContentType(.password)

On success, an environment variable changes, and the parent component will stop rendering the login page.成功后,环境变量会发生变化,并且父组件将停止呈现登录页面。

I've enabled the "Autofill Credential Provider" entitlement.我已启用“自动填充凭据提供程序”权利。

What I am trying to achieve is use of the native "password manager" in iOS, https://developer.apple.com/documentation/security/password_autofill .我想要实现的是在 iOS、 https://developer.apple.com/documentation/security/password_autofill中使用本机“密码管理器”。

iOS上的密码自动填充提示

When the user would submit their credentials and successfully authenticate, they should be shown a prompt like the one above, asking them if they want to save the password they used to sign in.当用户提交他们的凭据并成功进行身份验证时,他们应该会看到一个类似上面的提示,询问他们是否要保存他们用于登录的密码。

The problem with password autofill is not the UIKit/SwiftUI implementation.密码自动填充的问题不在于 UIKit/SwiftUI 实现。 To get support in your app, the most important things to get right are:要在您的应用程序中获得支持,最重要的事情是:

  1. Add "Associated Domains" capability to your app and set "Domains" to webcredentials:example.com (replacing example.com with a domain you actually own and have access to.).将“关联域”功能添加到您的应用程序并将“域”设置为webcredentials:example.com (将example.com替换为您实际拥有并有权访问的域。)。

  2. On your website (accessible under the given domain) place a file named apple-app-site-association into your public accessible root directory in a folder .well-known .在您的网站上(可在给定域下访问)将一个名为apple-app-site-association的文件放入您的公共可访问根目录中的.well-known文件夹中。
    You can test access using the command curl https://www.example.com/.well-known/apple-app-site-association .您可以使用命令curl https://www.example.com/.well-known/apple-app-site-association测试访问。 (Yes, HTTPS must be enabled and have a valid certificate!) (是的,必须启用 HTTPS 并拥有有效证书!)

  3. The content of the apple-app-site-association must reference your Apple Developer Team ID and the bundle identifier of your app as follows: (Team ID is "A1BC23REUG" and bundle identifier is "com.example.signin-playground") apple-app-site-association的内容必须引用您的 Apple Developer Team ID 和应用程序的捆绑标识符,如下所示:(团队 ID 为“A1BC23REUG”,捆绑标识符为“com.example.signin-playground”)

{
  "webcredentials": {
    "apps": [ 
      "A1BC23REUG.com.example.signin-playground"
    ]
  }
}
  1. Mark the input fields in SwiftUI using the appropriate textContentType modifier.使用适当的textContentType修饰符标记 SwiftUI 中的输入字段。

I have written a small example in this gist which I successfully tested.我在 这个要点中写了一个小例子,我成功地测试了它。

If autofill is not working (eg you get only an unspecific password completion dialog), you normally have to check the bundle/team identifier and the in the apple-app-site-association file.如果自动填充不起作用(例如,您只看到一个不特定的密码完成对话框),您通常必须检查捆绑/团队标识符和apple-app-site-association文件中的 。 If you changed something in the file recently, it's possible that the content is cached either on your device (so you can delete the app and reinstall it again) or it is cached in Apples CDN network (in which case you can activate an alternate mode as documented here如果您最近更改了文件中的某些内容,则内容可能会缓存在您的设备上(因此您可以删除应用程序并重新安装)或缓存在 Apples CDN 网络中(在这种情况下,您可以激活备用模式如此处所述

If the "Would you like to save this password" dialog is not appearing, you either didn't correctly setup "Associated Domains" or the iOS heuristic did not detect a relevant change in your screens.如果“您想保存此密码”对话框未出现,则您没有正确设置“关联域”或 iOS 启发式未检测到屏幕中的相关更改。

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

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