17

I want to check the database in my app every 12 hrs for rows with date column having corresponding date. This is accomplished by writing service in android. But is there any equivalent of services in iOS so that my requirement can be accomplished?

iammilind
  • 68,093
  • 33
  • 169
  • 336
viks
  • 404
  • 2
  • 6
  • 14

4 Answers4

11

No. There is no such thing in the SDK or in iPhone/iPad in general. You can only write code that will affect the eco system of the app, not the operating system. When your app is closed it's closed and no action will be taken until the user opens it/opens a push notification related to your app.

If the user approved location based services for your app, there are a few ways to run short background process even if your app is totally closed. One of them is by using Monitoring Shape-Based Regions which basically means if the user left region X/entered region Y open the app and run a few commands before closing it again.

The clever way (and the only way I can think of) to accomplish what you're after in iOS is to run that service on a server and pull the data from the server when the app is opened.

John Topley
  • 113,588
  • 46
  • 195
  • 237
Segev
  • 19,035
  • 12
  • 80
  • 152
  • In iOS13+ you can use BackgroundTasks to schedule the background work that you need to check and update your database. https://developer.apple.com/documentation/backgroundtasks, https://www.andyibanez.com/posts/modern-background-tasks-ios13/ – Lorraine R. May 24 '20 at 07:24
2

In iOS7 and later you can use background fetch for this task. You can check this tutorial: http://code.tutsplus.com/tutorials/ios-7-sdk-working-with-background-fetch--mobile-20520

Foriger
  • 459
  • 5
  • 30
  • The "Background processing" mode (iOS13+) is more suitable in this case - https://developer.apple.com/documentation/backgroundtasks. Apple might not approve using "background fetch" mode to do the kind of work described in the question, because it is designated to fetch content in the background to update the app's interface. – Lorraine R. May 24 '20 at 07:30
2

iOS Background work

Nowadays you you are able to use Background fetch or Background Processing Task or URLSessionDownload/UploadTask for doing something when app is in background more

[Xcode Background Modes]

[Background session]

yoAlex5
  • 29,217
  • 8
  • 193
  • 205
1

You can find the solution here. Background Execution does this.

http://developer.apple.com/library/ios/#documentation/iphone/conceptual/iphoneosprogrammingguide/ManagingYourApplicationsFlow/ManagingYourApplicationsFlow.html

Added: Apple does not allow apps to run in background for all the time. It provides some finite-length time to complete your app execution. You can increase that time depending on your execution need. But that is not recommended.

Hawk-Eye
  • 400
  • 1
  • 7
  • 23
  • 1
    That got nothing to do with what the op asked. Services in android run when the app is totally closed. The link you provided talkes about foreground or the background. – Segev May 21 '13 at 10:47
  • 2
    @EXEC_BAD_ACCESS actually the android services (if started by an app) don't run when the app is totally closed. They can run a longer time in the background but they are still linked to the app that started them. `A Service is an application component representing either an application's desire to perform a longer-running operation while not interacting with the user or to supply functionality for other applications to use. `(android docs). So if you force close an app it will kill all the services that are running. – danypata May 21 '13 at 11:21
  • The link is broken. Its encouraged to put the actual answer in stackoverflow and only provide links as a reference. – Xitcod13 Aug 11 '16 at 20:50