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
Upload tasks from NSData are not supported in background sessions
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];
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.
Here's an example it is uploading both the filepath and NSdata.
Uploads using backgroundSessionConfiguration and NSURLSessionUploadTask cause app to crash
Updated :
//Create a file to upload
UIImage *image = [UIImage imageNamed:@"[email protected]"];
NSData *imageData = UIImagePNGRepresentation(image);
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *URLs = [fileManager URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask];
NSString *documentsDirectory = [[URLs objectAtIndex:0] absoluteString];
// Here's the 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"];
// Here it is using it filePath.
NSURLSessionUploadTask *uploadTask = [upLoadSession uploadTaskWithRequest:request fromFile:[NSURL URLWithString:filePath] completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
//code
}];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With