登录Facebook评论iOS应用(Swift)没有反应。

11

我正在尝试将Facebook 评论插件集成到我的原生应用程序中。

Facebook 评论框已经呈现出来了。

进入图像描述

但是,当我点击“登录Facebook以发布评论”时,什么也没发生。我正在尝试使用以下函数捕获事件:func userContentController(userContentController: WKUserContentController,didReceiveScriptMessage message: WKScriptMessage) 但它不起作用。

这是我的代码:

import UIKit
import WebKit

class FacebookCommentsViewController: UIViewController, WKScriptMessageHandler{


    var webView: WKWebView!

    override func viewDidLoad()
    {
        super.viewDidLoad()

        // WKWebView
        let contentController = WKUserContentController();
        contentController.addScriptMessageHandler(self,name: "callbackHandler")

        let configuration = WKWebViewConfiguration()
        configuration.userContentController = contentController

        webView = WKWebView(frame: CGRectMake(0, 0, self.view.frame.width, self.view.frame.height), configuration: configuration)
        webView.scrollView.bounces = false
        self.view.addSubview(webView)

        let facebookCommentsURL = "<!DOCTYPE html><html> <head> <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\"> </head> <body> <div id=\"fb-root\"></div><script>(function(d, s, id){var js, fjs=d.getElementsByTagName(s)[0]; if (d.getElementById(id)) return; js=d.createElement(s); js.id=id; js.src=\"http://connect.facebook.net/en_US/all.js#xfbml=1&appId=527957123932456&status=0\"; fjs.parentNode.insertBefore(js, fjs);}(document, 'script', 'facebook-jssdk'));</script> <div class=\"fb-comments\" data-href=\url-to-my-item data-width=\"470\" data-num-posts=\"5\"></div></body></html>"

        webView.loadHTMLString(facebookCommentsURL, baseURL: NSURL(string:"my base url"))
    }

    func userContentController(userContentController: WKUserContentController,didReceiveScriptMessage message: WKScriptMessage)
    {
        print(message.name)
    }
}

我做错了什么?


你在使用Cocoapods吗? - Charles Truluck
仅限插件 - https://developers.facebook.com/docs/plugins/comments - Luda
你是否正在使用 Facebook SDK 进行登录? - Faran Ghani
@FaranGhani 不,只有这个 developers.facebook.com/docs/plugins/comments - Luda
请问您能否在使用loadHTMLString()后,将webView中的所有HTML内容发布出来吗? - user2917245
3个回答

1

找到了!

我已经将WKWebView替换为UIWebView,一切正常!

当登录成功时,请刷新webView:

func webViewDidFinishLoad(webView: UIWebView)
    {
        let currentURL : NSString = (webView.request?.URL!.absoluteString)!
        if (currentURL.containsString("/login_success"))
        {
            self.reloadFacebookComments()
        }

        self.updateFramesAccordingToWebViewcontentSize()
    }

0

你的JS代码格式有误吗?代码生成器为我生成了这个:

<div id="fb-root"></div>
<script>
  (function(d, s, id) {
    var js, fjs = d.getElementsByTagName(s)[0];
    if (d.getElementById(id)) return;
    js = d.createElement(s);
    js.id = id;
    js.src = "//connect.facebook.net/en_US/sdk.js#xfbml=1&version=v2.5";
    fjs.parentNode.insertBefore(js, fjs);
  }(document, 'script', 'facebook-jssdk'));
</script>

<!DOCTYPE html>
<html>

<head>
  <meta name=\ "viewport\" content=\ "width=device-width, initial-scale=1.0\">
</head>

<body>
  <div id=\ "fb-root\"></div>
  <script>
    (function(d, s, id) {
        var js, fjs = d.getElementsByTagName(s)[0];
        if (d.getElementById(id)) return;
        js = d.createElement(s);
        js.id = id;
        js.src = \"http://connect.facebook.net/en_US/all.js#xfbml=1&appId=527957123932456&status=0\"; fjs.parentNode.insertBefore(js, fjs);}(document, 'script', 'facebook-jssdk'));
  </script>
  <div class=\ "fb-comments\" data-href=\url-to-my-item data-width=\ "470\" data-num-posts=\ "5\"></div>
</body>

</html>

从我的理解来看,它看起来是这样的吗?

编辑:哦,也许不是。字符串的其他部分被摘出了上下文。


如果您有新的问题,请通过单击提问按钮来提出。如果它有助于提供上下文,请包含此问题的链接。 - Kousik
@200OK 猜想你有些困惑。这是对这个问题的一种回答。 - Praveen Kumar Purushothaman
是的,这就是我在facebookCommentsURL中拥有的内容。我并不真正理解你的回答。 - Luda

0

无需更改Web视图。主要问题是Facebook脚本试图在新页面(_blank)中打开此页面,因此脚本在Web视图中无法正常工作。为了解决WKWebView中的这个问题,您需要在控制器中实现WKUIDelegate并在viewDidLoad方法中添加_webView.UIDelegate = self代码。

然后调用此委托方法以防止此问题:

Objective C

- (WKWebView *)webView:(WKWebView *)webView createWebViewWithConfiguration:(WKWebViewConfiguration *)configuration forNavigationAction:(WKNavigationAction *)navigationAction windowFeatures:(WKWindowFeatures *)windowFeatures
{

  if (!navigationAction.targetFrame.isMainFrame) {

    [webView loadRequest:navigationAction.request];
  }

  return nil;
}

in Swift

optional func webView(_ webView: WKWebView, createWebViewWithConfiguration configuration: WKWebViewConfiguration,
   forNavigationAction navigationAction: WKNavigationAction,
        windowFeatures windowFeatures: WKWindowFeatures) -> WKWebView?{
  if navigationAction.targetFrame.isMainFrame == nil {
      webView.loadRequest(navigationAction.request)
  }

  return nil
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接