How to do BG NW load after didEnterRegion / didExitRegion geofence notification

I am developing a client / server application for a client where the clients are iOS devices.

One client module is for traveling salespeople.

My client wants the salesperson application to automatically download their local database when the salesperson leaves the office and when the salesperson returns to the office.

I am setting up geofencing using a location method startMonitoringForRegion

and will search UIApplicationLaunchOptionsLocationKey

on startup as well as look for calls to locationManager:didEnterRegion

/ locationManager:didExitRegion

from the background location control methods .

My question is, how can I ask the system to give me time to connect to the network and load new data in response to starting the application with UIApplicationLaunchOptionsLocationKey

or background calls on locationManager:didEnterRegion

/locationManager:didExitRegion

None of the background mode keys ( UIBackgroundModes

) looks like they are appropriate.

I dont need a key location

as I dont need real time location information, just enter geofence entry / exit messages. The key is fetch

also not what I need, as it forces the system to wake up my app in the background at times it chooses.

Do I have to have one of the background mode keys to make the call beginBackgroundTaskWithExpirationHandler

?

I guess I have to make my network request in mine beginBackgroundTaskWithExpirationHandler

in my methods locationManager:didEnterRegion

/ locationManager:didExitRegion

. Will the system allow me to do this without a kit UIBackgroundModes

?

And can I call beginBackgroundTaskWithExpirationHandler

if I'm already running from the background? (In the case where the application is running, but not in front, and a call locationManager:didEnterRegion

or or comes in locationManager:didExitRegion

.) My goal is to request enough background time to load my network and save the data to my local database.

Any guidance from someone who has done something like this would be a great help. The docs are pretty vague, and figuring out how to use the background APIs for trial and error is likely to be quite time consuming.

PS I am developing this app in Swift even though I am a longtime Objective-C developer. I'm more comfortable translating Objective-C to Swift or mixing Objective-C and Swift as needed, so examples in any language would be helpful.

+3


source to share


1 answer


It is okay to call beginBackgroundTaskWithExpirationHandler from the background. Starting with iOS8, this call will extend the execution time to 180 seconds from 10 seconds.

Keep in mind that you need to request Always authorization for location services, which is required to monitor a region.

I suggest you use a background key. I forgot to install it in one of my apps and it was still receiving I / O events in the region. Nevertheless,



- behavior may change with minor iOS update as well

- app may be rejected by Apple

+2


source







All Articles