AFNetworking Background Session Configuration for

2019-03-19 04:03发布

问题:

I'm currently developing an iOS 8 App Extension, and am having difficulty with this one last piece. In the rest of my app, I use an AFHTTPSessionManager subclass that I instantiate like this:

+ (MYAPIClient *)sharedClient {
    static MYAPIClient *_sharedClient = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _sharedClient = [[MYAPIClient alloc] initWithBaseURL:[NSURL URLWithString:kMYBaseURL]];
        _sharedClient.requestSerializer = [[MYAPIRequestSerializer alloc] init];
        _sharedClient.responseSerializer = [[MYAPIResponseSerializer alloc] init];
    });
    return _sharedClient;
}

When I just used this regular API client, just posting some text form a share extension worked fine, and it even works for images sometimes (usually fails though), but I know that I need to be using a background session configuration. So I made a very similar api client with a background configuration setup like this:

+ (MYAPIClient *)sharedBackgroundClient {
    static MYAPIClient *_sharedClient = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{

        NSURLSessionConfiguration *sessionConfiguration = [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:@"com.me.myapp.backgroundconfiguration"];
        _sharedClient = [[MYAPIClient alloc] initWithBaseURL:[NSURL URLWithString:kMYBaseURL] sessionConfiguration:sessionConfiguration];
        _sharedClient.requestSerializer = [[MYAPIRequestSerializer alloc] init];
        _sharedClient.responseSerializer = [[MYAPIResponseSerializer alloc] init];
    });
    return _sharedClient;
}

The problem is, when I make my POST using this client, I get these erros every single time.

Aug 21 19:19:07 MY-iPhone Share[6290] <Notice>: Attempted to create a task in a session that has been invalidated
Aug 21 19:19:07 MY-iPhone Share[6290] <Warning>: *** Assertion failure in -[MYAPIClient setDelegate:forTask:], /Users/me/Documents/myproject/myproduct/Pods/AFNetworking/AFNetworking/AFURLSessionManager.m:337
Aug 21 19:19:07 MY-iPhone Share[6290] <Error>: *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid parameter not satisfying: task' 

Any advice on how to get this to work? Thanks a lot.

回答1:

From the docs:

If your app extension initiates a background NSURLSession task, you must also set up a shared container that both the extension and its containing app can access. Use the sharedContainerIdentifier property of the NSURLSessionConfiguration class to specify an identifier for the shared container so that you can access it later.

And:

If you attempt to use create a URL session from your app extension but fail to set this property to a valid value, the URL session is invalidated upon creation.

Refer to Sharing Data with Your Containing App for guidance on setting up a shared container.

In your example, you'd add something like:

sessionConfiguration.sharedContainerIdentifier = @“com.me.myapp.containerIdentifier”;

You'll need one background session for the containing app and one for its extension.



回答2:

Make sure your sharedContainerIdentifier is the same identifier as the group identifier you registered for both your host app and extension app.

config.sharedContainerIdentifier = @“com.mycompany.myappgroupidentifier”;

You can register a group identifier in Xcode capabilities project tab.