我目前正在开发iOS 8 App扩展程序,并且最后一篇文章遇到了困难.在我的应用程序的其余部分,我使用AFHTTPSessionManager子类,我实例化如下:
+ (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客户端:
+ (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时,我每次都会得到这些错误.
Aug 21 19:19:07 MY-iPhone Share[6290]: Attempted to create a task in a session that has been invalidated Aug 21 19:19:07 MY-iPhone Share[6290] : *** 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] : *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid parameter not satisfying: task'
有关如何使其工作的任何建议?非常感谢.