Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to work with large file uploads in ios?

My app requires upload of video files from users phone which will then be processed on server. THe problem is the size of the file can go to 200 MB plus and the user won't keep the application open to wait for the file to upload. Since apple doesn't allow apps to run in background for more than a limited time. How can I ensure that my files are uploaded. I am using afnetworking to set up an upload task as given by ios 7 library.

Please if anyone can point me in the right direction or have any solution it would be greatly appreciated. I have banged my head on this for too long. Thanks.

        NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
    AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];


    [manager setTaskDidSendBodyDataBlock:^(NSURLSession *session,NSURLSessionTask *task ,int64_t bytesSent, int64_t totalBytesSent,int64_t totalBytesExpectedToSend){
        CGFloat progress = ((CGFloat)totalBytesSent / (CGFloat)sensize);

       NSLog(@"Uploading files %lld  -- > %lld",totalBytesSent,totalBytesExpectedToSend);
        [self.delegate showingProgress:progress forIndex:ind];
    }];



    dataTask = [manager uploadTaskWithStreamedRequest:request progress:nil completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
        if (error) {
            NSLog(@"Error: %@", error);
        } else {

        }

       }];

My request is a normal multipart form request.

like image 444
aradhya Avatar asked Oct 13 '13 08:10

aradhya


People also ask

How do I handle a large file upload?

Possible solutions: 1) Configure maximum upload file size and memory limits for your server. 2) Upload large files in chunks. 3) Apply resumable file uploads. Chunking is the most commonly used method to avoid errors and increase speed.

How can I send files larger than 25mb on iPhone?

You cannot email a file larger than 25 megabytes using an iPhone, which can make it difficult to send large or long videos. You can work around these file size limits to send a large video using the iPhone's Mail Drop feature, which sends content via iCloud, or by using a third-party platform like Google Drive.

Can you send large files on iPhone?

Mail Drop lets you send large files like videos, presentations, and images through iCloud. If you shared links through Mail Drop that are no longer available, you might have exceeded one or more of the service limits. With Mail Drop, you can send attachments up to 5 GB in size.

How do I send multiple large files from my iPhone?

Another great way to transfer large files from iPhone is through online cloud storage services like Google Drive, Dropbox, Microsoft OneDrive, Box, etc. You simply upload files to these services using the app on your iPhone, then use the in-app option to easily share with friends, family, colleagues, and more.


1 Answers

Use:

NSURLSessionConfiguration:backgroundSessionConfiguration:

instead of

NSURLSessionConfiguration:defaultSessionConfiguration

From the NSURLSessionConfiguration:backgroundSessionConfiguration: documentation:

Upload and download tasks in background sessions are performed by an external daemon instead of by the app itself. As a result, the transfers continue in the background even if the app is suspended, exits, or crashes.

So in your case, change:

NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];

to:

NSString *appID = [[NSBundle mainBundle] bundleIdentifier];
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration backgroundSessionConfiguration:appID];

Implementing application:handleEventsForBackgroundURLSession:completionHandler: on your app delegate will allow your app to be woken up (ie. un-suspended or un-terminated in background mode) when an upload has completed (whether it has completed successfully or not).

Don't get confused with Background Fetching. You don't need it. Background Fetching simply wakes you app up to periodically give your app the chance to fetch small amounts of content regularly. It may however, be useful for restarting failed "background-mode" uploads periodically.

like image 182
gavdotnet Avatar answered Sep 28 '22 22:09

gavdotnet