Search code examples
objective-cibeaconios7.1

Location detection wake up application


I have a iOS application that use iBeacons. I use the region monitor etc to setup the beacons etc.

When my application get terminated and i come within the region, iOS doesn't wake-up my terminated application.

Please advise.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    NSLog(@"applicationDidFinishLaunching");

    _locationManager = [[CLLocationManager alloc] init];
    _locationManager.delegate = self;
    _locationManager.desiredAccuracy=kCLLocationAccuracyBest;
    _locationManager.distanceFilter=1;
    _locationManager.pausesLocationUpdatesAutomatically = NO;
    CLBeaconRegion *region;

    region = [[CLBeaconRegion alloc] initWithProximityUUID:[[NSUUID alloc] initWithUUIDString:@"F7826DA6-4FA2-4E98-8024-BC5B71E0893E"] major: 1 minor: 1 identifier: @"Test"];
    [region setNotifyEntryStateOnDisplay:YES];
    [region setNotifyOnExit:YES];
    [region setNotifyOnEntry:YES];

    [_locationManager startMonitoringForRegion:region];
    [_locationManager startRangingBeaconsInRegion:region];

    [_locationManager startUpdatingLocation];

    return YES;
}



- (void)locationManager:(CLLocationManager *)manager didDetermineState:(CLRegionState)state forRegion:(CLRegion *)region
{
    NSLog(@"locationManager didDetermineState INSIDE for %@", region.identifier);

    UILocalNotification *notification = [[UILocalNotification alloc] init];
    notification.soundName = UILocalNotificationDefaultSoundName;
    notification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber]+1;

    notification.alertBody = @"Awake";

    [[UIApplication sharedApplication] presentLocalNotificationNow:notification];
}

- (void)locationManager:(CLLocationManager *)manager didRangeBeacons:(NSArray *)beacons inRegion:(CLBeaconRegion *)region
{
    NSLog(@"locationManager didRangeBeacons %d", beacons.count);
    CLBeacon *nearestExhibit = beacons && beacons.count > 0 ? [beacons firstObject] : nil;

    if (nearestExhibit &&
        (nearestExhibit.proximity == CLProximityImmediate ||
         nearestExhibit.proximity == CLProximityNear)
        && nearestExhibit.accuracy != -1
        && nearestExhibit.accuracy <= 10.0) {
        UILocalNotification *notification = [[UILocalNotification alloc] init];
        notification.soundName = UILocalNotificationDefaultSoundName;
        notification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber]+1;

        notification.alertBody = @"didRangeBeacons";

        [[UIApplication sharedApplication] presentLocalNotificationNow:notification];
   }

}

Solution

  • If by "terminated" you mean you have killed your app with the task switcher, then the behavior you describe is expected on iOS 7.0.x. As of iOS 7.1, the behavior has changed so your app gets notified the same as if it were never terminated.

    I have verified this functionality myself on 7.1. If you see something different on 7.1+, please double check that it works consistently without terminating your app.

    If the above does not help, please post your code so we can make sure it is set up properly.

    Update: After seeing your code, it appears that everything is set up properly. Make sure you do have iOS 7.1+ on the device with your aspp installed, then:

    1. Turn off the beacon.
    2. Terminate your app.
    3. Turn on a beacon with identifiers F7826DA6-4FA2-4E98-8024-BC5B71E0893E 1 1
    4. Wait up to 15 minutes for your notification.