简体   繁体   中英

Session completion handler not being called for ASWebAuthenticationSession

I am trying to use ASWebAuthenticationSession web view. After the authentication is complete, the session completion handler is not being called. Hence, the web view doesn't dismiss.

guard let authURL = URL(string: "https://github.com/login/oauth/authorize?client_id=<client_id>/") 
else { return }
let scheme = "octonotes"
session = ASWebAuthenticationSession.init(url: authURL, callbackURLScheme: scheme, completionHandler: { callbackURL, error in
       // Handle the callback.
       print(callbackURL!)
       print(error!)      
        })
session?.presentationContextProvider = self
session?.start()

I have set the callback url scheme in info.plist. The same is updated in Targets -> info -> URL Types It looks like: URL Types

After running the above code, ASWebAuthenticationSession web view is presented, which provides user with sign in page. Once the authentication is complete, web view does not dismiss unlike WKWebView. There is cancel option on top left of the web view, it calls the completion handler with error.

Is there a way to dismiss webview after the authentication session is complete?

It looks like you're missing the redirect URI in your authURL - so GitHub doesn't know where to redirect the successful auth to.

Try this:

let scheme = "octonotes"
guard let authURL = URL(string: "https://github.com/login/oauth/authorize?client_id=<client_id>&redirect_uri=\(scheme)://authcallback")

Bear in mind you may be missing some other parameters too, take a look here for the other ones you can provide https://developer.github.com/apps/building-oauth-apps/authorizing-oauth-apps/#redirect-urls

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM