I am developing a client/server app for a client where the clients are iOS devices.
One client module is for traveling salespeople.
My client wants the salesperson's app to automatically download it's local database when the salesperson leaves the office, and when the salesperson returns to the office.
I am setting up a geofence using the location manager's startMonitoringForRegion
method, and will look for the UIApplicationLaunchOptionsLocationKey
on launch, as well as looking for calls to the location manager locationManager:didEnterRegion
/locationManager:didExitRegion
methods from the background.
My question is, how do I ask the system to allow me time to make a network connection and download new data in response to either an app launch with a UIApplicationLaunchOptionsLocationKey
, or background calls to locationManager:didEnterRegion
/locationManager:didExitRegion
?
None of the background mode keys (UIBackgroundModes
) look like the right fit.
I don't need the location
key, since I don't need live location information, just geofence enter/exit messages. The fetch
key isn't what I need either, since that causes the system to wake my app up in the background at times it chooses.
Do I have to have one of the background mode keys set in order to make a beginBackgroundTaskWithExpirationHandler
call?
I guess what I should do is submit my network request in my beginBackgroundTaskWithExpirationHandler
in my locationManager:didEnterRegion
/locationManager:didExitRegion
methods. Will the system allow me to do that without having a UIBackgroundModes
key set?
And can I make a call to beginBackgroundTaskWithExpirationHandler
if I'm already running from the background? (In the case where the app is running but not frontmost and a locationManager:didEnterRegion
or locationManager:didExitRegion
call comes in.) My goal is to ask for enough background time to process my network download and save the data to my local database.
Any guidance from somebody who's done something like this would be a big help. The docs are pretty vague, and figuring out how to use the background APIs by trial and error is likely to be pretty time-consuming.
P.S. I'm developing this app in Swift, although I'm a longtime Objective-C developer. I'm comfortable translating Objective-C to Swift or mixing Objective-C and Swift as needed, so examples in either language would be useful.
It is OK to call beginBackgroundTaskWithExpirationHandler from background. As of iOS8 this call will extend your run time to 180 seconds from 10s.
Keep in mind that you need to request Always authorization for location services, that's what is required to run region monitoring.
I suggest you do use the background location key. I forgot to set it in one of my apps, and it still was receiving the region enter/exit events. However,
-- the behavior might change with minor iOS upgrade, and also
-- the app might be rejected by Apple