在 swift 3 中下载文件时,alamo fire 中的进度视图不会更新
Posted
技术标签:
【中文标题】在 swift 3 中下载文件时,alamo fire 中的进度视图不会更新【英文标题】:progress view in alamo fire will not update when downloading file in swift 3 【发布时间】:2017-09-06 08:39:03 【问题描述】:我想下载一个带有 alamo fire 的文件并使用带有进度的警报来显示它,但是当 alamo fire progress.fractionCompleted 增加时进度不会移动我使用静态 var 等于 progres.fractionCompleted 但它也不起作用 这是我的代码
let destination = DownloadRequest.suggestedDownloadDestination()
Alamofire.download("example.com", to: destination).downloadProgress(queue: DispatchQueue.global(qos: .utility)) (progress) in
print("Progress: \(progress.fractionCompleted)")
sixthLevelViewController.progressdownload = Float(progress.fractionCompleted)
DispatchQueue.main.async
let alertController = UIAlertController(title: "Downloading", message: "please wait", preferredStyle: .alert)
let progressDownload : UIProgressView = UIProgressView(progressViewStyle: .default)
progressDownload.setProgress(ViewController.progressdownload, animated: true)
progressDownload.frame = CGRect(x: 10, y: 70, width: 250, height: 0)
alertController.view.addSubview(progressDownload)
self.present(alertController, animated: true, completion: nil)
.validate().responseData ( response ) in
print(response.destinationURL!.lastPathComponent)
**请记住,这会很好,问题只是更新进度视图**
【问题讨论】:
【参考方案1】:我认为你的代码是错误的:根据latestAlamofire 文档,我举了一个例子来说明如何修改你的代码:
let destination: DownloadRequest.DownloadFileDestination = _, _ in
let documentsPath = NSSearchPathForDirectoriesInDomains(.documentDirectory,
.userDomainMask, true)[0]
let documentsURL = URL(fileURLWithPath: documentsPath, isDirectory: true)
let fileURL = documentsURL.appendingPathComponent("myfile.pdf")
return (fileURL, [.removePreviousFile, .createIntermediateDirectories])
func requestDownloadInfo(url:String)
Alamofire.download(url, to :destination)
.downloadProgress(queue: utilityQueue) progress in
let progressDic = ["progress" : progress.completedUnitCount, "total" : progress.totalUnitCount, "fraction" : progress.fractionCompleted] as [String:Any]
// here you can show your alert using fraction value..
.responseData response in
// check your file after download or check errors...
【讨论】:
swift 不允许我运行应用程序,因为 RetunzAPIProgress 好吧忘了这个我使用了另一个代码但是还有另一个问题请帮助我解决这个问题之后我会自己回答这个问题这里是那个问题的链接***.com/questions/46137799/…跨度>以上是关于在 swift 3 中下载文件时,alamo fire 中的进度视图不会更新的主要内容,如果未能解决你的问题,请参考以下文章