I need to perform the simple task of calling a localNoification if the user enters or exits a region while the app is in background mode. Only a single set of coordinates will trigger the notification. For example:
Lat: 41.8500
Lon: 87.6500
Radius: 300
I know how to call the localNotification, and how to use the basic functionality of the locationManager, but cannot seem to track the location in the background. Any help would be great!
Have you read up on CLLocationManager’s
startMonitoringForRegion:method? I think this will do exactly what you want. The code to set it up would look something like this:After that, the device will monitor for entrances/exits to the specified region, even when your app is in the background. When a region is crossed, the delegate will receive a call
locationManager:didEnterRegion:orlocationManager:didExitRegion:. You can use this opportunity to post aUILocalNotification. If your app is not running at the time the region is crossed, it will be launched in the background, and you will need to look for the appropriate key inapplication: didFinishLaunchingWithOptions:. Use code like the following:Be aware that the app will only have a short amount of time to execute while running in the background (a few seconds); if you need to perform a longer task, call the
beginBackgroundTaskWithExpirationHandler:method that Nebs mentioned in his/her answer immediately after your app is notified of the region crossing. This will give you about 600 seconds to run in the background.