1

Context: I try to call a create a task (download or upload) from an action extension, with a backgroundSessionConfiguration.

To do this I fallow the exemple in apple documention

-(void)downloadTest
{
    NSURLSession *mySession = [self configureMySession];
    NSURL *url = [NSURL URLWithString:@"http://www.sellcell.com/blog/wp-content/uploads/2014/03/dog-apps.jpg"];
    NSURLSessionTask *myTask = [mySession downloadTaskWithURL:url];
    [myTask resume];
}

- (NSURLSession *) configureMySession {
    if (!_mySession) {
        NSURLSessionConfiguration* config = [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:@"com.mycompany.myapp.backgroundsession"];
        // To access the shared container you set up, use the sharedContainerIdentifier property on your configuration object.
        config.sharedContainerIdentifier = @"group.com.mycompany.appname"; 
        _mySession = [NSURLSession sessionWithConfiguration:config delegate:self delegateQueue:nil];
    }
    return _mySession;
}

My problem is that when I call [mySession downloadTaskWithURL:url]; it returns nil.

If I change the configuration to NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration]; then a task is created.

I don't see what I'm doing wrong , I have created an app group and I use it the app and in the extension.

I use the group name that I have created in config.sharedContainerIdentifier but I'm not sure it's necessary.

NOTE: I have the same problem with uploadTask.

Hikosei
  • 193
  • 1
  • 11

1 Answers1

0
  1. Are you using ARC? If not, make sure your session is being retained properly. (It looks like you're using an ivar directly.)

  2. Is that shared container identifier correct? If the container isn't in your entitlements or doesn't exist yet, your session will be immediately invalidated. Add an invalidation delegate method and see if it is getting called. If so, that's probably the issue.

dgatwood
  • 10,129
  • 1
  • 28
  • 49