Search code examples
iosswiftuiautofill

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.

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.


Solution

  • 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.