Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS Enterprise app - How can I make sure my app runs in the background?

Tags:

ios

iphone

First of all it's important to note that I'm developing an Enterprise App, so there is no need for App Store guidelines \ approvals considerations.

My goal is the following:

1) An iPhone app which the user should open only one time only. During this one time he will go through some sign up process.

2) Once step (1) has finished (either by app suspension or app close), the app should "wake up" every hour (more or less) and send the server some data regarding the user from the background (all is done with the user agreement of course).

Optional Solutions I tried:

I read very thoroughly Apple guidelines for running in background in https://developer.apple.com/library/ios/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/BackgroundExecution/BackgroundExecution.html

I decided to go with 2 UIBackgroundModes:

  • location
  • fetch

1) For location I use:

  • startMonitoringSignificantLocationChanges();
  • manager.requestAlwaysAuthorization()

    In order to make sure my app will run after "app close" or even "device reboot" I had to use both options. See Apple guidelines: https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/LocationAwarenessPG/CoreLocation/CoreLocation.html#//apple_ref/doc/uid/TP40009497-CH2-SW1

If you leave the significant-change location service running and your iOS app is subsequently suspended or terminated, the service automatically wakes up your app when new location data arrives.

2) For background fetch I use

performFetchWithCompletionHandler()

and

UIApplication.sharedApplication().setMinimumBackgroundFetchInterval(UIApplicationBackgroundFetchIntervalMinimum);

For some reason the app doesn't run in background after 24 hours. Meaning the app never "wakes up" again even though according to Apple guidelines whenever significant location update occurs - it suppose to wake up.

What can I do in order to guarantee as much as possible that my app will run regularly in the background?

like image 392
Oded Regev Avatar asked Mar 14 '16 12:03

Oded Regev


2 Answers

In your project properties you have to allow background mode for your application to Update location and for background fetch.

Fetch in background is not so easy as it seems. iOS will define by itself how often it will fetch data in background modeeven you set option for each 15 minutes :( As my practice shows time interval for fetching new data is less if user open application from time to time so iOS makes some calculations about normal app use. As a result app calls background fetch data according to app ussage.

But you can make one trick: you can call fetch procedure from you location procedure. For data exchange it was usefull in my projects :)

like image 112
Vlad Avatar answered Sep 23 '22 12:09

Vlad


The solution worked with Silent PN.

My goal as noted in the bounty was: ".. to have an iOS app that runs regularly in the background for 30+ days when not opened at all. Should also run after the app closes."

I found that when sending PN with "content-available" : 1 did the trick for me.

I set up a server which sends every 15 min a "silent PN" to all registered devices. I was able to confirm from the same server that those clients receive the PN and act on it.

Thanks @andlin for the advice.

like image 20
Oded Regev Avatar answered Sep 23 '22 12:09

Oded Regev