-1

I am trying to upload multiple images using NSURLSession .It works fine when application is running in foreground.When application enter background,uploading process stop after uploading current task.I would like to upload all the files when application is in background. Any help would be greatly appreciated. Here is my code.

//background task configuration

-(void) uploadOneByOne:(NSString *)individualpath{
         NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:[NSURL URLWithString:mutableUrlString]];

    NSString *requestURL = [NSString stringWithFormat:@"http://myservice/Service.svc/UploadOrdersToDrive?orderFolder=%@",OrderFolderID];

           NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:[NSURL URLWithString:requestURL]];

            [request setHTTPMethod:@"POST"];

            NSURL *filePath =[NSURL fileURLWithPath:individualpath];
            NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration backgroundSessionConfiguration:kSessionIdentifier];
            defaultSession= [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:nil];
            NSURLSessionUploadTask *uploadTask =
            [defaultSession uploadTaskWithRequest:request
                                         fromFile:filePath];
            [uploadTask resume];
    }

NSURLSession Delegate

receive first request response

 - (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask
            didReceiveData:(NSData *)data
        {
            NSString * str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
            NSLog(@"Received String %@",str);
            NSDictionary *jsonResponseData = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];

    //FolderID to create next image in same folder.
                OrderFolderID =[jsonResponseData 

objectForKey:@"OrderFolderID"];

        }

create next request

  - (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task
    didCompleteWithError:(NSError *)error
    {
        if(error == nil)
        {

                //Remove DetailFist
                [orderDetailarray removeObjectAtIndex:0];
                if (orderDetailarray.count >0){
                    ChosenImages *item = [orderDetailarray objectAtIndex:0];

                    [self uploadOneByOne:item.path ];
    }
    }

//Update progress bar

- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task
   didSendBodyData:(int64_t)bytesSent
    totalBytesSent:(int64_t)totalBytesSent
totalBytesExpectedToSend:(int64_t)totalBytesExpectedToSend{

//update progress bar
}  
sasha
  • 343
  • 1
  • 6
  • 14
  • Did you try with for loop for calling all the request??? [question about my problem with loop](http://stackoverflow.com/questions/30251881/issue-while-uploading-image) – Sumeet Mourya May 20 '15 at 10:53
  • I am trying to achieve same thing - upload multiple files while app is in background. This looks like close to something that should work. It has been two years, did you ever get this working ? In either case, please update this. – Curious101 Dec 03 '17 at 19:49

1 Answers1

0

Probably you didn't wait enough for next task to start. Depending from different things like WiFi and battery status, user activity etc. your next task queued in background could start in few minutes.. or few hours. Btw I don't see code related with completionHandler implementation.

Do not forgot to implement

- (void)application:(UIApplication *)application handleEventsForBackgroundURLSession:(NSString *)identifier completionHandler:(void (^)())completionHandler

in the App delegate and appropriate session delegate.

Skie
  • 1,942
  • 16
  • 27