在swift 3中下载文件时,alamo fire中的进度视图不会更新

时间:2017-09-06 08:39:04

标签: swift3 download alamofire uialertcontroller uiprogressview

我想下载一个带有alamo fire的文件并使用带有进度的alert来显示它但是当alamo fire progress.fractionCompleted增加时进度将不会移动我使用static 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 个答案:

答案 0 :(得分:0)

我认为您的代码错误:根据latest Alamofire文档,我举例说明了如何修改代码:

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...
                }
}