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

问题描述

对于以前的iOS 8测试版,加载一个本地Web应用程序(在Bundle中),它适用于 UIWebView WKWebView ,我甚至使用新的 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)

但是在测试版4中,我只有一个空白的白色屏幕( UIWebView 仍在工作),看起来没有任何内容被加载或执行。我在日志中看到错误:



无法为 /

$ b创建沙箱扩展名
$ b

任何指导我正确方向的帮助?谢谢!

解决方案

他们终于解决了这个错误!现在我们可以使用 - [WKWebView loadFileURL:allowsReadAccessToURL:]
显然,修补程序在所述,这是WKWebView 正如他所说的有一个解决办法:)



我正在回答的原因只是因为我想在这里展示解决方法。 中显示的IMO代码充满了大多数人无关的详细信息可能不感兴趣。



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

  func fileURLForBuggyWKWebView8(fileURL:URL)throws  - > URL {
//一些安全检查
if!fileURL.isFileURL {
throw NSError(
domain:BuggyWKWebViewDomain,
code:1001,
userInfo:[NSLocalizedDescriptionKey:NSLocalizedString(URL必须是文件URL。,评论:)])
}
试试! fileURL.checkResourceIsReachable()

//创建/ temp / www目录
let fm = FileManager.default
let tmpDirURL = URL(fileURLWithPath:NSTemporaryDirectory())。appendingPathComponent (www)
试试! fm.createDirectory(at:tmpDirURL,withIntermediateDirectories:true,attributes:nil)

//现在将给定文件复制到临时目录
let dstURL = tmpDirURL.appendingPathComponent(fileURL.lastPathComponent)
让_ =试试? fm.removeItem(at:dstURL)
试试! fm.copyItem(at:fileURL,to:dstURL)

/// temp / www中的文件加载崩溃:)
return dstURL
}

可以用作:

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

if #available(iOS 9.0,*){
// iOS9及以上版本。一年后事情还可以。
webView.loadFileURL(fileURL,allowsReadAccessTo:fileURL)
} else {
// iOS8。事情可以(有时)解决方法
//勇敢的人可以做这个
// fileURL =试试! pathForBuggyWKWebView8(fileURL:fileURL)
// webView.load(URLRequest(url:fileURL))
do {
fileURL = try fileURLForBuggyWKWebView8(fileURL:fileURL)
webView.load( URLRequest(url:fileURL))
} catch错误为NSError {
print(Error:+ 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 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)
        }
    }
}

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

07-30 09:33