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

时间:2016-03-07 15:25:20

标签: ios uiwebview

使用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和js文件包含在mainBundle中,而不是js文件放在同一目录fileUrl

例如: fileUrl指向

  

“文件:///var/mobile/Containers/Data/Application/4F2A96AF-8E58-41A6-A4D3-A19D254C048F/Documents/courses/agdg_test1455704820291/index.html”

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

  

“文件:///var/mobile/Containers/Data/Application/4F2A96AF-8E58-41A6-A4D3-A19D254C048F/Documents/courses/agdg_test1455704820291/data/player.js”

但是包括来自捆绑工作的js文件是完美的。我还使用iPad文件资源管理器验证了文件/data/player.js是否存在,情况确实如此。

更新2

我用set allowingReadAccessToURL将其解析为整个目录,而不仅仅是我的索引文件。

由于我使用这种方法来配置WKWebView,js代码永远不会完成缓冲声音/视频在设备上播放......我不明白在-[WKWebView loadFileURL:allowingReadAccessToURL:]和{之间执行速度如此之慢的区别是谁{1}}

1 个答案:

答案 0 :(得分:7)

在iOS 9及更高版本中,WKWebView有一种新方法可以满足您的需求,-[WKWebView loadFileURL:allowingReadAccessToURL:]

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

如果您支持iOS 8,则会变得更加困难。有关详细信息,请参阅this answer