iosiphonensurlsessionnsurlsessionconfiguration

(iOS)Running download task in background


Hi can I run a download task for infinite time (say for 1hr) if I use NSUrlsession backgroundSessionConfiguration in ios. Or will it get killed after 3mins?


Solution

  • Yes and no. You can set up downloads to run in the background using NSURLSession (or URLSession, as it's called in Swift 3.)

    It's been a while since I've used this, but here's what I remember:

    When you do that the system takes over managing your download for you. Your app can be in the foreground, in the background, or not running at all and the download still proceeds. If your app is no longer running when the download completes, your app will be re-launched. You will need to implement the application:handleEventsForBackgroundURLSession:completionHandler: method in your app delegate. See the docs on that method for instructions on what to do if a download completes when your app is not running.

    Because the system takes over the job of managing background downloads it doesn't keep your app alive in the background. It follows the same background rules as normal. (It gets suspended quite quickly if the user switches apps, and can be terminated at any time after that.)