在后台会话中不支持使用NSData上传任务。

6

我正在尝试使用共享扩展上传图片,但问题是当我添加后台任务时,它会给出以下错误:nsdata在后台任务中不受支持,但wwdc会话在nsdata中上传图片。请问问题出在哪里?我该如何解决?

在后台会话中,不支持使用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];
1个回答

8
似乎Up的NSURLSessionUploadTask不支持大型NSData, 但是您可以将文件路径提供给NSURLSessionUploadTask以上传到服务器,或者先将图像写入临时磁盘,然后再提供路径。 以下是一个例子,它同时上传了文件路径和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 URL地址

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
}];

我该如何提供URL?我检查了链接,但它没有使用URL。 - Iqbal Khan
你是否在共享扩展中运行了这段代码?它似乎无法正常工作。 - Iqbal Khan
要在本地文件中使用NSURL,请使用[[NSURL alloc] initFileURLWithPath:filePath]] - thijsonline

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接