我正在尝试将Facebook Comments Plugin合并到我的原生应用中。
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)
}
}
我做错了什么?
答案 0 :(得分:3)
Fount it!
我已将WKWebView
替换为UIWebView
,一切正常!
登录成功后刷新webView:
func webViewDidFinishLoad(webView: UIWebView)
{
let currentURL : NSString = (webView.request?.URL!.absoluteString)!
if (currentURL.containsString("/login_success"))
{
self.reloadFacebookComments()
}
self.updateFramesAccordingToWebViewcontentSize()
}
答案 1 :(得分: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>
据我所知,它看起来像是什么?
编辑:哦,也许不是。字符串的其他部分脱离了上下文。
答案 2 :(得分:0)
无需更改网页视图。主要问题是facebook脚本试图在新页面中打开此页面(_blank)因此脚本在webview中不起作用。要在WKWebView
中防止出现此问题,您需要在控制器中实现WKUIDelegate
,并且需要在_webView.UIDelegate = self
方法中添加viewDidLoad
此代码。
然后调用此委托方法以防止此问题:
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
}