I am trying to incorporate Facebook Comments Plugin into my native app.
The Facebook Comments Box is presented alright.
but when I press "Login to Facebook to Post a Comment", nothing happens. I am trying to catch the event with this function:
func userContentController(userContentController: WKUserContentController,didReceiveScriptMessage message: WKScriptMessage)
but it's not working.
Here is my code:
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)
}
}
What am I doing wrong?
Fount it!
I've replaced WKWebView
with UIWebView
and everything worked!
When login was successful refresh webView:
func webViewDidFinishLoad(webView: UIWebView)
{
let currentURL : NSString = (webView.request?.URL!.absoluteString)!
if (currentURL.containsString("/login_success"))
{
self.reloadFacebookComments()
}
self.updateFramesAccordingToWebViewcontentSize()
}