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)
}
这是我试图嵌入的帖子
这就是它的显示方式
我已在\"
内用myHTML
替换了所有引号。我错过了什么?
修改
我也试过this解决方案。这次视图是空白的,没有显示任何内容。代码:
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!")
}
}
}
答案 0 :(得分:2)
我通过使用GCDWerbServer解决了这个问题: https://github.com/swisspol/GCDWebServer
因此,我使用服务器加载本地css文件,js和图像,这使您可以为Web视图基本URL设置http链接。在这样做之后,Facebook发布,视频开始出现,就像常规浏览器一样。