UIWebView 未正确加载 JavaScript - 嵌入式 Facebook 帖子 [英] UIWebView not loading JavaScript properly - Embedded Facebook post

查看:22
本文介绍了UIWebView 未正确加载 JavaScript - 嵌入式 Facebook 帖子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Facebook 有这项新功能,允许用户将公开帖子嵌入到网页中.我想尝试在 UIWebView 内的 iPhone 应用程序中使用它.转义必要的代码非常简单,但即使我手动转义代码,网络视图也不会正确加载帖子.JavaScript 根本不起作用.

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)

}

这是我试图嵌入的帖子

这就是它的显示方式

我已经用 \" 替换了 myHTML 中的所有引号.我错过了什么?

I've replaced all quotation marks with \" inside myHTML. What am I missing?

编辑

我也尝试过这个解决方案.这次视图是空白的,没有显示任何内容.代码:

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

推荐答案

我使用 GCDWerbServer 解决了这个问题:https://github.com/swisspol/GCDWebServer

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

所以,我使用服务器来加载本地 css 文件、js 和图像,这使您能够为 web 视图基本 url 设置 http 链接.这样做之后,Facebook 帖子、视频开始出现,就像在常规浏览器中一样.

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.

这篇关于UIWebView 未正确加载 JavaScript - 嵌入式 Facebook 帖子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆