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.
From the docs:
And:
Refer to Sharing Data with Your Containing App for guidance on setting up a shared container.
In your example, you'd add something like:
You'll need one background session for the containing app and one for its extension.
Make sure your sharedContainerIdentifier is the same identifier as the group identifier you registered for both your host app and extension app.
You can register a group identifier in Xcode capabilities project tab.