I have a DownloadSessionDelegate Class to handle a downloadprocess for big files. I would like to show the progress in a progressview. The Information about the download state is in my DownloadSessionDelegate Class. Now I don't know howto update my progressview outside of that class.
Howto to do that ?
class DownloadSessionDelegate : NSObject, NSURLSessionDelegate, NSURLSessionDownloadDelegate {
var handlerQueue: [String : CompleteHandlerBlock]!
...
...
...
func URLSession(session: NSURLSession, downloadTask: NSURLSessionDownloadTask, didWriteData bytesWritten: Int64, totalBytesWritten: Int64, totalBytesExpectedToWrite: Int64) {
println("session \(session) download task \(downloadTask) wrote an additional \(bytesWritten) bytes (total \(totalBytesWritten) bytes) out of an expected \(totalBytesExpectedToWrite) bytes.")
progressView.setProgress(0.5, animated: true); // <<<Howto reference the progressView
}
}
Triggering the download from my ViewController.swift:
func download_zip(sURL: String, sToLocation: String) {
let progressView = UIProgressView(progressViewStyle: .Bar);
progressView.center = view.center;
progressView.progress = 1/2;
progressView.trackTintColor = UIColor.lightGrayColor();
progressView.tintColor=UIColor.blueColor();
view.addSubview(progressView);
var delegate = DownloadSessionDelegate.sharedInstance;
delegate.storePath=sToLocation;
struct SessionProperties {
static let identifier : String! = "url_session_background_download"
}
var configuration = NSURLSessionConfiguration.backgroundSessionConfigurationWithIdentifier(SessionProperties.identifier)
var backgroundSession = NSURLSession(configuration: configuration, delegate: delegate, delegateQueue: nil)
var url = NSURLRequest(URL: NSURL(string: sURL)!)
var downloadTask = backgroundSession.downloadTaskWithRequest(url)
downloadTask.resume()
}