How to upload images in background in iOS?

How to upload more then 200 images in background by AFNetworking in IOS

  • I am trying to upload more then 200 images (as per my need) When application in background. I am using NSUrlSession (upload task). It is uploading approx. 20 images easily but after that process is not responding. I am using a single request (Not using an array for upload images on server for client requirement). Please suggest me any idea with some example code, Because i have tried with approx. 10-15 different approach. Thanks is advance

  • Answer:

    Application goes to suspended state (In Background) after a specific time . So all the ongoing tasks remain suspended. Possible approach is , Application can request OS for some extra time to complete the unfinished task using beginBackgroundTaskWithExpirationHandler. A sample code is below: __block UIBackgroundTaskIdentifier backgroundTaskIdentifier = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{ NSLog(@"Background Time:%f",[[UIApplication sharedApplication] backgroundTimeRemaining]); [[UIApplication sharedApplication] endBackgroundTask:backgroundTaskIdentifier]; backgroundTaskIdentifier = backgroundTaskIdentifier; }]; NOTE: For 200 images as you are mentioning, it will be a tough task for the application to be in active state. Try different approach, for example giving support to App for Background modes.

devil at Stack Overflow Visit the source

Was this solution helpful to you?

Other answers

Try this I am using the AFNetworking for doing this Step 1: create your request NSMutableURLRequest *request1 = [[AFHTTPRequestSerializer serializer] multipartFormRequestWithMethod:@"POST" URLString:[NSString stringWithFormat:@"%@%@", API_MAIN_URL, IMAGE_UPLOAD] parameters:param constructingBodyWithBlock:^(id formData) { [formData appendPartWithFileURL:[NSURL fileURLWithPath:strImagePath] name:@"sendimage" fileName:[strImagePath lastPathComponent] mimeType:@"image/png" error:nil]; } error:nil]; Step 2: here I am creating the Stream at given Location [[AFHTTPRequestSerializer serializer] requestWithMultipartFormRequest:request1 writingStreamContentsToFile:[NSURL fileURLWithPath:[strImagePath stringByDeletingPathExtension]] completionHandler:^(NSError *error){ Step 3: here I am creating uploading task. ///here is file NSProgress *progress = nil; NSURLSessionUploadTask *uploadTask = [self.manager uploadTaskWithRequest:request1 fromFile:[NSURL fileURLWithPath:strImagePath] progress:&progress completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) { NSLog(@"response : %@\n\n responseObject : %@\n\n error : %@", response, responseObject, error); }]; [uploadTask resume]; } }]; } repest this code for uploading with background task

Mady

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.