SFSafariViewControllerDelegate method for initialLoadDidRedirectTo not being triggered for subsequent reloads as specified in the docs

For some reason, not all of the time but most of the time, the SFSafariViewControllerDelegate method for initialLoadDidRedirectTo is not being triggered for subsequent reloads as specified in the docs.

@discussion This method may be called even after -safariViewController:didCompleteInitialLoad: if the web page performs additional redirects without user interaction.

I am allowing a user to log in with an OAuth 2.0 Provider on the Safari browser and expecting to detect the redirect to continue the flow from the app once their credentials have securely been consumed by the IdP in Safari.

It was working consistently. It went from a 100% success rate up until this week to maybe 1/20 successful redirects.

Code snippet:

            let config = SafariViewController.Configuration()
            config.entersReaderIfAvailable = false
            let vc = SFSafariViewController(url: url, configuration: config)
            vc.delegate = self
            self.safariViewController = vc
            print(self.safariViewController?.delegate)
            self.present(vc, animated: true)

Why is it not always detecting the redirects?

Answered by Engineer in 789502022
I am allowing a user to log in with an OAuth 2.0 Provider on the Safari browser and expecting to detect the redirect to continue the flow from the app once their credentials have securely been consumed by the IdP in Safari.

This API allows you to determine how a shortened or obscured URL might unfurl, but we intentionally restrict the returned URLs to preserve user privacy. From the use case you describe, I think you'd be better served using ASWebAuthenticationSession, as there really are not hard guarantees here.

Specifically we shipped a new API for ASWebAuthenticationSession which allows specifying an https callback.

let session = ASWebAuthenticationSession(
    url: URL(string: "https://example.com/signIn")!,
    callback: .https(host: "mycoolsite.com", path: "/auth") // New API
) { callbackURL, error in
    guard let callbackURL else { return }
    signIn(using: callbackURL)
}

session.presentationContextProvider = presentationContextProvider
session.start()

For more info you can also see this post.

Accepted Answer
I am allowing a user to log in with an OAuth 2.0 Provider on the Safari browser and expecting to detect the redirect to continue the flow from the app once their credentials have securely been consumed by the IdP in Safari.

This API allows you to determine how a shortened or obscured URL might unfurl, but we intentionally restrict the returned URLs to preserve user privacy. From the use case you describe, I think you'd be better served using ASWebAuthenticationSession, as there really are not hard guarantees here.

Specifically we shipped a new API for ASWebAuthenticationSession which allows specifying an https callback.

let session = ASWebAuthenticationSession(
    url: URL(string: "https://example.com/signIn")!,
    callback: .https(host: "mycoolsite.com", path: "/auth") // New API
) { callbackURL, error in
    guard let callbackURL else { return }
    signIn(using: callbackURL)
}

session.presentationContextProvider = presentationContextProvider
session.start()

For more info you can also see this post.

SFSafariViewControllerDelegate method for initialLoadDidRedirectTo not being triggered for subsequent reloads as specified in the docs
 
 
Q