如何在ios app扩展中上传

时间:2014-09-05 00:31:37

标签: ios8 afnetworking-2 ios-app-extension nsurlsessionuploadtask ios-app-group

如何在ios app扩展程序中使用AFNetworking上传?

apple的例子使用NSURLSession,你能解释一下这是如何工作的吗?

- (void)didSelectPost {
NSExtensionItem *imageItem = [self.extensionContext.inputItems lastObject];


// Verify that we have a valid NSExtensionItem
if (!imageItem) {
    return;
}

// Verify that we have a valid NSItemProvider
NSItemProvider *imageItemProvider = [[imageItem attachments] firstObject];
if (!imageItemProvider) {
    return;
}

// Look for an image inside the NSItemProvider
if ([imageItemProvider hasItemConformingToTypeIdentifier:(NSString *)kUTTypeImage]) {
    [imageItemProvider loadItemForTypeIdentifier:(NSString *)kUTTypeImage options:nil completionHandler:^(id item, NSError *error) {
        if (item)
        {

            NSData *data = [NSData dataWithContentsOfURL:item];
            [self method:data];
        }

        [self.extensionContext completeRequestReturningItems:nil completionHandler:nil];
    }];
}

}

如何使用此方法或使用AFNetworking或使用我的应用上传此数据来上传此数据?

- (void)method:(NSData *)data
{
    NSString *confName = @"com.example.photoblog.backgroundconfiguration";
    NSURLSessionConfiguration *conf = [NSURLSessionConfiguration backgroundSessionConfiguration:confName];

    NSURLSession *session = [NSURLSession sessionWithConfiguration:conf delegate:self delegateQueue:nil];

    NSURLRequest *requeust = [self requestForExtensionItems];
    NSURLSessionUploadTask *upload = [session uploadTaskWithStreamedRequeust:request];

    [upload resume];

}

1 个答案:

答案 0 :(得分:0)

您应该将应用程序组设置为您的扩展程序和包含应用程序,然后像这样配置会话

config.sharedContainerIdentifier =  @"group.xxxxx";

您可以通过本教程参考更多信息 http://www.shinobicontrols.com/blog/posts/2014/07/21/ios8-day-by-day-day-2-sharing-extension