I am trying to configure the boot using NSURLSession , which will continue in the background. I have a singleton class called DownloadManager that creates an NSURLSession and runs the download task as follows:
- (id)init { self = [super init]; if (self) { self.queue = [[NSOperationQueue alloc] init]; self.queue.maxConcurrentOperationCount = 1; // Initialize the background session. self.session = [self backgroundSession]; } return self; } - (NSURLSession *)backgroundSession { static NSURLSession *session = nil; static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ NSURLSessionConfiguration *sessionConfiguration = [NSURLSessionConfiguration backgroundSessionConfiguration:@"com.mycompany.myapp.BackgroundSession"]; session = [NSURLSession sessionWithConfiguration:sessionConfiguration delegate:self delegateQueue:self.queue]; }); return session; } - (void)startDownload:(Download *)download { NSURL *remoteURL = ... NSURLSessionDownloadTask *task = [self.session downloadTaskWithURL:remoteURL]; [task resume]; }
I implemented the NSURLSessionDelegate and NSURLSessionDownloadDelegate , including URLSessionDidFinishEventsForBackgroundURLSession: In addition, my application delegate implements application:handleEventsForBackgroundURLSession:completionHandler:
However, when I move my application to the background by pressing the home button after starting the download, all delegate methods loaded stop shooting, and application:handleEventsForBackgroundURLSession:completionHandler: never called. Downloading continues, and if I wait long enough to finish, then URLSession:downloadTask:didFinishDownloadingToURL: is the moment when I bring my application back to the forefront. This means that I can not do post-processing in the background (for example, save kernel data, start a new boot, etc.).
I tried adding the "Background fetch" background to my plist in case it was necessary, and I tried changing my identifier used to create the NSURLSessionConfiguration background configuration as suggested in this answer . Did I make a mistake in configuring this, or should I not handle sending delegate events in the background?
ios objective-c cocoa-touch ios7 nsurlsession
jjoelson
source share