Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSURLSession URLSessionDidFinishEventsForBackgroundURLSession handler never called ios 8. OK in ios 7

I have NSURLSession that is downloading multiple files. I'm updating an ios 7 application for ios 8. It works fine in ios 7, but when complied against ios 8 The delegate meathod

URLSessionDidFinishEventsForBackgroundURLSession: 

is never called.

I dug a little deeper and on the

-(void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location

and

-(void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error

delegate meathods I am checking the session for the remaining download tasks:

[self.session getTasksWithCompletionHandler:^(NSArray *dataTasks, NSArray *uploadTasks, NSArray *downloadTasks){ ...}

On the last delegate meathod called there is always one element remaning in the downloadTasks array. What is strange is that this download task is not always referencing the same file, and the status on that tasks shows that the download is complete.

Again, works fine in ios7. Problem occurs in ios 8 only.

like image 803
nwales Avatar asked Sep 16 '14 15:09

nwales


1 Answers

I presume you are testing on the iOS Simulator. If yes, another bug is responsible for this misbehavior. iOS8 Simulator never truly backgrounds apps, hence the completion handler won't trigger.

Try running on the device and it will work.

You can find a full discussion in the Apple forums or on this blog post

like image 193
Krumelur Avatar answered Oct 13 '22 00:10

Krumelur