Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
iOS 8扩展的AFNetworking后台会话配置_Ios_Objective C_Afnetworking_Ios8_Nsurlsession - Fatal编程技术网

iOS 8扩展的AFNetworking后台会话配置

iOS 8扩展的AFNetworking后台会话配置,ios,objective-c,afnetworking,ios8,nsurlsession,Ios,Objective C,Afnetworking,Ios8,Nsurlsession,我目前正在开发一个iOS 8应用程序扩展,在这最后一个方面我遇到了困难。在我的应用程序的其余部分中,我使用了一个AFHTTPSessionManager子类,我将其实例化如下: + (MYAPIClient *)sharedClient { static MYAPIClient *_sharedClient = nil; static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ _

我目前正在开发一个iOS 8应用程序扩展,在这最后一个方面我遇到了困难。在我的应用程序的其余部分中,我使用了一个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;
}
+ (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;
}
当我刚刚使用这个常规API客户端时,只需将一些文本表单发布到共享扩展就可以了,它有时甚至可以用于图像(虽然通常会失败),但我知道我需要使用后台会话配置。因此,我制作了一个非常类似的api客户端,其后台配置设置如下:

+ (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;
}
+ (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;
}
问题是,当我使用这个客户端发布帖子时,每次都会出现这些错误

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' 
Aug 21 19:19:07我的iPhone共享[6290]:试图在已失效的会话中创建任务
8月21日19:19:07我的iPhone共享[6290]:***在-[MyapClient setDelegate:forTask:],/Users/me/Documents/myproject/myproduct/Pods/AFNetworking/AFURLSessionManager.m:337中断言失败
8月21日19:19:07我的iPhone共享[6290]:***由于未捕获的异常“nsInternalInconsistenceException”而终止应用程序,原因:“无效参数未满足:任务”
关于如何让它工作有什么建议吗?非常感谢

来自:

如果你的应用程序扩展启动后台任务,你还必须设置一个共享容器,扩展程序及其包含的应用程序都可以访问该容器。使用
NSURLSessionConfiguration
类的属性指定共享容器的标识符,以便以后可以访问它

以及:

如果尝试使用应用程序扩展创建URL会话,但未能将此属性设置为有效值,则URL会话在创建时将无效

有关设置共享容器的指南,请参阅

在您的示例中,您将添加如下内容:

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

您将需要一个后台会话用于包含应用程序,另一个用于其扩展。

确保您的sharedContainerIdentifier与您为主机应用程序和扩展应用程序注册的组标识符相同

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

您可以在Xcode capabilities项目选项卡中注册组标识符。

请随时提交错误报告以获得更好的错误消息。真的,这应该会引发一个异常,并告诉您设置属性。您可以共享代码吗?我们如何在共享照片扩展中使用adnetworking上传图像阿隆·布拉格:嗨,我做了所有这些事情,但仍然没有共享图像。我需要为共享映像调用两个api。在第一个api中,我上传了图像,得到了“图像路径”,然后我需要在另一个api中发送图像路径。你能帮我解决我的问题吗?@AaronBrager:嗨,我做了所有这些事情,但仍然没有分享图像。我需要为共享映像调用两个api。在第一个api中,我上传了图像,得到了“图像路径”,然后我需要在另一个api中发送图像路径。你能帮我解决我的问题吗?评论只能编辑5分钟?评论只能编辑5分钟?评论只能编辑5分钟?你得到解决方案了吗?我和原来的海报有同样的问题,并且用相同的标识符为应用和扩展设置了应用组。一旦我更新了如上所述的共享容器标识符,我现在得到以下错误:任务:{taskIdentifier:1}已完成,但有错误:未知错误和任务:{taskIdentifier:1}已完成,但有错误:与后台传输服务的连接断开为什么要使用下载任务进行共享?我想你需要一个上传任务,这是一个动作扩展,不是为了分享。@mgcm我也遇到了同样的错误。你有什么解决办法吗?