NSURLSession URLSessionDidFinishEventsForBackgroundURLSession handler never called ios 8. OK in ios 7
Asked Answered
R

2

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.

Rotterdam answered 16/9, 2014 at 15:25 Comment(1)
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#1021734Mcatee
M
2

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

Mcatee answered 24/9, 2014 at 9:56 Comment(2)
It does not work when running on the simulator or the device. I'm compiling my app against ios 7 in xcode 5.1 and installing on an ios 8 device.Rotterdam
@Rotterdam What exactly are you doing? The handler will only trigger if the app is backgrounded, is still running or was terminated by iOS. If you terminate it manually from the task switcher, it will not fire.Mcatee
U
-1

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.

Ultraconservative answered 12/2, 2015 at 21:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.