matt---
matt---

Reputation: 2470

Why does UILocalNotification with region not fire?

I have followed the example given in the WWDC 2014 session "What's New in iOS Notifications" to set up a UILocalNotification delivery when a region's boundary is crossed.

However, my notification never fires on device nor simulator running iOS beta 3. The notification works fine when I tested using a fireDate instead of a region (not both at same time-- that's not allowed).

I have set NSLocationWhenInUseUsageDescription in my Info.plist.

Here is my Obj-C code:

#import "ViewController.h"
#import <CoreLocation/CoreLocation.h>

@interface ViewController () <CLLocationManagerDelegate>

@property (nonatomic, strong) CLLocationManager *locationManager;
@property (nonatomic, strong) CLLocation *notifLocation;

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    // test points at Apple HQ
    self.notifLocation = [[CLLocation alloc] initWithLatitude:37.331741 longitude:-122.030333];

    [self setupLocationMonitoring];
}

- (void) setupLocationMonitoring
{
    if (self.locationManager == nil) {
        self.locationManager = [[CLLocationManager alloc] init];
    }

    self.locationManager.delegate = self;
    self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    self.locationManager.distanceFilter = 10; // meters

    // iOS 8+ request authorization to track the user’s location
    if ([self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
        [self.locationManager requestWhenInUseAuthorization];
    }

    [self.locationManager startUpdatingLocation];
}

- (void)registerLocationNotification
{
    UILocalNotification *locNotification = [[UILocalNotification alloc] init];
    locNotification.alertBody = @"Hello!";

    // doesn't work
    locNotification.regionTriggersOnce = NO;
    locNotification.region = [[CLCircularRegion alloc] initWithCenter:self.notifLocation.coordinate radius:50 identifier:@"PlaceName"];

    // works:
//    locNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:10];

    UIApplication *application = [UIApplication sharedApplication];
    [application cancelAllLocalNotifications];
    [application scheduleLocalNotification:locNotification];
}

#pragma mark - CLLocationManagerDelegate

- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status
{
    // check status to see if we’re authorized
    BOOL canUseLocationNotifications = (status == kCLAuthorizationStatusAuthorizedWhenInUse);
    if (canUseLocationNotifications) {
        [self registerLocationNotification];
    }
}

@end

Note: this question is about iOS 8's new Location Notifications, not about region monitoring on older versions of iOS.

Upvotes: 1

Views: 1890

Answers (1)

Alex Peda
Alex Peda

Reputation: 4290

Your code works fine for me. Here are the steps on iPhone Simulator:

1) Open Simulator app and choose the following option from the top menu Debug -> Location -> Custom Location and put there the coordinates as you have in code: (37.331741, -122.030333)

2) Run your Xcode project on iPhone Simulator

3) Background the app

4) Open the same top menu item Debug -> Location but change it from Custom Location to Freeway Drive or City Run. Play with City Run/ Freeway Drive and Custom Location

I see the notification when I follow these steps. Looks like it doesn't work in case appropriate coordinate is a starting one but it works when the coordinate changes to appropriate from some others.

Upvotes: 4

Related Questions