0

I have been stucked in this from last week. But I did not get the solution.

I want to access all photos from camera roll and want to save all photos on remote server with the help of NSURLSessionUploadTask. For this I am using following code:

 NSString *boundary = @"----WebKitFormBoundarycC4YiaUFwM44F6rT";
NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"%@.jpeg\"\r\n", @"upload",self.pictureName] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Type: image/jpeg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:self.imageData];
[body appendData:[[NSString stringWithFormat:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];

NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
config.HTTPMaximumConnectionsPerHost = 1;
config.HTTPAdditionalHeaders = @{
                                 @"api-key"       : @"55e76dc4bbae25b066cb",
                                 @"Accept"        : @"application/json",
                                 @"Content-Type"  : [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary]
                                 };
NSURLSession *upLoadSession = [NSURLSession sessionWithConfiguration:config delegate:self delegateQueue:nil];
NSURL *url = self.uploadURL;
NSLog(@"URL=%@",url);
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
[request setHTTPMethod:@"PUT"];
[request setHTTPBody:body];

// 3

self.uploadTask = [upLoadSession uploadTaskWithRequest:request fromData:nil];
// self.uploadTask = [upLoadSession uploadTaskWithRequest:request fromFile:tmpFileUrl];
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];

// 5
[_uploadTask resume];

All thing is working fine in foreground. But as I enter in background, It stop working. Where is the problem? What I will have to do for uploading images in background using NSURLSessionUploadTask?

Rox
  • 909
  • 11
  • 31

2 Answers2

1

Your session config is not specified to run in the background.

NSURLSessionConfiguration *config = [NSURLSessionConfiguration backgroundSessionConfiguration:@"com.test.backgroundSession"];

NSURLSession *upLoadSession = [NSURLSession sessionWithConfiguration:config delegate:self delegateQueue:nil];

Implement as above.

Woodstock
  • 22,184
  • 15
  • 80
  • 118
  • Now It has stop uploading images on server – Rox Sep 03 '14 at 10:17
  • +backgroundSessionConfiguration: is unique in that it creates a background session. Background sessions differ from regular, run-of-the-mill sessions in that they can run upload and download tasks even when the app is suspended, exits, or crashes. The identifier specified during initialization is used to provide context to any daemons that may resume background transfers out of process. – Woodstock Sep 03 '14 at 10:21
  • @Rox please read this: http://www.raywenderlich.com/51127/nsurlsession-tutorial - should you have any further questions that are not covered in detail here let me know. Basically you have to call the background session in a background task. – Woodstock Sep 03 '14 at 10:23
  • @Woodstock The app is in the background but is not executing code. The system moves apps to this state automatically and does not notify them before doing so. While suspended, an app remains in memory but does not execute any code. – Anurag Sharma Jun 06 '17 at 08:10
1

For upload task to work in background, you have to use uploadTaskWithRequest:fromFile: instead of uploadTaskWithRequest:fromData:

If you use NSData to supply the files, it will work while the app is active and will stop once the app is in background.

Suran
  • 1,209
  • 1
  • 17
  • 21