How can I link a file download with a Progress View

前端 未结 2 585
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-03 00:18

My button code below download a file from a URL, I need to link it with a Progress View to show the Downloading Progress.

@IBAction func btnStream(sender: U         


        
相关标签:
2条回答
  • 2021-01-03 00:36

    Here is complete working example for you:

    import UIKit
    
    class ViewController: UIViewController, NSURLSessionDownloadDelegate {
    
    
        @IBOutlet weak var progressBar: UIProgressView!
        @IBOutlet weak var progressCount: UILabel!
    
        var task : NSURLSessionTask!
    
        var percentageWritten:Float = 0.0
        var taskTotalBytesWritten = 0
        var taskTotalBytesExpectedToWrite = 0
    
        lazy var session : NSURLSession = {
            let config = NSURLSessionConfiguration.ephemeralSessionConfiguration()
            config.allowsCellularAccess = false
            let session = NSURLSession(configuration: config, delegate: self, delegateQueue: NSOperationQueue.mainQueue())
            return session
            }()
    
        override func viewDidLoad() {
            progressBar.setProgress(0.0, animated: true)  //set progressBar to 0 at start
        }
    
        @IBAction func doElaborateHTTP (sender:AnyObject!) {
    
            progressCount.text = "0%"
            if self.task != nil {
                return
            }
    
            let s = "http://www.qdtricks.com/wp-content/uploads/2015/02/hd-wallpapers-1080p-for-mobile.png"
            let url = NSURL(string:s)!
            let req = NSMutableURLRequest(URL:url)
            let task = self.session.downloadTaskWithRequest(req)
            self.task = task
            task.resume()
    
        }
    
        func URLSession(session: NSURLSession, downloadTask: NSURLSessionDownloadTask, didWriteData bytesWritten: Int64, totalBytesWritten writ: Int64, totalBytesExpectedToWrite exp: Int64) {
            println("downloaded \(100*writ/exp)")
            taskTotalBytesWritten = Int(writ)
            taskTotalBytesExpectedToWrite = Int(exp)
            percentageWritten = Float(taskTotalBytesWritten) / Float(taskTotalBytesExpectedToWrite)
            progressBar.progress = percentageWritten
            progressCount.text = String(format: "%.01f", percentageWritten*100) + "%"
        }
    
        func URLSession(session: NSURLSession, downloadTask: NSURLSessionDownloadTask, didResumeAtOffset fileOffset: Int64, expectedTotalBytes: Int64) {
            // unused in this example
        }
    
        func URLSession(session: NSURLSession, task: NSURLSessionTask, didCompleteWithError error: NSError?) {
            println("completed: error: \(error)")
        }
    
        // this is the only required NSURLSessionDownloadDelegate method
    
        func URLSession(session: NSURLSession, downloadTask: NSURLSessionDownloadTask, didFinishDownloadingToURL location: NSURL) {
    
            let documentsDirectoryURL =  NSFileManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask).first as! NSURL
            println("Finished downloading!")
            println(documentsDirectoryURL)
            var err:NSError?
    
            // Here you can move your downloaded file
            if NSFileManager().moveItemAtURL(location, toURL: documentsDirectoryURL.URLByAppendingPathComponent(downloadTask.response!.suggestedFilename!), error: &err) {
                println("File saved")
            } else {
                if let err = err {
                    println("File not saved.\n\(err.description)")
    
                }
            }
    
        }
    
    }
    

    You can use NSURLSessionDownloadDelegate to achieve this whose method will be called when user downloading data.

    This will show you the process into progressCount label and the progressBar will show process as count will increment. you can modify this as per your need.

    You can download this example from HERE.

    0 讨论(0)
  • 2021-01-03 00:45

    Check this tutorial. It's in Objective-C, but it will be easy to convert to Swift.

    The principle is to implement some NSURLConnectionDataDelegatefunctions on your VC :

    • connection:didReceiveResponse -> You can retrieve the size of the file that will be downloaded and estimate the download percentage with it.
    • connection:didReceiveData -> It's the refresh function, it will be called multiple times during the download. You can compare the size of your incomplete NSData object with the size of the file.
    • connectionDidFinishLoading -> This method is called at the end of the download process.

    Hope it helps, and don't hesitate to comment if you have some troubles converting Obj-C to Swift.

    0 讨论(0)
提交回复
热议问题