Reputation: 23
So I'm creating an iOS app, and I'm ranging for beacons in the background. It works well once my iPhone is awake and it continues to work even when the iPhone is locked...however the iPhone must still be awake. Once the iPhone goes to sleep my app ranges about 10 more times and then stops. If you wake the iPhone up it starts ranging again.
I've tried monitoring as well but no luck.
Can anyone tell me if this if even possible? I've searched everywhere and can't find an answer! Please find my beacon methods (which are in the AppDelegate) below
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[[UIApplication sharedApplication] setMinimumBackgroundFetchInterval:UIApplicationBackgroundFetchIntervalMinimum];
self.locationManager = [[CLLocationManager alloc] init];
if([self.locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]) {
[self.locationManager requestAlwaysAuthorization];
}
self.locationManager.delegate = self;
self.locationManager.pausesLocationUpdatesAutomatically = NO;
[self.locationManager startUpdatingLocation];
return YES;
}
-(void)locationManager:(CLLocationManager *)manager didRangeBeacons:(NSArray *)beacons inRegion:(CLBeaconRegion *)region {
if(beacons.count > 0) {
CLBeacon *nearestBeacon = beacons.firstObject;
if (nearestBeacon.proximity == CLProximityImmediate || nearestBeacon.proximity == CLProximityNear) {
NSLog("Beacon detected");
}
}
}
- (void)startRangingBeacons {
self.beaconRegion = [[CLBeaconRegion alloc] initWithProximityUUID:@"EBEFD083-70A2-47C8-9837-E7B5634DF525" identifier:@"receptionBeacon"];
[self.locationManager startRangingBeaconsInRegion:self.beaconRegion];
}
Any help is appreciated Thanks Sonia
Upvotes: 2
Views: 494
Reputation: 764
There is one way to awake your phone. If your app comes under the region of beacon then,
- (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region
above method gets called automatically. So you need to put some local notification like "You are near to beacon zone" to awake the phone. But you must reenter into the region.
Once your app enter in the region write following code to restart the beacon process,
[self.locationManager startRangingBeaconsInRegion:self.beaconRegion];
UIBackgroundTaskIdentifier bgTask = [[UIApplication sharedApplication]
beginBackgroundTaskWithExpirationHandler:^{
NSLog(@"End of tolerate time. Application should be suspended now if we do not ask more 'tolerance'");
[[UIApplication sharedApplication] endBackgroundTask:UIBackgroundTaskInvalid];
}];
if (bgTask == UIBackgroundTaskInvalid)
{
NSLog(@"This application does not support background mode");
} else {
//if application supports background mode, we'll see this log.
NSLog(@"Application will continue to run in background");
}
Hope this will work for you.
Upvotes: 1