登录 Facebook 评论 iOS (swift) 没有响应

Login to Facebook Comments iOS (swift) not responding

我正在尝试将 Facebook Comments Plugin 合并到我的本机应用程序中。

Facebook 评论框显示正常。

但是当我按下 "Login to Facebook to Post a Comment" 时,没有任何反应。我正在尝试使用此功能捕获事件: 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)
    }
}

我做错了什么?

你的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>

据我所知,它看起来像吗?

编辑:哦,也许不是。字符串的其他部分已脱离上下文。

加油!

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

登录成功后刷新webView:

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

        self.updateFramesAccordingToWebViewcontentSize()
    }

无需更改网页视图。主要问题是 facebook 脚本试图在新页面 (_blank) 中打开此页面,因此脚本在 webview 中不起作用。要防止 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;
}

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
}