How to close ASWebAuthenticationSession with redirect to external website not to app universal link

982 Views Asked by At

I have an authorisation flow that at the end redirects to our API endpoint not to the app universal link like https://api.example.com/redirect not the app://redirect

For now I am able to use WKWebView to detect that redirect was done by comparing urls and if match to close WebView.

The problem is that in this approach I cannot use google login (WebView is rejected) during this flow.

I tried to use ASWebAuthenticationSession but after redirect I am not able to detect that this redirect was done (as it hits API not the app) to close AuthenticationSession view automatically.

Is it possible at all in such case or the only way to close AS is to redirect to app universal link app:// not to the https://?

Any help really appreciated

1

There are 1 best solutions below

0
On

You need to use the ASWebAuthenticationSession window with the AppAuth pattern, as described in RFC8252. This is a form of the system browser so will not be blocked by providers such as Sign in with Google.

This form of login can be used with either custom URI schemes, such as com.mycompany.myapp:/callback or with HTTPS callback URIs that require iOS universal links to be configured. You are then notified when login conpletes, or is cancelled, or fails.

A sample app of mine does logins with universal links via this code, which adapts AppAuth classes to more of a [Swift async await style. If you are new to AppAuth libraries, see also my introductory blog post.

func login(viewController: UIViewController) async throws {

    try await self.authenticator.getMetadata()

    try await MainActor.run {
        try self.authenticator.startLoginRedirect(viewController: viewController)
    }

    let response = try await self.authenticator.handleLoginResponse()

    try await self.authenticator.finishLogin(authResponse: response)
}