I want to upload a list of files using NSUrlSession.
I have a variable for counting successful downloads @property (nonatomic) int downloadsSuccessfulCounter; . When downloading files, the Download Button disabled. When the counter is equal to the size of the download list, I turn on the button again and set the counter to 0. I do this in the method:
-(void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location { ... [[NSOperationQueue mainQueue] addOperationWithBlock:^ { downloadsSuccessfulCounter++; if(downloadsSuccessfulCounter == self.downloadList.count) { NSLog(@"All downloads finished"); [self.syncButton setEnabled:YES]; downloadsSuccessfulCounter = 0; } }];
}
Everything works fine, but when I open the ViewController again, I get the message A background URLSession with identifier com.myApp already exists! . The counter is not set to 0, and user interface elements (UIButtons, UILabels) do not respond.
I assume the problem is that NSURLSession is still open, but I'm not sure how this works.
I tried all the tutorials, but 99% of them are only for downloading 1 file, not more than 1 ... Any ideas?
Here is my code:
... @property (nonatomic, strong) NSURLSession *session; ... - (void)viewDidLoad { [super viewDidLoad]; appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate]; self.downloadList = [[NSMutableArray alloc] init]; NSURLSessionConfiguration *sessionConfiguration = [NSURLSessionConfiguration backgroundSessionConfiguration:@"com.myApp"]; sessionConfiguration.HTTPMaximumConnectionsPerHost = 5; self.session = [NSURLSession sessionWithConfiguration:sessionConfiguration delegate:self delegateQueue:nil]; }
When I click the Download Button , I call this method (I have a Downloadable object that contains NSURLSessionDownloadTask ):
-(void)startDownload { for (int i=0; i<[self.downloadList count]; i++) { Downloadable *d = [self.downloadList objectAtIndex:i]; if (!d.isDownloading) { if (d.taskIdentifier == -1) { d.downloadTask = [self.session downloadTaskWithURL:[NSURL URLWithString:d.downloadSource]]; }else { d.downloadTask = [self.session downloadTaskWithResumeData:fdi.taskResumeData]; } d.taskIdentifier = d.downloadTask.taskIdentifier; [d.downloadTask resume]; d.isDownloading = YES; } } }
When the application is in the background:
-(void)URLSessionDidFinishEventsForBackgroundURLSession:(NSURLSession *)session{ AppDelegate *appDelegate = [UIApplication sharedApplication].delegate; [self.session getTasksWithCompletionHandler:^(NSArray *dataTasks, NSArray *uploadTasks, NSArray *downloadTasks) { if ([downloadTasks count] == 0) { if (appDelegate.backgroundTransferCompletionHandler != nil) { void(^completionHandler)() = appDelegate.backgroundTransferCompletionHandler; appDelegate.backgroundTransferCompletionHandler = nil; [[NSOperationQueue mainQueue] addOperationWithBlock:^{ completionHandler(); UILocalNotification *localNotification = [[UILocalNotification alloc] init]; localNotification.alertBody = @"All files downloaded"; [[UIApplication sharedApplication] presentLocalNotificationNow:localNotification]; }]; } } }]; }