【问题标题】:how to access downloaded file in IOS?如何在IOS中访问下载的文件?
【发布时间】:2019-12-11 12:49:38
【问题描述】:

我已经在 IOS 中下载了文件,并在下载它的应用程序的当前会话中成功地在播放器中打开。下载文件后,我的路径是:

 file:///var/mobile/Containers/Data/Application/2644B0D9-A68E-4A70-BAC3-2747949A980D/Documents/videoplayback.m4a 

但是当我关闭应用程序并尝试在下一个会话中使用相同的 URL 时,即当应用程序再次打开时,它就不起作用了。当我检查容器时,它显示文件仍然存在。

【问题讨论】:

  • 尝试从路径中删除“file://”。

标签: ios swift


【解决方案1】:

每个会话都是独一无二的,你必须像这样得到它

func url(for fileName: String, extension: String) -> URL? {
   guard let documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first else { return nil }
   return documentsURL.appendingPathComponent(fileName).appendingPathExtension(extension)
}
let url = url(for: "videoplayback", extension: "m4a")

【讨论】:

    【解决方案2】:

    您可以使用最常用的方法,那就是使用UIDocumentInteractionControllerUIAlertController

    检查这部分使用流行的Alamofire 和progressview 的代码。请注意,如果您在控制器中使用它,它必须实现以下委托 UIDocumentInteractionControllerDelegate

    这里是完整的带有下载功能的源代码:

    class MyController: UIViewController, UIDocumentInteractionControllerDelegate {
    
            var progressView: UIProgressView?    
    
            override func viewDidLoad() {
                super.viewDidLoad()
            }               
    
            func downloadCommentFile(id: Int) {
                let destination = DownloadRequest.suggestedDownloadDestination(for: .documentDirectory)  
    
                //MARK: Progress controller
                let alertView = UIAlertController(title: "Downloading", message: "Downloading file", preferredStyle: .alert)
                alertView.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
    
                //  Show UIAlertController it to your users
                present(alertView, animated: true, completion: {
                    //  Add your progressbar after alert is shown (and measured)
                    let margin:CGFloat = 8.0
                    let rect = CGRect(x: margin, y: 72.0, width: alertView.view.frame.width - margin * 2.0 , height: 10.0)
                    self.progressView = UIProgressView(frame: rect)
                    self.progressView!.progress = 0
                    self.progressView!.tintColor = self.view.tintColor
                    alertView.view.addSubview(self.progressView!)
                })
    
                let url = "http://MyUrl/DownloadFile"
                let headers = ["Header1": "header 1 value"]
    
                Alamofire.download(url,
                    method: .post,
                    parameters: ["id": id],
                    encoding: JSONEncoding.default,
                    headers: headers,
                    to: destination).downloadProgress(closure: { (progress) in
                        //progress closure                  
                        self.progressView?.setProgress(Float(progress.fractionCompleted), animated: true)
                    }).response(completionHandler: { (DefaultDownloadResponse) in
                        //here you able to access the DefaultDownloadResponse
                        //result closure
                        alertView.dismiss(animated: true, completion: {
                            let viewer = UIDocumentInteractionController(url: DefaultDownloadResponse.destinationURL!)
                            viewer.delegate = self
                            viewer.presentPreview(animated: true)
                    })
                })      
            }
    
            func documentInteractionControllerViewControllerForPreview(_ controller: UIDocumentInteractionController) -> UIViewController {
                return self
            }
    
            private func documentInteractionControllerViewForPreview(controller: UIDocumentInteractionController!) -> UIView! {
                return self.view
            }
    
            func documentInteractionControllerRectForPreview(_ controller: UIDocumentInteractionController) -> CGRect {
                return self.view.frame
            }
        }
    

    【讨论】:

    • 感谢您抽出宝贵时间,但我的问题与其他问题有关,现在在上述解决方案的帮助下工作正常。还是感谢您的关心。
    • 我后来看到了:)祝你有美好的一天;)
    猜你喜欢
    • 2011-12-16
    • 2021-11-07
    • 1970-01-01
    • 2020-02-15
    • 2015-05-29
    • 1970-01-01
    • 2020-04-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多