How to work with large files in ios?

My application requires downloading video files from the user's phone, which will then be processed on the server. The problem is that the file size can reach 200 MB plus, and the user will not keep the application open to wait for the file to load. Because apple does not allow applications to run in the background for more than a limited time. How can I ensure the download of my files. I am using afnetworking to configure the download task defined by the ios 7 library.

Please, if someone can point me in the right direction or have any solution, we will be very grateful. I've been banging my head about 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 regular multi-profile form request.

+7
source share
2 answers

Using:

 NSURLSessionConfiguration:backgroundSessionConfiguration: 

instead

 NSURLSessionConfiguration:defaultSessionConfiguration 

From NSURLSessionConfiguration:backgroundSessionConfiguration: documentation :

Downloading and loading tasks in background sessions is performed by an external daemon, and not by the application itself. As a result, transfers continue in the background, even if the application is paused, completed, or crashes.

So in your case change:

 NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration]; 

in

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

Implementing application:handleEventsForBackgroundURLSession:completionHandler: in your application will allow your application to wake up (that is, not paused or completed in the background) when the download is completed (whether it completed successfully or not).

Do not confuse background extraction. You do not need it. Background fetching just wakes up your application to periodically give your application the ability to regularly receive a small amount of content. However, it can be useful for restarting failed background downloads.

+6
source

Instead, you should use the background session configuration if this is the default session configuration. This ensures that your data transfer continues in the background after the user exits your application.

Of course, this is correct if the user has background sampling enabled for your application in the deviceโ€™s Settings application.

Be sure to enable the Background Sampling function in your project settings:

Capabilities
(source: migueldiazrubio.com )

Background fetch
(source: migueldiazrubio.com )

Then implement application:handleEventsForBackgroundURLSession:completionHandler: in your application delegate to receive information about the completion of data transfer, and do everything you need (updating the user interface ...) inside your application with the received / sent file. Remember to call completionHandler and tell the system that you have completed your tasks. Then, iOS will take a screenshot of the active screen of your application and update it on the iOS 7 multitasking screen.

-1
source

Source: https://habr.com/ru/post/955863/


All Articles