Swift : How to run a GET call in background and then show notifications

Talha Ahmad Khan picture Talha Ahmad Khan · Aug 1, 2016 · Viewed 9.6k times · Source

I am making an app which displays data from a Website/Blog. User can follow any author. Requirement is , when the app goes to background 1) First a GET call for the data should run 2) After analysing the data, proper notifications should be displayed

I've read about Background Fetch details. But it states that OS will set it's own timing to run that code. Ive checked the app and it has just displayed notifications one time in 3 days.

Can any one tell me how to do background fetch properly and display notifications accordingly. Any good article link will be appreciated too.

Answer

Ramesh Lingappa picture Ramesh Lingappa · Aug 1, 2016

You read right, in iOS you cannot run in background indefinitely, instead iOS will monitor the usage pattern of your application and launch your application if certain criteria matches (like battery, time etc).

you can check this link to understand more about background executions, https://developer.apple.com/library/ios/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/BackgroundExecution/BackgroundExecution.html

For implementation, checkout the following articles, its really useful

https://www.raywenderlich.com/92428/background-modes-ios-swift-tutorial

https://www.appcoda.com/ios7-background-fetch-programming/

https://blog.newrelic.com/2016/01/13/ios9-background-execution/

Hope it helps!