Facebook has this new feature where they allow users to embed public posts into a web page. I want to try to use this in an iPhone application inside a UIWebView. Escaping the code necessary is very straight forward but even if I escape the code manually, the web view will not load the post properly. The JavaScript doesn't work at all.
class WebViewController: UIViewController{
@IBOutlet weak var webView: UIWebView!
override func viewDidLoad() {
loadFacebookPost()
}
func loadFacebookPost(){
//Code from Facebook as a string
var myHTML: String = "<html><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 = \"//connect.facebook.net/sv_SE/sdk.js#xfbml=1&version=v2.3\"; fjs.parentNode.insertBefore(js, fjs);}(document, 'script', 'facebook-jssdk'));</script><div class=\"fb-post\" data-href=\"https://www.facebook.com/BestofVines/posts/691077077726242\" data-width=\"350\"><div class=\"fb-xfbml-parse-ignore\"><blockquote cite=\"https://www.facebook.com/BestofVines/posts/691077077726242\"><p>Steven Tyler made this girl's day!</p>Posted by <a href=\"https://www.facebook.com/BestofVines\">Best Vines</a> on <a href=\"https://www.facebook.com/BestofVines/posts/691077077726242\">den 2 oktober 2015</a></blockquote></div></div></body></html>"
webView.loadHTMLString(myHTML, baseURL: nil)
}
This is the post I tried to embed
And this is how it was displayed
I've replaced all quotation marks with \" inside myHTML. What am I missing?
EDIT
I've also tried this solution. This time the view is blank, not showing anything. Code:
import Foundation
import UIKit
import WebKit
class WebViewController: UIViewController{
@IBOutlet var uiview: UIView!
var webView: WKWebView?
override func loadView() {
super.loadView()
self.webView = WKWebView()
self.uiview = self.webView!
}
override func viewDidLoad() {
super.viewDidLoad()
loadFacebookPost()
}
func loadFacebookPost(){
var path = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as! NSString
path = path.stringByAppendingString("/myHTML.html")
var textHTML: String = "<html><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 = \"//connect.facebook.net/sv_SE/sdk.js#xfbml=1&version=v2.3\"; fjs.parentNode.insertBefore(js, fjs);}(document, 'script', 'facebook-jssdk'));</script><div class=\"fb-post\" data-href=\"https://www.facebook.com/BestofVines/posts/691077077726242\" data-width=\"350\"><div class=\"fb-xfbml-parse-ignore\"><blockquote cite=\"https://www.facebook.com/BestofVines/posts/691077077726242\"><p>Steven Tyler made this girl's day!</p>Posted by <a href=\"https://www.facebook.com/BestofVines\">Best Vines</a> on <a href=\"https://www.facebook.com/BestofVines/posts/691077077726242\">den 2 oktober 2015</a></blockquote></div></div></body></html>"
var ok: Bool = textHTML.writeToFile(path as String, atomically: true, encoding: NSUTF8StringEncoding, error: NSErrorPointer())
if ok {
println("Write done!")
var url: NSURL = NSURL.fileURLWithPath(path as String, isDirectory: false)!
webView!.loadRequest(NSURLRequest(URL: url))
}
else {
println("Error!")
}
}
}

