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

标签 ios swift facebook-graph-api facebook-comments wkwebview

我正在尝试将 Facebook Comments Plugin 合并到我的原生应用中。

Facebook 评论框显示正常。

enter image description here

但是当我按下“登录 Facebook 以发表评论”时,没有任何反应。我正在尝试使用此功能捕获事件: func userContentController(userContentController: WKUserContentController,didReceiveScriptMessage 消息: 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)
    }
}

我做错了什么?

最佳答案

加油!

我用 UIWebView 替换了 WKWebView 并且一切正常!

登录成功后刷新webView:

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

        self.updateFramesAccordingToWebViewcontentSize()
    }

关于ios - 登录到 Facebook 评论 iOS (swift) 没有响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32992188/

相关文章:

swift - 最小化应用程序时避免调用 traitCollectionDidChange

ios - 我想使用我的 iOS 应用程序在 Facebook 墙上发布带有自定义文本的视频

python - 使用 Facebook Graph API 获取广告出价估算(覆盖范围/转换)

c# - 如何在按赞按钮后获取用户信息

ios swift CMutableVoidPointer 在 observeValueForKeyPath 中无法识别

iphone - "Operation couldn' t 完成。连接由对等方重置。”在 iOS 上使用 NSURLConnection

ios - 如何避免使用 avplayer 出现内存警告?

ios - 如何在多个 View 中使用来自单例的一个回调

ios - 向苹果商店提交应用程序(验证问题)

ios - 如何在 Swift 中制作一个可点击的 UITextView?