I am using Alamofire for my webServices and Alamofire download method for downloading pdf files. Everything is working fine data is coming & files also properly downloading. Only the issue is how can I set progress bar for this?
I am using tableView with custom Xib (which has progressBar).
- I want to set one progressBar per cell for all pdf files download and data complete like 100%.
- In case of Halt due to any reason it also shows progress 10% done and stopped. (resume, cancel) only.
Note: I m using static value for printing cell in tableView
ViewController code:
import UIKit
import Alamofire
class ViewController: UIViewController {
@IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
tableView.dataSource = self
tableView.delegate = self
let nib = UINib.init(nibName: "DownloadEntryViewCell", bundle: nil)
self.tableView.register(nib, forCellReuseIdentifier: "DownloadEntryViewCell")
}
func webService() {
DataProvider.main.serviceGetAppointmentDetail(Id: AppId ?? 0, callback: {success, result in
do{
if(success){
let decoder = JSONDecoder()
let response = try decoder.decode(AppointmentDetail.self, from: result! as! Data)
self.AppDetailData = response
for firmParam in (self.AppDetailData?.sectionList ?? []) {
for firmItem in firmParam.items! {
if firmItem.actionParamData != nil {
let str = firmItem.actionParamData
let param = str?.components(separatedBy: ":")
let final = param![1].replacingOccurrences(of: "}", with: "")
let fmId = final.components(separatedBy: ",")
let frmId = fmId[0]
self.firmDetails(actionParamData: Int(frmId) ?? 0)
}
//pdf download
if firmItem.actionUrl != nil {
let pdfFilesURL = firmItem.actionUrl ?? ""
self.downloadFile(url: pdfFilesURL, filetype: ".pdf", callback: { success, response in
if !success || response == nil {
return false
}
return true
})
}
}
}
return true
}else{
return false
}
}catch let error {
print(error as Any)
return false
}
})
}
@objc public func downloadFile(url:String, filetype: String, callback:@escaping (_ success:Bool, _ result:Any?)->(Bool)) -> Void {
var destination = DownloadRequest.suggestedDownloadDestination(for: .documentDirectory)
if filetype.elementsEqual(".pdf"){
destination = { _, _ in
let documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
let downloadFileName = url.filName()
let fileURL = documentsURL.appendingPathComponent("\(downloadFileName).pdf")
return (fileURL, [.removePreviousFile, .createIntermediateDirectories])
}
}
Alamofire.download(
url,
method: .get,
parameters: nil,
encoding: JSONEncoding.default,
headers: nil,
to: destination).downloadProgress(closure: { (progress) in
print(progress)
print(progress.fractionCompleted)
}).response(completionHandler: { (DefaultDownloadResponse) in
callback(DefaultDownloadResponse.response?.statusCode == 200, DefaultDownloadResponse.destinationURL?.path)
print(DefaultDownloadResponse)
})
}
}
extension ViewController: UITableViewDataSource, UITableViewDelegate {
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 78
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "DownloadEntryViewCell", for: indexPath) as! DownloadEntryViewCell
return cell
}
}
Xib code:
class DownloadEntryViewCell: UITableViewCell {
@IBOutlet weak var rightView: UIView!
@IBOutlet weak var leftView: UIView!
@IBOutlet weak var fileNameLabel: UILabel!
@IBOutlet weak var downloadURLLabel: UILabel!
@IBOutlet weak var progressLabel: UILabel!
@IBOutlet weak var individualProgress: UIProgressView!
@IBOutlet weak var imgView: UIImageView!
@IBOutlet weak var stackViewFooter: UIStackView!
override func awakeFromNib() {
super.awakeFromNib()
}
}