How to work with large file uploads in ios?

梦想与她 提交于 2019-11-30 14:15:43

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.

Sendoa

You should use a background session configuration instead if a default session configuration. This ensures that your data transfer will continue in the background once the user has exited your app.

Of course, this is correct as long as the user has background fetching enabled for your app in the Settings app of the device.

Be sure to enable the Background fetch capability on your project settings:


(source: migueldiazrubio.com)


(source: migueldiazrubio.com)

Then implement the application:handleEventsForBackgroundURLSession:completionHandler: in your App Delegate to be informed when the data transfer ends and do whatever you need to do (UI update…) inside your app with the received/sent file. Don't forget to call the completionHandler to inform the system that you have ended your tasks. iOS then will take a screenshot of the active screen of your app and update the one in the iOS 7 multitasking screen.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!