0

I have implemented

-(void)uploadMultipleImagesUsingAFNetworkingMultipartFormat:(id)sender {

NSArray * directoryContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:[[FileUtility basePath] stringByAppendingPathComponent:[self getPathOfRootImageFolderDocumentId:@"1"]] error:nil];

NSString *path = [directoryContents objectAtIndex:0];

NSString* filePath=[[[FileUtility basePath] stringByAppendingPathComponent:[self getPathOfRootImageFolderDocumentId:@"1"]] stringByAppendingPathComponent:path];

NSURLSessionConfiguration *config = [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:@"com.uploadDocument.Background"];

MBProgressHUD *progres = [MBProgressHUD showHUDAddedTo:[[AppDelegate sharedInstance] window] animated:YES];
progres.mode = MBProgressHUDModeDeterminateHorizontalBar;
progres.progress = 0.0;

if (!self.sessionManager) {
    _sessionManager = [[AFHTTPSessionManager alloc] initWithSessionConfiguration:config];

}
NSError *error;
NSMutableURLRequest *requet= [[AFHTTPRequestSerializer serializer] multipartFormRequestWithMethod:@"POST" URLString:@"URLTO UPLOAD" parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
    NSError *error1;
    [formData appendPartWithFileURL:[NSURL fileURLWithPath:filePath] name:@"fileUpload" fileName:path mimeType:@"image/jpeg" error:&error1];
    NSLog(@"%@",error1);


} error:&error];


NSURLSessionUploadTask *uploadTask = [self.sessionManager uploadTaskWithRequest:requet fromFile:[NSURL fileURLWithPath:filePath] progress:^(NSProgress * _Nonnull uploadProgress) {
    dispatch_async(dispatch_get_main_queue(), ^{
        [progres setProgress:uploadProgress.fractionCompleted];
    });

    NSLog(@" Progress %f",uploadProgress.fractionCompleted);
} completionHandler:^(NSURLResponse * _Nonnull response, id  _Nullable responseObject, NSError * _Nullable error) {
    dispatch_async(dispatch_get_main_queue(), ^{
        [MBProgressHUD hideHUDForView:[AppDelegate sharedInstance].window animated:YES];
    });

}];
[uploadTask resume];


}

But when i put it in background, and set active after some time. i observe that process start from where is paused.

How to continue upload task in background too?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Prashant Tukadiya
  • 15,838
  • 4
  • 62
  • 98

2 Answers2

1

enter image description here

plz on background fetch in background modes then try :)

balkaran singh
  • 2,754
  • 1
  • 17
  • 32
1

You just need to add below code as there is bug in NSURLSessionTask for background task.

// Prepare a temporary file to store the multipart request prior to sending it to the server due to an alleged
// bug in NSURLSessionTask.
NSString* tmpFilename = [NSString stringWithFormat:@"%f", [NSDate timeIntervalSinceReferenceDate]];
NSURL* tmpFileUrl = [NSURL fileURLWithPath:[NSTemporaryDirectory() stringByAppendingPathComponent:tmpFilename]];


// Dump multipart request into the temporary file.
[[AFHTTPRequestSerializer serializer] requestWithMultipartFormRequest:requet
                                          writingStreamContentsToFile:tmpFileUrl
                                                    completionHandler:^(NSError *error)
 {
     // Once the multipart form is serialized into a temporary file, we can initialize
     // the actual HTTP request using session manager.


     // Here note that we are submitting the initial multipart request. We are, however,
     // forcing the body stream to be read from the temporary file.

     NSURLSessionUploadTask *uploadTask = [self.sessionManager uploadTaskWithRequest:requet fromFile:tmpFileUrl progress:^(NSProgress * _Nonnull uploadProgress) {
         dispatch_async(dispatch_get_main_queue(), ^{
             [progres setProgress:uploadProgress.fractionCompleted];
         });

         NSLog(@" Progress %f",uploadProgress.fractionCompleted);
     } completionHandler:^(NSURLResponse * _Nonnull response, id  _Nullable responseObject, NSError * _Nullable error) {
         dispatch_async(dispatch_get_main_queue(), ^{
             [MBProgressHUD hideHUDForView:[AppDelegate sharedInstance].window animated:YES];
         });

     }];

     [uploadTask resume];
 }];

Hope this will help you. And if you have any question then please ask.

Ekta Padaliya
  • 5,743
  • 3
  • 39
  • 51