如何在IOS中访问下载的文件?
我已经在IOS中下载了该文件,并在下载该文件的应用程序的当前会话中成功地在播放器中打开了该文件。下载文件后,我的路径是:如何在IOS中访问下载的文件?,ios,swift,Ios,Swift,我已经在IOS中下载了该文件,并在下载该文件的应用程序的当前会话中成功地在播放器中打开了该文件。下载文件后,我的路径是: file:///var/mobile/Containers/Data/Application/2644B0D9-A68E-4A70-BAC3-2747949A980D/Documents/videoplayback.m4a 但是,当我关闭应用程序并在下一个会话中尝试使用相同的URL时,即当再次打开应用程序时,它就是不起作用。当我检查容器时,它显示文件仍然存在 每个会话都
file:///var/mobile/Containers/Data/Application/2644B0D9-A68E-4A70-BAC3-2747949A980D/Documents/videoplayback.m4a
但是,当我关闭应用程序并在下一个会话中尝试使用相同的URL时,即当再次打开应用程序时,它就是不起作用。当我检查容器时,它显示文件仍然存在
每个会话都是唯一的,您必须像这样获得它
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)
}
您可以使用最常用的方法,即使用
UIDocumentInteractionController
和UIAlertController
检查使用progressview的流行Alamofire
的这部分代码。注意:如果您在控制器中使用它,它必须实现following委托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
}
}
请尝试从路径中删除“file://”。谢谢您的时间,但我的问题与其他问题有关,在上述解决方案的帮助下,它现在运行良好。还是谢谢你的关心。我后来看到:)祝你今天愉快;)
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
}
}