4

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&#039;s day!</p>Posted by <a href=\"https://www.facebook.com/BestofVines\">Best Vines</a> on&nbsp;<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

enter image description here

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&#039;s day!</p>Posted by <a href=\"https://www.facebook.com/BestofVines\">Best Vines</a> on&nbsp;<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!")
    }
  }
}
2
  • I suggest you to check your "myHTML.html" file in file system. Try to open it with a browser in OSX. Commented Oct 5, 2015 at 18:37
  • I tried altering the html so that it wasn't protocol agnostic by adding "https:" before the "//connect.facebook.net/sv_SE/sdk.js#xfbml=1&version=v2.3" in the js.src. That doesn't seem to work either even though it did for Twitter embeds. Commented Nov 10, 2015 at 22:38

1 Answer 1

2

I solved this by using GCDWerbServer: https://github.com/swisspol/GCDWebServer

So, I used server to load local css files, js and images, that enables you to set http link for web view base url. After doing this, Facebook posts, videos started to come up just like in regular browser.

Sign up to request clarification or add additional context in comments.

1 Comment

could you share you code? I am trying to create embed posts as well. Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.