2015-10-07 86 views
13

我试图将Facebook Comments Plugin并入我的本地应用程序。登录Facebook注释iOS(swift)not responding

Facebook评论框呈现正常。

enter image description here

但是当我按下“登录到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) 
    } 
} 

我在做什么错?

+0

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; } 

您正在使用的CocoaPods? –

+0

不,只有插件 - https://developers.facebook.com/docs/plugins/comments – Luda

+0

您是否使用Facebook sdk登录? –

回答

3

Fount it!

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

当登录成功刷新web视图:

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>

从我可以告诉它看起来是什么?

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

+0

如果您有新问题,请点击[Ask Question](问问题)(http://stackoverflow.com/questions/ask)按钮。如果有助于提供上下文,请包含此问题的链接。 – Nullify

+0

@ 200OK猜你很困惑。这是这个问题的答案。 –

+0

是的,这是我在facebookCommentsURL。我真的不明白你的答案 – Luda

0

有没有需要改变网页视图。主要问题是Facebook脚本试图在新页面(_blank)中打开此页面,因此脚本不适用于webview。要在WKWebView中防止出现此问题,您需要在控制器中实现WKUIDelegate,并且您需要在viewDidLoad方法中添加_webView.UIDelegate = self此代码。

然后调用此委托方法来避免此问题:在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 
}