iOS 8 下 WKWebView 不加载本地文件

WKWebView not loading local files under iOS 8(iOS 8 下 WKWebView 不加载本地文件)

本文介绍了iOS 8 下 WKWebView 不加载本地文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于以前的 iOS 8 测试版,加载本地 Web 应用程序(在 Bundle 中),它适用于 UIWebViewWKWebView,我什至使用新的 WKWebView API.

var url = NSURL(fileURLWithPath:NSBundle.mainBundle().pathForResource("car", ofType:"html"))webView = WKWebView(frame:view.frame)webView!.loadRequest(NSURLRequest(URL:url))view.addSubview(webView)

但在 beta 4 中,我只有一个空白屏幕(UIWebView 仍然有效),看起来没有加载或执行任何内容.我在日志中看到了一个错误:

无法为 /

创建沙盒扩展

有什么帮助可以引导我走向正确的方向吗?谢谢!

解决方案

他们终于解决了这个错误!现在我们可以使用 -[WKWebView loadFileURL:allowingReadAccessToURL:].显然,在 所说,这是 WKWebView 的一个错误 显然不会很快得到解决 正如他所说,有一个解决方法:)

我回答只是因为我想在这里展示解决方法.https://github.com/shazron/WKWebViewFIleUrlTest 中显示的 IMO 代码充满了大多数人不相关的细节可能不感兴趣.

解决方法是 20 行代码,包括错误处理和注释,不需要服务器 :)

func fileURLForBuggyWKWebView8(fileURL: URL) throws ->网址 {//一些安全检查如果 !fileURL.isFileURL {抛出 NSError(域:BuggyWKWebViewDomain",代码:1001,userInfo: [NSLocalizedDescriptionKey: NSLocalizedString("URL 必须是文件 URL.", comment:"")])}尝试!fileURL.checkResourceIsReachable()//创建/temp/www"目录让 fm = FileManager.default让 tmpDirURL = URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent("www")尝试!fm.createDirectory(at: tmpDirURL, withIntermediateDirectories: true, attributes: nil)//现在将给定文件复制到临时目录让 dstURL = tmpDirURL.appendingPathComponent(fileURL.lastPathComponent)让_ =试试?fm.removeItem(在:dstURL)尝试!fm.copyItem(at: fileURL, to: dstURL)//"/temp/www" 中的文件完美加载 :)返回 dstURL}

并且可以用作:

覆盖 func viewDidLoad() {super.viewDidLoad()var fileURL = URL(fileURLWithPath: Bundle.main.path(forResource:"file", ofType: "pdf")!)如果 #available(iOS 9.0, *) {//iOS9 及以上.一年后一切正常.webView.loadFileURL(fileURL, allowedReadAccessTo: fileURL)} 别的 {//iOS8.事情可以(有时)变通//勇敢的人可以做到这一点//fileURL = 试试!pathForBuggyWKWebView8(fileURL: fileURL)//webView.load(URLRequest(url:fileURL))做 {fileURL = 试试 fileURLForBuggyWKWebView8(fileURL: fileURL)webView.load(URLRequest(url:fileURL))} 将错误捕获为 NSError {print("错误:" + error.debugDescription)}}}

For previous iOS 8 betas, load a local web app (in Bundle) and it works fine for both UIWebView and WKWebView, and I even ported a web game using the new WKWebView API.

var url = NSURL(fileURLWithPath:NSBundle.mainBundle().pathForResource("car", ofType:"html"))

webView = WKWebView(frame:view.frame)
webView!.loadRequest(NSURLRequest(URL:url))

view.addSubview(webView)

But in beta 4, I just got a blank white screen (UIWebView still work), looks like nothing is loaded or executed. I saw an error in the log:

Could not create a sandbox extension for /

Any help to guide me to the right direction? Thanks!

解决方案

They finally solved the bug! Now we can use -[WKWebView loadFileURL:allowingReadAccessToURL:]. Apparently the fix was worth some seconds in WWDC 2015 video 504 Introducing Safari View Controller

For iOS8 ~ iOS10 (Swift 3)

As Dan Fabulish's answer states this is a bug of WKWebView which apparently is not being solved any time soon and as he said there is a work-around :)

I am answering just because I wanted to show the work-around here. IMO code shown in https://github.com/shazron/WKWebViewFIleUrlTest is full of unrelated details most people are probably not interested in.

The work-around is 20 lines of code, error handling and comments included, no need of a server :)

func fileURLForBuggyWKWebView8(fileURL: URL) throws -> URL {
    // Some safety checks
    if !fileURL.isFileURL {
        throw NSError(
            domain: "BuggyWKWebViewDomain",
            code: 1001,
            userInfo: [NSLocalizedDescriptionKey: NSLocalizedString("URL must be a file URL.", comment:"")])
    }
    try! fileURL.checkResourceIsReachable()

    // Create "/temp/www" directory
    let fm = FileManager.default
    let tmpDirURL = URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent("www")
    try! fm.createDirectory(at: tmpDirURL, withIntermediateDirectories: true, attributes: nil)

    // Now copy given file to the temp directory
    let dstURL = tmpDirURL.appendingPathComponent(fileURL.lastPathComponent)
    let _ = try? fm.removeItem(at: dstURL)
    try! fm.copyItem(at: fileURL, to: dstURL)

    // Files in "/temp/www" load flawlesly :)
    return dstURL
}

And can be used as:

override func viewDidLoad() {
    super.viewDidLoad()
    var fileURL = URL(fileURLWithPath: Bundle.main.path(forResource:"file", ofType: "pdf")!)

    if #available(iOS 9.0, *) {
        // iOS9 and above. One year later things are OK.
        webView.loadFileURL(fileURL, allowingReadAccessTo: fileURL)
    } else {
        // iOS8. Things can (sometimes) be workaround-ed
        //   Brave people can do just this
        //   fileURL = try! pathForBuggyWKWebView8(fileURL: fileURL)
        //   webView.load(URLRequest(url: fileURL))
        do {
            fileURL = try fileURLForBuggyWKWebView8(fileURL: fileURL)
            webView.load(URLRequest(url: fileURL))
        } catch let error as NSError {
            print("Error: " + error.debugDescription)
        }
    }
}

这篇关于iOS 8 下 WKWebView 不加载本地文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:iOS 8 下 WKWebView 不加载本地文件

基础教程推荐