ASWebAuthenticationSession with Universal link never dismiss the sign-in screen

We’re using ASWebAuthenticationSession with Universal Link for the redirectURI. On success sign-in, server returns an universal link with the authentication code. 
 
The SceneDelegate, func scene(
scene: UIScene, continue userActivity:) is called, but the completionHandler from the WebAuthentication Session is never called and the sign-in screen never dismiss.
 
Do you have an idea why?
Do you have a sample Xcode project?

I create a singleton class to handle this

SceneDelegate

@available(iOS 13.0, *)

    func scene(_ scene: UIScene, continue userActivity: NSUserActivity) {

        // handle here

        OAuthManager.instance.callBackUserActivity(userActivity: userActivity)

    }

OAuthManager.swift

import Foundation

import AuthenticationServices



protocol UserActivityListener {

    func callBackUserActivity( userActivity : NSUserActivity )

}



class OAuthManager {

    public static let instance = OAuthManager()

    var asWebSession: ASWebAuthenticationSession?

}

extension OAuthManager : UserActivityListener {

    func callBackUserActivity(userActivity: NSUserActivity) {

        // Get URL components from the incoming user activity.

        guard userActivity.activityType == NSUserActivityTypeBrowsingWeb,

              let incomingURL = userActivity.webpageURL,

              let components = NSURLComponents(url: incomingURL, resolvingAgainstBaseURL: true) else {

            return

        }

        

        // Check for specific URL components that you need.

        guard let path = components.path,

              let params = components.queryItems else {

            return

        }

        asWebSession?.cancel()


        print("path = \(userActivity.webpageURL)")



        if let token = params.first(where: { $0.name == "x-auth" })?.value {

            print("token = \(token)")

        }

    }


}

Your ViewController

class CallWebViewViewController: UIViewController {
// set your instance
    var oauthManager = OAuthManager.instance


    private func startSignInAsWebAuthSession() {
        let callbackURLScheme = "YOURAPP"
        guard let authURL = URL(string: "YOUR URL LINK") else { return }
            
            self.oauthManager.asWebSession = ASWebAuthenticationSession.init(url: authURL, callbackURLScheme: callbackURLScheme,completionHandler: { callbackURL, error in
                // we dont listen to the call back, this web authentication session only open for login only
            })
            
            oauthManager.asWebSession?.prefersEphemeralWebBrowserSession = true
            oauthManager.asWebSession?.presentationContextProvider = self
            oauthManager.asWebSession?.start()
    }
}

extension CallWebViewViewController: ASWebAuthenticationPresentationContextProviding {
    func presentationAnchor(for session: ASWebAuthenticationSession) -> ASPresentationAnchor {
        ASPresentationAnchor()
    }
}

UniversalLink and redirectURI are different things. OAuth standard protocol assumes that once the authentication part on the ASWebAuthenticationSession panel is completed successfully, the backend will redirect you to the URI indicated into the redirectURI parameter you used to instruct the request for the ASWebAuthenticationSession.

If this doesn't happen for any reason (like a 302 on the redirectURI), then the ASWebAuthenticationSession isn't completed and your call back is never called.

ASWebAuthenticationSession with Universal link never dismiss the sign-in screen
 
 
Q