UIWebView not loading JavaScript properly - Embedded Facebook post

蓝咒 提交于 2019-12-10 20:06:40

问题


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

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!")
    }
  }
}

回答1:


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.



来源:https://stackoverflow.com/questions/32921577/uiwebview-not-loading-javascript-properly-embedded-facebook-post

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!