简体   繁体   English

未为 ASWebAuthenticationSession 调用会话完成处理程序

[英]Session completion handler not being called for ASWebAuthenticationSession

I am trying to use ASWebAuthenticationSession web view.我正在尝试使用 ASWebAuthenticationSession Web 视图。 After the authentication is complete, the session completion handler is not being called.身份验证完成后,不会调用会话完成处理程序。 Hence, the web view doesn't dismiss.因此,Web 视图不会关闭。

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.我已经在 info.plist 中设置了回调 url 方案。 The same is updated in Targets -> info -> URL Types It looks like: URL Types同样在 Targets -> info -> URL Types 中更新它看起来像: URL Types

After running the above code, ASWebAuthenticationSession web view is presented, which provides user with sign in page.运行上述代码后,会出现ASWebAuthenticationSession web view,为用户提供登录页面。 Once the authentication is complete, web view does not dismiss unlike WKWebView.身份验证完成后,与 WKWebView 不同,Web 视图不会关闭。 There is cancel option on top left of the web view, it calls the completion handler with error. Web 视图的左上角有取消选项,它调用完成处理程序时出错。

Is there a way to dismiss webview after the authentication session is complete?身份验证会话完成后,有没有办法关闭 webview?

It looks like you're missing the redirect URI in your authURL - so GitHub doesn't know where to redirect the successful auth to.看起来您的 authURL 中缺少重定向 URI - 因此 GitHub 不知道将成功的身份验证重定向到何处。

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请记住,您可能还缺少其他一些参数,请在此处查看您可以提供的其他参数https://developer.github.com/apps/building-oauth-apps/authorizing-oauth-apps/#redirect-网址

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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