使用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}
Run Code Online (Sandbox Code Playgroud)
这是配置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!)
}
Run Code Online (Sandbox Code Playgroud)
实施后,allowingReadAccessToURL我有这个错误:
Received an unexpected URL from the web process: 'file:///[...]commands.js'
Received an invalid message "WebPageProxy.DecidePolicyForNavigationAction" from the web process.
Run Code Online (Sandbox Code Playgroud)
这是因为,js要求的文件面临同样的问题?
func webView(webView: WKWebView, decidePolicyForNavigationAction navigationAction: WKNavigationAction, decisionHandler: (WKNavigationActionPolicy) -> Void) {
decisionHandler(WKNavigationActionPolicy.Allow)
}
Run Code Online (Sandbox Code Playgroud)
更新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)
}
}
Run Code Online (Sandbox Code Playgroud)
谁在模拟器(iOS 9)上正常工作,即fileUrljs代码包含的加载和文件.但是在设备上(也就是iOS 9),它是fileUrl从mainBundle包含的load 和js文件,而不是同一目录中的js文件fileUrl
例如:
fileUrl指向
"文件:///var/mobile/Containers/Data/Application/4F2A96AF-8E58-41A6-A4D3-A19D254C048F/Documents/courses/agdg_test1455704820291/index.html"
包括来自子目录的js文件fileUrl不起作用
"文件:///var/mobile/Containers/Data/Application/4F2A96AF-8E58-41A6-A4D3-A19D254C048F/Documents/courses/agdg_test1455704820291/data/player.js"
但是包括来自捆绑工作的js文件是完美的.我还用iPad文件资源管理器验证了文件是否/data/player.js存在,情况就是这样.
更新2
我解决了这个问题,设置allowingReadAccessToURL为整个目录,而不仅仅是我的索引文件.
由于我使用这种方法来配置WKWebView,js代码永远不会完成缓冲声音/视频在设备上播放...我不明白谁的差异导致执行如此缓慢-[WKWebView loadFileURL:allowingReadAccessToURL:]和[WKWebView loadRequest]
| 归档时间: |
|
| 查看次数: |
3842 次 |
| 最近记录: |