【问题标题】:Upload tasks from NSData are not supported in background sessions后台会话不支持从 NSData 上传任务
【发布时间】:2014-09-04 08:10:13
【问题描述】:

我正在尝试使用 Share extension 上传图像,但问题是当我添加后台任务时,它给了我这个错误,它说后台任务不支持 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];

【问题讨论】:

    标签: ios objective-c iphone


    【解决方案1】:

    看来UpNSURLSessionUploadTask不支持Large size NSData, 但是你可以给NSURLSessionUploadTask一个文件路径上传到服务器,或者把你的图像临时写在磁盘上然后给路径。

    这是一个同时上传文件路径和 NSdata 的示例。

    Uploads using backgroundSessionConfiguration and NSURLSessionUploadTask cause app to crash

    更新:

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

    【讨论】:

    • 我如何提供 url,我检查了链接,但它没有使用 url
    • 您是否为共享扩展运行了此代码,这不起作用
    • 对于本地文件使用 NSURL,使用[[NSURL alloc] initFileURLWithPath:filePath]
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多