7

I’m trying to download a series of files from server to iOS app, with the intent that the download of these files happen even when the App is in the background mode.

I'm using background transfer provided by NSURLSession and its series of APIs.

I initiated download task when app was running in foreground mode and it gave me expected result as series of download was working good but if I press the home button when series of download is in progress then it will stop immediately and when I go into the app again download shall resume.

If say it in simple words background transfer isn’t even working me.

EDIT

Initially I was testing this in simulator it didn’t work well and someone commented to test the same in real device, so I tested it by connecting real device in debug mode it really worked well and I thought everything was solved but again it gave me a shock that is if I disconnect the device from Xcode and check app, it did not work at all :(

I created a sample application to demonstrate this issue, please download it from here

Note: In this sample application I reinitiated download call in the didFinishDownloadingToURL to simulate series of download files

Can you anyone please help me to solve this?

thavasidurai
  • 1,934
  • 1
  • 25
  • 47

1 Answers1

0

You have not selected the correct background mode in the Capabilities tab. Check the Background fetch option. Background fetch option not checked

Also, if you are testing in the simulator only, click on Debug -> Simulate Background Fetch in the menu. Otherwise, the background fetch will not be initiated in the simulator.

saugat gautam
  • 294
  • 2
  • 8
  • 2
    Thanks for your input. I guess just to use background transfer (NSURLSessionTask) we don't need to enable background fetch. I am assuming background fetch is completely different. – thavasidurai Apr 03 '17 at 08:56
  • You're welcome :) . You will eventually need to check the background fetch feature to pass the apple review when sending your app to the app store. – saugat gautam Apr 03 '17 at 15:28
  • Oh really? If possible can you please share me a link which says enabling background fetch is must to pass review? – thavasidurai Apr 03 '17 at 17:18
  • Checkout this tutorial from raywenderlich. https://www.raywenderlich.com/143128/background-modes-tutorial-getting-started – saugat gautam Apr 04 '17 at 03:23
  • But here is a contradictory piece of information which says its optional. https://developer.apple.com/library/content/documentation/IDEs/Conceptual/AppDistributionGuide/AddingCapabilities/AddingCapabilities.html . So I guess since Apple says its optional, it is indeed optional. Never knew that. Should dig on it some more. Sorry for the misinformation. But in any case I will not take any chances with my app for now :). – saugat gautam Apr 04 '17 at 03:51
  • The background fetch checkbox is for a distantly related feature that allows your app to be periodically launched in the background to fetch resources (e.g. for a newspaper's application to periodically fetch the latest news). – dgatwood Apr 26 '17 at 00:44