6

Im currently working with the "Regions" Sample code: https://developer.apple.com/library/ios/#samplecode/Regions/Introduction/Intro.h tml#//apple_ref/doc/uid/DTS40010726-Intro-DontLinkElementID_2

I'd like to take it a step further and generate, or fire, a notifcation when the user exits the region (can be for both enter & exit, i don't mind, whatever is easiest for an initial implementation).

I have been looking at the CLLocation Class reference, Location Awareness Programming Guide and Local and Push Notification Programming guide. And im suffering from information overload.

Many thanks :)

EDIT: I think i may have an idea which solves the problem: in the RegionsViewController implementation file there is this:

- (void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region        {
    NSString *event = [NSString stringWithFormat:@"didExitRegion %@ at %@", region.identifier, [NSDate date]];

    [self updateWithEvent:event];
}

Since i want to implement a local notification when the user exits the designated region boundary i've input this:

- (void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region {
    NSString *event = [NSString stringWithFormat:@"didExitRegion %@ at %@", region.identifier, [NSDate date]];
    [self updateWithEvent:event];
    //implement local notification: 
    UIApplication *app                = [UIApplication sharedApplication];
    UILocalNotification *notification = [[UILocalNotification alloc] init];
    [[UIApplication sharedApplication] cancelAllLocalNotifications];

    if (notification == nil)
        return;

    notification.alertBody = [NSString stringWithFormat:@"Did You Lock Your House?"];
    notification.alertAction = @"Lock House";
    notification.soundName = UILocalNotificationDefaultSoundName; 
    notification.applicationIconBadgeNumber = 1;
    [app presentLocalNotificationNow:notification];

    [notification release];
}

Could somebody advice me as to whether this is correct, or if there are any recommendations? (apologies for the poor formatting)

klefevre
  • 8,595
  • 7
  • 42
  • 71
Dan_R
  • 191
  • 1
  • 3
  • 14

1 Answers1

1

you are correct, there is no better way than firing a local notification from locationManager:didExitRegion: Moreover, this will work even if your app is running in background or closed.

Laurent
  • 318
  • 3
  • 9