7
votes

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.

2
I can confirm this problem. There's always one task around and that breaks the implementation. Problem is also being discussed over at Apple's forums: devforums.apple.com/message/1047695#1047695 - seems like bugs have been filed: devforums.apple.com/message/1021734#1021734Krumelur

2 Answers

2
votes

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

-1
votes

Make sure you call

 if ([UIApplication instancesRespondToSelector:@selector(registerUserNotificationSettings:)]){
        [application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound categories:nil]];
    }

in your

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

This should fix it on the device but the simulator has a bug that does not allow it to work.