加载本地文件在模拟器中工作正常但在设备上崩溃

Loading local file works fine in simulator but crash on device

使用 WKWebview 播放本地文件在模拟器上有效,但在设备上给我这个错误 (iOS 9.2.1)

fail in didFailProvisionalNavigation Error Domain=NSPOSIXErrorDomain Code=1 "Operation not permitted"
UserInfo={NSErrorFailingURLKey=file:///var/mobile/Containers/Data/Application/2FBE4DE6-9441-4C5A-BB1F-8598CA89664C/Documents/courses/agdg_test1455704820291/index.html?jqueryFilePath=file:///var/mobile/Containers/Bundle/Application/5A442D34-3360-46C7-8B28-B6F3AED373CE/elearning.app/view/jquery.js&commandsFilePath=file:///var/mobile/Containers/Bundle/Application/5A442D34-3360-46C7-8B28-B6F3AED373CE/elearning.app/view/commands.js,
_WKRecoveryAttempterErrorKey=<WKReloadFrameErrorRecoveryAttempter: 0x15e295f0>,NSErrorFailingURLStringKey=file:///var/mobile/Containers/Data/Application/2FBE4DE6-9441-4C5A-BB1F-8598CA89664C/Documents/courses/agdg_test1455704820291/index.html?jqueryFilePath=file:///var/mobile/Containers/Bundle/Application/5A442D34-3360-46C7-8B28-B6F3AED373CE/elearning.app/view/jquery.js&commandsFilePath=file:///var/mobile/Containers/Bundle/Application/5A442D34-3360-46C7-8B28-B6F3AED373CE/elearning.app/view/commands.js}

这是配置 webView 的代码:

let lesson:NSMutableArray = courseDB_Manager.getInstance().getRowDB("SELECT * FROM lesson_chapter WHERE lesson_chapter_id = ?", args: [chapterToPlay], structType: "lesson_chapter")
let occ: lesson_chapter_struct = lesson[0] as! lesson_chapter_struct
fileToPlay = Utility.getPath("courses/" + occ.lesson_chapter_fichier)

let commandsFilePath = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("commands", ofType: "js", inDirectory: "view")!, isDirectory: false)
let jqueryFilePath = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("jquery", ofType: "js", inDirectory: "view")!, isDirectory: false)

let target = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("start", ofType: "html", inDirectory: "view")!, isDirectory: false)
let params = "?fileToPlay=" + fileToPlay + "&commandsFilePath=" + String(commandsFilePath) + "&jqueryFilePath=" + String(jqueryFilePath) + "&" + NSUUID().UUIDString
let url = NSURL(string: NSString(string: target.absoluteString + params) as String)
NSURLCache.sharedURLCache().removeAllCachedResponses()
NSURLCache.sharedURLCache().diskCapacity = 0

if(progress.current() != 1) {
    let askForResume = UIAlertController(title: "Resume", message: "Resume ????????????", preferredStyle: UIAlertControllerStyle.Alert)
    askForResume.addAction(UIAlertAction(title: "YES", style: .Default, handler: { (action: UIAlertAction!) in
        self.resume = true
        self.webView.loadFileURL(url!, allowingReadAccessToURL: url!)
    }))
    askForResume.addAction(UIAlertAction(title: "NO", style: .Default, handler: { (action: UIAlertAction!) in
        self.webView.loadFileURL(url!, allowingReadAccessToURL: url!)
    }))
    presentViewController(askForResume, animated: true, completion: nil)
} else {
    self.webView.loadFileURL(url!, allowingReadAccessToURL: url!)
}

实施 allowingReadAccessToURL 后出现此错误:

Received an unexpected URL from the web process: 'file:///[...]commands.js'
Received an invalid message "WebPageProxy.DecidePolicyForNavigationAction" from the web process.

是因为js请求的文件也遇到了同样的问题?

func webView(webView: WKWebView, decidePolicyForNavigationAction navigationAction: WKNavigationAction, decisionHandler: (WKNavigationActionPolicy) -> Void) {
    decisionHandler(WKNavigationActionPolicy.Allow)
}

更新 1 - 在第二次更新中解决

现在,我正在使用此代码:

let target = NSURL(fileURLWithPath: fileToPlay + "/index.html", isDirectory: false)
let params = "?commandsFilePath=" + String(commandsFilePath) + "&jqueryFilePath=" + String(jqueryFilePath) + "&" + NSUUID().UUIDString
let url = NSURL(string: NSString(string: target.absoluteString + params) as String)

if #available(iOS 9.0, *) {
    self.webView.loadFileURL(url!, allowingReadAccessToURL: url!)
} else {
    do {
        let fileUrl = try self.fileURLForBuggyWKWebView8(url!)
        self.webView.loadRequest(NSURLRequest(URL: fileUrl))
    } catch let error as NSError {
        print("Error: " + error.debugDescription)
    }
}

谁在模拟器(iOS 9)上工作正常,即加载fileUrl和js代码包含的文件。 但是在设备上(也是 iOS 9),它加载 fileUrl 和 mainBundle 中包含的 js 文件,而不是 fileUrl

同一目录中的 js 文件

例如: fileUrl 指向

"file:///var/mobile/Containers/Data/Application/4F2A96AF-8E58-41A6-A4D3-A19D254C048F/Documents/courses/agdg_test1455704820291/index.html"

fileUrl 的子目录中包含 js 文件不起作用

"file:///var/mobile/Containers/Data/Application/4F2A96AF-8E58-41A6-A4D3-A19D254C048F/Documents/courses/agdg_test1455704820291/data/player.js"

但是从 bundle 中包含 js 文件是完美的。我还使用 iPad 文件资源管理器验证了文件 /data/player.js 存在并且确实如此。

更新 2

我通过将 allowingReadAccessToURL 设置为整个目录解决了这个问题,而不仅仅是我的索引文件。

因为我使用这种方法来配置 WKWebView,js 代码永远不会完成缓冲 sounds/videos 以在设备上播放...我不明白是谁的区别导致 [=23] 之间的执行如此缓慢=] 和 [WKWebView loadRequest]

在 iOS 9 及更高版本中,WKWebView 有一个新方法可以执行您想要的操作,-[WKWebView loadFileURL:allowingReadAccessToURL:]

确保您正在使用该调用加载本地资源。

如果你支持iOS8,那就有点困难了。有关详细信息,请参阅 this answer