【问题标题】:Load local html files from applications's document directory swift wkwebview从应用程序的文档目录加载本地 html 文件 swift wkwebview
【发布时间】:2019-01-29 08:02:49
【问题描述】:

我正在尝试加载一个本地 html 文件,该文件使用 wkwebview 下载到我的应用程序中。除了这个 html 文件,我还下载了两个 js 文件,它们在 html 文件中被引用。

但是无论我做什么,他们都没有在 wkwebview 中加载

 @IBOutlet var webView: WKWebView!
    @IBOutlet var act: UIActivityIndicatorView!
    override func viewDidLoad() {
        super.viewDidLoad()
        // Adding webView content
        webView.uiDelegate = self
        webView.navigationDelegate = self


let fm = FileManager.default
    let docsurl = try! fm.url(for:.documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false)
    let myurl = docsurl.appendingPathComponent("index.html")
    print("my url = ",myurl )
// myurl prints file///var/some directories/index.html
let request = URLRequest(url: myurl!)
            webView.load(request)
}

我的 html 文件

<!DOCTYPE html>
 <html> 
<head>
<script src="jquery.min.js"></script>
<script>
$(document).ready(function(){
  $("button").click(function(){
    $("p").hide();
  });
});
</script>
</head>
<body>

<h2>This is a heading</h2>

<p>This is a paragraph.</p>
<p>This is another paragraph.</p>

<button>Click me</button>

</body>
</html>

但它没有从 safari(Develop/) 加载到屏幕上,我可以看到 An error occured trying to load the resource,页面即将到来:空白

【问题讨论】:

  • myurl 打印什么?您在这里如何使用WKWebView
  • But it is not loading on the screen from safari(Develop/) 是什么意思? Safari 是外部浏览器。你的意思是WebView,而不是Safari?
  • 这段代码let request = URLRequest(url: myurl) self.webView.load(request)没有问题。您需要通过从该 url 创建 Data 对象来检查文件是否存在于同一 url。
  • @TheTiger,是的,它在我的模拟器中运行良好,但在 iPhone(真实设备)中无法运行
  • 您的接受率为 0。您尚未接受所有问题的任何一个答案。这不好。

标签: ios swift wkwebview


【解决方案1】:

URLRequest 基本上是为实时网址制作的。对于系统文件,只需加载URL。您需要使用以下方法,它会起作用:

/*! @abstract Navigates to the requested file URL on the filesystem.
     @param URL The file URL to which to navigate.
     @param readAccessURL The URL to allow read access to.
     @discussion If readAccessURL references a single file, only that file may be loaded by WebKit.
     If readAccessURL references a directory, files inside that file may be loaded by WebKit.
     @result A new navigation for the given file URL.
     */
    @available(iOS 9.0, *)
    open func loadFileURL(_ URL: URL, allowingReadAccessTo readAccessURL: URL) -> WKNavigation?

这样:

self.webView.loadFileURL(myurl, allowingReadAccessTo: myurl)

【讨论】:

    【解决方案2】:

    swift 4.2

     //load HTML
    let bundle = Bundle.main
    var path = bundle.path(forResource: "index", ofType: "html")
    var html = ""
    do {
        try html = String(contentsOfFile: path!, encoding: .utf8)
    } catch {
    //ERROR
    }
    let urlstr = "http://website.com"
    webViewWK.loadHTMLString(html, baseURL: URL(string: urlstr)!)
    

    【讨论】:

      猜你喜欢
      • 2018-01-29
      • 2018-12-26
      • 1970-01-01
      • 2023-03-30
      • 1970-01-01
      • 2017-05-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多