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

ios – 加载本地文件在模拟器中工作正常但在设备上崩溃,第1张

概述使用WKWebview播放本地文件在模拟器上工作,但在设备上给我这个错误(iOS 9.2.1) fail in didFailProvisionalNavigation Error Domain=NSPOSIXErrorDomain Code=1 "Operation not permitted"UserInfo={NSErrorFailingURLKey=file:///var/mobile/ 使用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_structfiletoplay = 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",isDirectory: false)let target = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("start",ofType: "HTML",isDirectory: false)let params = "?filetoplay=" + filetoplay + "&commandsfilePath=" + String(commandsfilePath) + "&jqueryfilePath=" + String(jqueryfilePath) + "&" + NSUUID().UUIDStringlet url = NSURL(string: Nsstring(string: target.absoluteString + params) as String)NSURLCache.sharedURLCache().removeAllCachedResponses()NSURLCache.sharedURLCache().diskCapacity = 0if(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",handler: { (action: UIAlertAction!) in        self.webVIEw.loadfileURL(url!,allowingReadAccesstoURL: url!)    }))    presentVIEwController(askForResume,animated: true,completion: nil)} else {    self.webVIEw.loadfileURL(url!,allowingReadAccesstoURL: url!)}

实现allowReadAccesstoURL后,我遇到了这个错误:

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().UUIDStringlet 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)上,它是从mainBundle加载的fileUrl和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”

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

更新2

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

由于我使用这种方法来配置WKWebVIEw,Js代码永远不会完成缓冲声音/视频在设备上播放…我不明白是什么区别使得执行之间的执行如此缓慢 – [WKWebVIEw loadfileURL:allowsReadAccesstoURL:]和[ WKWebVIEw loadRequest]

解决方法 在iOS 9及更高版本中,WKWebVIEw有一个新方法可以满足您的需求,– [WKWebVIEw loadfileURL:allowsReadAccesstoURL:]

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

如果你支持iOS 8,那就有点难了.有关详细信息,请参见this answer.

总结

以上是内存溢出为你收集整理的ios – 加载本地文件在模拟器中工作正常但在设备上崩溃全部内容,希望文章能够帮你解决ios – 加载本地文件在模拟器中工作正常但在设备上崩溃所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1015634.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-22
下一篇 2022-05-22

发表评论

登录后才能评论

评论列表(0条)

保存