Reputation: 563
I now try to implement background file download in my ios app, and i'm a little bit confused... I have got a lot of different views in my app, and i want to start downloading files from many of them, and display progress in the view been presented. The first part of my aim (downloading files) was implemented by creating one NSURLSession in app delegate, so i can get it in any view and start downloading a file.
In AppDelegat
NSURLSessionConfiguration *sessionConfiguration = [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier: @"com.myEnglishLessons"];
sessionConfiguration.HTTPMaximumConnectionsPerHost = 111;
self.currentSession = [NSURLSession sessionWithConfiguration:sessionConfiguration
delegate:nil
delegateQueue:nil];
In some view
NSURLSessionDataTask *downloadTask = [[(AppDelegate *)[[UIApplication sharedApplication] delegate] currentSession] downloadTaskWithURL: myURL];
[downloadTask resume];
That all works good, but what's the problem - i can't handle and show progress in the view. Delegate of NSURLSession is appDelegate, so it receives events of downloading process, but not the class of the view. What can you suggest me?
Upvotes: 0
Views: 206
Reputation: 803
You can use downloadTask.taskDescription
to set the view id or view class name. So when you create the task from the view you could write like:
NSURLSessionDataTask *downloadTask = [[(AppDelegate *)[[UIApplication sharedApplication] delegate] currentSession] downloadTaskWithURL: myURL];
[downloadTask setTaskDescription:NSStringFromClass([self class])];
[downloadTask resume];
Then you can send the notification in progress URLSession method:
CGFloat progress = (CGFloat)totalBytesWritten/totalBytesExpectedToWrite;
[[NSNotificationCenter defaultCenter] postNotificationName:kDownloadTaskProgressNotification object:nil
userInfo:@{@"progress": @(progress), @"viewClass": downloadTask.taskDescription];
and all views should observe this notification. also you need to add a class check when you get the notification.
Upvotes: 1