How to get Password AutoFill to work with ASWebAuthenticationSession?

3.9k Views Asked by At

I have a sample HTML form that follows the password auto-fill requirements. When navigating to the web page without the app, using the built-in Safari app, the native Password AutoFill dialogs for the login and signup forms correctly show:

enter image description here

However, when viewed within the app, the same flow does not trigger the dialog in neither the login or signup submission. It simply dismisses the login/signup screen without any error.

This is how my view controller looks like:

class ViewController: UIViewController {

    @IBAction func didTapLoginButton() {
        let session = ASWebAuthenticationSession(
            url: URL(string: "https://example.com/login.html")!,
            callbackURLScheme: "customscheme") {
                print($0, $1)
        }

        session.prefersEphemeralWebBrowserSession = true
        session.presentationContextProvider = self
        session.start()
    }
}

extension ViewController: ASWebAuthenticationPresentationContextProviding {

    func presentationAnchor(for session: ASWebAuthenticationSession) -> ASPresentationAnchor {
        view.window!
    }
}

Is there a way to make the Password AutoFill to work with the mobile app via ASWebAuthenticationSession? I also tried SFSafariViewController but same issue. I have a sample iOS app here. Any help or experience is greatly appreciated!

2

There are 2 best solutions below

1
On BEST ANSWER

The only working and clean solution I've found after a loooong research is adding a post login page as suggested here. This way the save credentials prompt won't be dissmissed.

Unfortunately I didn't found any solution from the iOS side, it would be really appreciated since in my case I can't change the server side due to project restrictions.

1
On

From the docs:

Password AutoFill recommends credentials only for your app’s associated domain, and the user must authenticate using Face ID or Touch ID before accessing these credentials.

Make sure the app is associated with your domain (example.com in your code). This will allow iCloud Keychain to identify the login form and suggest any stored credentials for that domain.

  1. Associate your app with the domain, as described here.
  2. Add the html tags to your login form, as described here.
  3. Make sure the user is properly authenticated on the iOS device before launching the ASWebAuthenticationSession.