geofence - check if user is already inside the fence

geofence in iOS 7.1 and above. so when the user is already inside a geofence and say sees the advertisement for an app and only then downloads the app from the appstore and opens the app. At this point, the app needs to give the user a certain message (but only this once). HOW can this be achieved?

Later the app registers the region and monitors it as normal so that when the user enters and exits the region, specific things will happen.

I feel this should be a solved issue. ie. there should be a reliable way of determining if the user was inside a region already at the time that monitoring began, but I dont find any pointers as to how this can be done with the given APIs.

When the app starts up in this scenario I see the following sequence of apis in iphone 5s

 locationManager startMonitoringForRegion
 
 locationManager:didStartMonitoringForRegion: (in this delegate method i can call the below to check if the device is already inside the region) 
   |-locationManager requestStateForRegion: 
 locationManager:didDetermineState: (fired as a result of the requestStateForRegion: call)

But locationManager:didDetermineState: also gets called on region entry and exit

 locationManager:didExitRegion:
 locationManager:didDetermineState:

So will I need to store state that for a particular region and initial message was already displayed? and so dont display it if that state exists.. sounds Hacky!

Solved

To determine if the app is within a region when monitoring begins, do the following:

 [locationManager requestStateForRegion: region];

When the CLLocationManager determines the state for the given region, the following is sent to its delegate:

- (void)locationManager:(CLLocationManager *)manager
      didDetermineState:(CLRegionState)state forRegion:(CLRegion *)region
{
    // When regions are initialized, see if we are already within the geofence.
    switch (state)
    {
        case CLRegionStateInside:
            // We are in the region.
            break;
        case CLRegionStateUnknown:
        case CLRegionStateOutside:
        default:
            break;
    }
}

Comments