繁体   English   中英

iOS、Swift:连续下载多个文件并将所有文件的单个进度条显示为一个进度

[英]iOS, Swift : Download multiple file serially and showing single progress bar for all file as a one progress

我正在我的 ios(swift4) 应用程序中使用 Alamofire 下载 zip 文件。 我可以使用 Alamofire 串行下载文件。 但我也想为所有下载的文件显示一个进度条。 意思是如果我有 4 个 zip 文件,并且当所有文件都下载后,那么进度应该是 100%。

直到我尝试了下面的代码,它为每个 url 提供了一个进度值,进度显示为一个下载的文件,显示进度为 100% 然后再次从 0 开始第二个 url,当第二个 url 下载时,进度显示为 100 % 完全的。

请指导我。 当使用 Alamofire 下载所有文件时,我希望获得 100% 的进度值。

Alamofire 可以吗?

代码:

func serialZipFileDownload(downloadPath: String){
    let documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
    let name = self.offlineDownloadFileName?[urlCount]
    let currentVideoURL = documentsURL.appendingPathComponent(name ?? "Default.zip")
    let str = downloadPath
    let urlString = str.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)
    let url = URL(string: urlString ?? "")

if super.isConnectedToNetwork() == true {
    let manager = Alamofire.SessionManager.default
    let headers = ["Accept-Encoding" : ""]

    manager.request(url ?? "", method: .get, parameters: nil, encoding: JSONEncoding.default, headers: headers).downloadProgress { (progress) in
        print(progress.fractionCompleted)

        DispatchQueue.main.async{
            self.progressDownload.setProgress((Float(progress.fractionCompleted)), animated: true)
            let per = round((Float(progress.fractionCompleted)) * 100)
            self.lblDownloadPercent.text = "\(Int(per))%"
        }
    }.responseData { (response) in
        switch (response.result){
        case .success(_) :
            print(response)
            print(response.result.value!)
            print(response.result.description)

            if let data = response.result.value {
                do {
                    try data.write(to: currentVideoURL)
                    self.showToast(message: "File downloaded successfully")
                }
                catch {
                    print("Something went wrong!")
                }
            }
        case .failure(let error) :
            print(response)
            if error._code == NSURLErrorNetworkConnectionLost {
                DispatchQueue.main.async {
                    super.showPopup(title: msgStruct.networkTitle, message: msgStruct.noInternet)
                }
            }
            else if error._code == NSURLErrorTimedOut {
                DispatchQueue.main.async {
                    super.showPopup(title: msgStruct.networkTitle, message: msgStruct.noInternet)
                }
            }
            else if error._code == NSURLErrorDownloadDecodingFailedMidStream {
                print("error",error.localizedDescription)
            }
            break
        }
    }
}
else{
    super.showPopup(title: msgStruct.networkTitle, message: msgStruct.noInternet)
}

您可以这样做:维护一个全局变量 - tatalPercentage其他一些 swift 类。

  static let tatalPercentage = 0

tatalPercentage = tatalPercentage + Int(per/4)
self.lblDownloadPercent.text = "\(tatalPercentage) %"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM