用于iOS 8扩展的AFNetworking后台会话配置 [英] AFNetworking Background Session Configuration for iOS 8 Extension

查看:169
本文介绍了用于iOS 8扩展的AFNetworking后台会话配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在开发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.

推荐答案

来自文档


如果您的应用扩展程序启动了背景 NSURLSession 任务,您还必须设置扩展及其包含应用程序都可以访问的共享容器。使用 NSURLSessionConfiguration 类的noreferrer> sharedContainerIdentifier 属性,用于指定共享容器的标识符,以便您以后可以访问它。

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.

并且:


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

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.

这篇关于用于iOS 8扩展的AFNetworking后台会话配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆