Password AutoFill save password SwiftUI
Asked Answered
G

1

12

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.

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.

password autofill prompt on 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.

Gaffrigged answered 15/4, 2020 at 12:58 Comment(6)
I am not sure about your question, but if I understood well you could make that with an alertView after the succesfull login, or with a switch named "remember me?" that is turned off by default on the login page, which could be turned on by the user before logging in.Cheshvan
Apologies @stacks, I've updated the question with more details now. Let me know if it's still unclear.Gaffrigged
No problem, it's clear now. I will try to answer your questionCheshvan
@FredrikA. Did you found the solution?Chaechaeronea
@iDroid I did not, sadly.Gaffrigged
"Autofill Credential Provider" is not to have auto-generated passwords. It's for apps that generate credentials for other apps, e.g. password managers. It's in the word "provider".Incoordination
H
11

The problem with password autofill is not the UIKit/SwiftUI implementation. 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!).

  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.
    You can test access using the command curl https://www.example.com/.well-known/apple-app-site-association. (Yes, HTTPS must be enabled and have a valid certificate!)

  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")

{
  "webcredentials": {
    "apps": [ 
      "A1BC23REUG.com.example.signin-playground"
    ]
  }
}
  1. Mark the input fields in SwiftUI using the appropriate textContentType modifier.

I have written a small example in this gist which I successfully tested.

If autofill is not working (e.g. 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. 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

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.

Handily answered 9/7, 2022 at 12:2 Comment(1)
Wow this feels overly complicated compared to Android's AutofillManager.commit() function call.Packing

© 2022 - 2024 — McMap. All rights reserved.