后台会话不支持从NSData上载任务 [英] Upload tasks from NSData are not supported in background sessions

查看:325
本文介绍了后台会话不支持从NSData上载任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用共享扩展程序上传图片,但问题是当我添加后台任务时它给我这个错误,它说后台任务不支持nsdata但是wwdc会话是在nsdata中上传图像。你能告诉我问题在哪里吗?以及我如何解决它

i am trying to upload image using Share extension but the problem is when i add the background task it is giving me this error, It is saying nsdata is not supported in background task but wwdc session is uploading the image in nsdata. Will you please let me know where is the problem. and how i can fix it

后台会话不支持从NSData上传任务

NSString *boundary = @"SportuondoFormBoundary";

NSString * configurationName = @"com.xxxxxxxx.PhotoSharing.backgroundConfiguration";

NSURLSessionConfiguration *configuration= [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:configurationName];
[configuration setSharedContainerIdentifier:kGroupNameToShareData];


configuration.HTTPAdditionalHeaders = @{
                                        @"api-key"       : @"55e76dc4bbae25b066cb",
                                        @"Accept"        : @"application/json",
                                        @"Content-Type"  : [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary]
                                        };

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


NSMutableData *body = [NSMutableData data];


for (NSString *key in parameters)
{
    [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n", key] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithFormat:@"%@\r\n", [parameters objectForKey:key]] dataUsingEncoding:NSUTF8StringEncoding]];

}

NSData *imageData = UIImageJPEGRepresentation(image, 0.8);
NSLog(@"imageDATE, %@", imageData);
if (imageData)
{
    [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"image.jpg\"\r\n", @"file"] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[@"Content-Type: image/jpg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:imageData];
    [body appendData:[[NSString stringWithFormat:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
}
[body appendData:[[NSString stringWithFormat:@"--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];




// Data uploading task. We could use NSURLSessionUploadTask instead of NSURLSessionDataTask if we needed to support uploads in the background
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@",kURLBase,kURLAddPostDL]];
NSLog(@"url %@",url);
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:10000];
request.HTTPMethod = @"POST";
request.HTTPBody = body;



NSURLSessionUploadTask *upload=[session uploadTaskWithRequest:request fromData:request.HTTPBody];
[upload resume];


推荐答案

似乎Up NSURLSessionUploadTask 不支持大尺寸NSData,
但您可以提供 NSURLSessionUploadTask 的文件路径以在服务器上传,或临时写入您的图像disk firs然后给出路径。

It seems Up NSURLSessionUploadTask does not support Large size NSData, But you can give a file path to NSURLSessionUploadTask to upload on server, or write your image temporary on disk firs then give path.

这是一个上传文件路径和NSdata的例子。

Here's an example it is uploading both the filepath and NSdata.

使用backgroundSessionConfiguration和NSURLSessionUploadTask上传会导致应用崩溃

更新:

//Create a file to upload
UIImage *image = [UIImage imageNamed:@"onboarding-4@2x.png"];
NSData *imageData = UIImagePNGRepresentation(image);
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *URLs = [fileManager URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask];
NSString *documentsDirectory = [[URLs objectAtIndex:0] absoluteString];

//这是 filePath 网址

NSString *filePath = [documentsDirectory stringByAppendingString:@"testfile.png"];
[imageData writeToFile:filePath atomically:YES];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"https://file.upload/destination"]];
[request setHTTPMethod:@"PUT"];

//这里正在使用 filePath

NSURLSessionUploadTask *uploadTask = [upLoadSession uploadTaskWithRequest:request fromFile:[NSURL URLWithString:filePath] completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
    //code
}];

这篇关于后台会话不支持从NSData上载任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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