Josh
Josh

Reputation: 1718

iOS 7.1 CoreLocation and geofencing when app is closed

I am currently using CoreLocation to set up geofences and need the app to store events in a database whenever the user enters or exits a geofence. It works when the app is running or in the background, but I need it to somehow execute the database saving code of the app even when the app is terminated (i.e. closed by swiping up on the multitasking menu).

I am using iOS 7.1 which I thought automatically did this, but it is not working for me. How can I get this to work? I am using the startMonitoringForRegion method in the CLLocationManager class to monitor the regions. Is there something extra I need to do so that the app will be notified in the background? Also, where would the database saving code go in the app for when the app is restarted (currently it is in the didFinishLoadingWithOptions menu of the app delegate).

Upvotes: 2

Views: 1217

Answers (3)

Ricky
Ricky

Reputation: 10505

I believe if your app is terminated (suspended mode), you can not do anything. When a local notification is triggered during the suspended mode, you can only handle that in didFinishLaunchingWithOptions when the app is becoming active again by tapping on the notification.

The code can be something like:-

UILocalNotification *localNotif = [launchOptions objectForKey:  
                                  UIApplicationLaunchOptionsLocalNotificationKey];
if (localNotif) {
    NSDictionary * userinfo = localNotif.userInfo;        
    if(userinfo){
        NSString * typeOfNotify = [userinfo objectForKey:@"typeOfNotify"];
        if([typeOfNotify isEqualToString:@"YOURKEY"])
            {
                //TODO: Handle the database saving and etc.
            }
    }
}

The other place to handle the local notification is at application:didReceiveLocalNotification:.

I have the same code in 2 places to handle the local notification.

Addition Note: Please take a look at the code on how I handle Region Monitoring here: Region Monitoring Glitch on iOS 7 - Multiple Notifications at the same time

There is a glitch on Region Monitoring that I have faced, you might want to take a precaution measure to handle that.

Upvotes: 0

Gautam Jain
Gautam Jain

Reputation: 2911

I'm not sure about iOS7, but as of iOS8, according to Apple Docs:

The region monitoring service delivers events normally while an app is running in the foreground or background. (You can use this service for both geographic and beacon regions.) For a terminated iOS app, this service relaunches the app to deliver events. Use of this service requires “Always” authorization from the user.

That means geofence will work even when the app is terminated. Also, the callback is in appDidFinishLaunching, where you get the info dictionary in the key UIApplicationLaunchOptionsLocationKey

In iOS, the regions you register with the location manager persist between launches of your application. If a region crossing occurs while your iOS app is not running, the system automatically wakes it up (or relaunches it) in the background so that it can process the event. When relaunched, all of the regions you configured previously are made available in the monitoredRegions property of any location manager objects you create.

Upvotes: 2

Aaron
Aaron

Reputation: 7145

I am using the startMonitoringForRegion method in the CLLocationManager class to monitor the regions. Is there something extra I need to do so that the app will be notified in the background?

No, because you're talking about termination, not backgrounding:

It works when the app is running or in the background, but I need it to somehow execute the database saving code of the app even when the app is terminated (i.e. closed by swiping up on the multitasking menu).

You're probably confusing the Background and Not Running state as mentioned here. If you're app is not running, then it can't and shouldn't do anything.

Also, think about it this way. Your app gets a SIGKILL from the system, when you swipe to kill your app. You can observe this in main.m if you're running the debugger in Xcode.

Also, where would the database saving code go in the app for when the app is restarted (currently it is in the didFinishLoadingWithOptions menu of the app delegate).

You can do this in didFinishLaunchingWithOptions, or in some other class by listening for the UIApplicationDidBecomeActiveNotification from NSNotificationCenter: See more about that here.

Upvotes: 1

Related Questions