I have progress view in alert when downloading with alamofire, but the problem is that when I want to download the progress view in alert will start to be complete but flashing and the other problem is that the behind screen will start to be fade to black I think in every second a new alert with progress view will show and at the end there are lots of alerts how can I avoid them?
downloadTimer = Timer.scheduledTimer(timeInterval: 1 , target: self, selector: #selector(flashingDownload), userInfo: nil, repeats: true)
let destination = DownloadRequest.suggestedDownloadDestination()
Alamofire.download("http://example.com", to: destination).downloadProgress(queue: DispatchQueue.global(qos: .utility)) { (progress) in
print("Progress: \(progress.fractionCompleted)")
sixthLevelViewController.progressdownload = Float(progress.fractionCompleted)
if sixthLevelViewController.progressdownload == 1.0 {
self.downloadfinished = true
let alertController = UIAlertController(title: "finish download ", message: " download Completed! ", preferredStyle: UIAlertControllerStyle.alert)
let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.default)
{
(result : UIAlertAction) -> Void in
self.view.isUserInteractionEnabled = true
}
alertController.addAction(okAction)
self.present(alertController, animated: true, completion: nil)
alertController.view.tintColor = UIColor.green
alertController.view.backgroundColor = UIColor.green
alertController.view.layer.cornerRadius = 0.1 * alertController.view.bounds.size.width
}
} .validate().responseData { ( response ) in
print(response.destinationURL!.lastPathComponent)
}
and here is the codes for timer
func flashingDownload() {
while (topViewController.presentedViewController != nil){
topViewController = topViewController.presentedViewController!
}
DispatchQueue.main.async {
let alert = UIAlertController(title: "downloading", message: "pls wait", preferredStyle: .alert)
let progressBar = UIProgressView(progressViewStyle: .default)
progressBar.setProgress(sixthLevelViewController.progressdownload, animated: true)
progressBar.frame = CGRect(x: 10, y: 70, width: 250, height: 0)
alert.view.addSubview(progressBar)
self.topViewController.present(alert, animated: false, completion: nil)
if sixthLevelViewController.progressdownload == 1.0 {
print("finished download !")
self.topViewController.dismiss(animated: false , completion: nil)
self.downloadTimer.invalidate()
}
}
as you see in my codes I have another variable that controls the download finish so when progress is 1.0 I want to dismiss alert