1

I am using startMonitoringForRegion to monitor specific region. When App is not running I have successfully generated a local notification when a user enters/leaves a particular location by comparing [launchOptions objectForKey:@"UIApplicationLaunchOptionsLocationKey"] in AppDelegate.

But now I am not able to understand how can I know that a user enters in a location or leaves a location. I am not able to find a way to check the response. Can I show the response in the alert body? I searched in internet but unable to find any tutorial that have written some code in AppDelegate.

1 Answers1

0

you can use location manager delegate method

- (void)locationManager:(CLLocationManager *)manager didDetermineState:(CLRegionState)state forRegion:(CLRegion *)region{

    if (state == CLRegionStateInside) {

        // We entered a region
         NSLog(@"Inside region");
 if([[UIApplication sharedApplication] applicationState] == UIApplicationStateBackground){

            UILocalNotification *notification = [[UILocalNotification alloc] init];
            notification.alertBody = @"Region Detected";
            notification.soundName = @"Default";
            [[UIApplication sharedApplication] presentLocalNotificationNow:notification];


    } else if (state == CLRegionStateOutside) {

       // We are outside region
        NSLog(@"Outside region");
    }
}

IN APPDELEGATE

-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
    [[NSNotificationCenter defaultCenter] postNotificationName:@"PerformAction" object:nil userInfo:nil];
}

IN your ViewController viewDidLoad Method :

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(performAction) name:@"PerformAction" object:nil];

In your same viewController add the following method and perform ur action

-(void)performAction
{
  // perform your action
}
Dheeraj Singh
  • 4,983
  • 23
  • 32
  • In AppDelegate I have to write this? I am comparing `[launchOptions objectForKey:@"UIApplicationLaunchOptionsLocationKey"]` in AppDelegate inside `didFinishLaunchingWithOptions` method. So what I need to write your code inside `didFinishLaunchingWithOptions` ?? –  Mar 03 '15 at 12:36
  • but why u want to write the code in appdelegate? u can determine using this delegate method if user is in the monitioringregion or not – Dheeraj Singh Mar 03 '15 at 12:41
  • When my App is not running and I enter the location then App relaunches itself. So I have to write some code in AppDelegate to say what to do next. Please read [here](http://stackoverflow.com/questions/17174042/ios-corelocation-and-geofencing-while-app-is-closed/17174344#17174344). –  Mar 03 '15 at 12:48
  • so u can create a local notification to notify the user. – Dheeraj Singh Mar 03 '15 at 12:50
  • Yes I am generating a local notification but then I don't know weather user enters a location or exits and what's the Region->Identifier so that I know the place and do some further tasks. –  Mar 03 '15 at 12:53
  • Is there any way to write the response as an alert message after local notification is generated? Or can you please give me the response, if you have any idea? –  Mar 03 '15 at 12:55
  • I have to write location manager delegate method in my View Controller. Please correct me if I am wrong. –  Mar 03 '15 at 13:07
  • yes u have to use the location manager delegate method in viewcontroller – Dheeraj Singh Mar 03 '15 at 13:08
  • I have done the same as you mentioned but performAction method is not called. Can I get the UUID (Region Identifier) AppDelegate. Actually I have to know the UUID even if your code works because on the basis of this UUID I have to done further tasks. –  Mar 03 '15 at 13:20
  • declare your uuid globally.. or is there going to be different uuids – Dheeraj Singh Mar 05 '15 at 08:24
  • I can't declare it globally. It's different for different monitoring regions. –  Mar 07 '15 at 13:53