I can successfully download a zip file in background with my app code with ios 10 and swift 3.
I use a backgroundsession data task:
let backgroundSessionConfiguration = URLSessionConfiguration.background(withIdentifier: (prefix + postfix))
let backgroundSession = Foundation.URLSession(configuration: backgroundSessionConfiguration, delegate: self, delegateQueue: OperationQueue.main)
var request = URLRequest(url: dlUrl )
request.httpMethod = "GET"
request.cachePolicy = NSMutableURLRequest.CachePolicy.reloadIgnoringCacheData
let task = backgroundSession.downloadTask(with: request)
task.resume()
But I wonder what is the best practice to pass some specific metadata to the delegate method after the download finished:
func urlSession(_ session: URLSession,
downloadTask: URLSessionDownloadTask,
didFinishDownloadingTo location: URL){
I need inside this delegate some information about that download which depends e.g. on a choice a user made earlier. I am not able to get this data from the server or the url.
So I would like to have a variable or dictionary downloadType telling me what to do with the finished download, to have something like this in the delegate
if downloadType == "normal" ... do this
else ... do that
I am not sure if I can safely use properties of the class implementing the URLSessionDownloadDelegate protocol while the App was suspend? Will the class instance itself be restored when the download finishes while the app is suspended or will ios create a new instance of the class?
So will this always work:
class ServerCom: NSObject, URLSessionDownloadDelegate {
var updateTypeStr = ""
[...]
func somePublic(setUpdateType: String) {
self.updateTypeStr = setUpdateType
[...]
func urlSession(_ session: URLSession,
downloadTask: URLSessionDownloadTask,
didFinishDownloadingTo location: URL){
[...]
if( self.updateTypeStr == "normal" ) { // do this
else { // do that
Are there some better ways for that use case?
thanks in advance!