UIWebView 中的 Facebook 身份验证不会重定向回我网站上要求身份验证的原始页面 [英] Facebook authentication in a UIWebView does not redirect back to original page on my site asking for auth

查看:16
本文介绍了UIWebView 中的 Facebook 身份验证不会重定向回我网站上要求身份验证的原始页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我们的 iOS 应用中,我们有一个 UIWebView,用于显示我们域上的 Web 内容,该视图具有 Facebook 评论模块.评论模块要求用户使用 facebook 登录.当用户点击登录按钮时,他们会通过登录流程,但永远不会重定向回我们的页面.他们最终出现在 FB 拥有的页面上,该页面只是告诉用户您现在已登录".

In our iOS app, we have a UIWebView that shows web content on our domain that has a Facebook comment module. The comment module requires that the user is signed in with facebook. When user clicks on the sign in button, they are taken through the sign in flow, but are never redirect back to our page. They end up on an FB owned page that just tells the user "You are now signed in".

重现步骤:

  1. 在 iOS 应用中创建 UIWebView,并在您拥有的某个域(例如 http://foo.com/test.htm).
  2. 点击评论模块上的登录"按钮,注意您被重定向到 FB 登录.
  3. 使用有效的 FB 凭据登录并观察会发生什么.

在您登录(第 3 步)后,我希望在成功进行身份验证后,您会被重定向回原始页面(例如 http://foo.com/test.htm) 以便您可以继续您的互动.然而,这并没有发生.

After you sign in (step 3) I would expect that after a successful authentication, you are redirected back to the original page (e.g http://foo.com/test.htm) so you can continue your interaction. However, this isn't happening.

相反,您在一个 FB 拥有的页面上,该页面只是说您现在已登录"之类的内容,并且您被困在那里.没有重定向发生.

Instead, you are on an FB owned page that just says something like "You are now signed in" and you are trapped there. No redirect happens.

这确实是一个错误还是我应该做些什么来确保重定向发生?

Is this indeed a bug or is there something else I should be doing to ensure the redirect happens?

推荐答案

如果你只支持 iOS 8 及更高版本,你可以使用 WKWebView 已经实现了 @kabuko:

If you are just supporting iOS 8 and up, you can use WKWebView which already implements the functionality described by @kabuko:

// Container view including the main WKWebView
var container : UIView?
var popupWebView : WKWebView?

override func viewDidLoad() {
    super.viewDidLoad()

    let prefs = WKPreferences()
    prefs.javaScriptEnabled = true
    // allow facebook to open the login popup
    prefs.javaScriptCanOpenWindowsAutomatically = true

    let config = WKWebViewConfiguration()
    config.preferences = prefs

    webView = WKWebView(frame: container.frame, configuration: config)
    webView?.UIDelegate = self
    webView?.navigationDelegate = self
}

// callback if the content of the webView wants to create a new window
func webView(webView: WKWebView, createWebViewWithConfiguration configuration: WKWebViewConfiguration, forNavigationAction navigationAction: WKNavigationAction, windowFeatures: WKWindowFeatures) -> WKWebView? {
    // create new popup webview and add it to the view hierarchy
    popupWebView = WKWebView(frame: container.frame, configuration: configuration)
    container.addSubview(popupWebView!)
    return popupWebView
}

func webView(webView: WKWebView, didFinishNavigation navigation: WKNavigation!) {
    // if the main webView loads a new page (e.g. due to succesful facebook login)
    // remove the popup
    if (popupWebView != nil) {
        popupWebView?.removeFromSuperview()
        popupWebView = nil
    }
}

这篇关于UIWebView 中的 Facebook 身份验证不会重定向回我网站上要求身份验证的原始页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆