问题描述
我目前正在开发iOS 8 App扩展程序,并且在最后一篇文章中遇到了困难。在我的应用程序的其余部分,我使用AFHTTPSessionManager子类,我实例化如下:
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;
}
当我刚刚使用这个常规API客户端时,只需在共享中发布一些文本扩展工作正常,它甚至有时适用于图像(虽然通常会失败),但我知道我需要使用后台会话配置。所以我用这样的后台配置设置了一个非常相似的api客户端:
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;
}
问题是,当我使用这个客户端进行POST时,我得到了这些每次都是错误的。
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.
推荐答案
来自:
并且:
请参阅,以获取有关设置共享容器的指导。
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.
这篇关于用于iOS 8扩展的AFNetworking后台会话配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!