简体   繁体   中英

Swift alert popup progress bar

I'm trying to implement an alert message showing a progress bar during a download.

I found this code:

let alertView = UIAlertController(title: "Please wait", message: "Need to download some files.", preferredStyle: .Alert)
alertView.addAction(UIAlertAction(title: "Cancel", style: .Cancel, handler: nil))


presentViewController(alertView, animated: true, completion: {
//  Add your progressbar after alert is shown (and measured)
let margin:CGFloat = 8.0
let rect = CGRectMake(margin, 72.0, alertView.view.frame.width - margin * 2.0 , 2.0)
let progressView = UIProgressView(frame: rect)
progressView.progress = 0.5
progressView.tintColor = UIColor.blueColor()
alertView.view.addSubview(progressView)
})

But I don't know how to update the progress (progressView.progress) during processing and more important how to exit from this alert when download is finished.

Any help will be appreciated!

I'm sure there's a better answer to the progress bar... But my solution is to know how may items must be loaded (ie some variable "total_item), increment the number completed (numDownloaded) and calculate the percentage based on how many have loaded, then update that percentage.

Dismissing the UIAlertController is easy. Simply add:

let okAction = UIAlertAction(title: "Okay", style: UIAlertActionStyle.default, handler: {action in self.dismiss(animated: true, completion: nil)})
alertView.addAction(okAction)

This will add an "Okay" button, which will dismiss the view controller. Perhaps you can add a closure which only enables the "Okay" UIAlertAction when the load is complete.

At the moment it works for me like this in the code below. The alert view and progress view I declared as vars with global access, that way I could change the progress as the download goes on, I dismiss the alert after the download task completes.

I am implementing the following:

  • URLSessionDelegate
  • URLSessionTaskDelegate
  • URLSessionDownloadDelegate

For more information, posted sample code below:

var globalAlert: UIAlertController! //with global access
var globalProgressView: UIProgressView! //with global access

//flags
var downloading: Bool = false //true if we have a download in progress
var globalAlertShowing = false //true if global alert is showing

func urlSession(_ session: URLSession,
                downloadTask: URLSessionDownloadTask,
                didWriteData bytesWritten: Int64,
                totalBytesWritten: Int64,
                totalBytesExpectedToWrite: Int64){

    if totalBytesExpectedToWrite > 0 {
        let progress = Float(totalBytesWritten) / Float(totalBytesExpectedToWrite)
        print("Progress (ByteCountFormatter.string(fromByteCount: progress, countStyle: ByteCountFormatter.CountStyle.decimal))")
        var humanReadableFileSize: String = ByteCountFormatter.string(fromByteCount: Int64(totalBytesWritten), countStyle: ByteCountFormatter.CountStyle.decimal)
        print("total byte written: \(humanReadableFileSize)")

        if(globalAlertShowing){
            DispatchQueue.main.async {
                self.globalAlert.message = "\(humanReadableFileSize)"
                self.globalProgressView.progress = progress
            }
        }


    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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