ASWebAuthenticationSession dismiss completion?

We're using ASWebAuthenticationSession and whenever no errors occur, all is well. However, if there's an error generated by our OAUTH web endpoint, the authentication session's UI is automatically dismissed. And, right at the beginning of its dismissal, our callback is called. But if we attempt to show say an error alert, or even present the prior view controller, we get errors like: Attempt to present <UINavigationController: ...> on <MainViewController: ...> while a presentation is in progress!


In looking at the documentation, I see no way to tap into the authentication session's UI dismissal. Is there any way to defer work until after the session's UI is dismissed?


For now, we're using a hack where we 'perform with selector' after a one-second delay. But I don't want to rely on that whatsoever.


Here's the gist of our code:


// In our main view controller...

private var webSession : ASWebAuthenticationSession?

func presentAuthorization(withURL aURL: URL) {
    webSession = ASWebAuthenticationSession(url: aURL, callbackURLScheme: "ourcustomscheme://callback",
        completionHandler: { (aURL, anError) in

        if let theCallbackURL = aURL {
            // We get here whenever there are particular errors. If we get a 'cancel' OAUTH
            // error, that is working.  The UI isn't automatically dismissed.  So in our
            // cancel handler we call webSession.cancel() and go to the prior view controller a-ok.

            // But for some reason, any other callbacks seems to auto-dismiss the session's UI.
            // Here, the app is left in a bad state since we cannot present alerts or view
            // controllers.
        } else {
            // this block of code works a-ok.
        } 
    }
}
ASWebAuthenticationSession dismiss completion?
 
 
Q