问题
I am having difficulties getting this to work for when the app is not running. I have locationManager:didRangeBeacons:inRegion:
implemented and it is called when the app is running in the foreground or background, however it doesn't seem to do anything when I quit the app and lock the screen. The location services icon goes away and I never know that I entered a beacon range. Should the LocalNotification still work?
I have Location updates and Uses Bluetooth LE accessories selected in Background Modes (XCode 5) I didn't think I needed them.
Any help greatly appreciated.
-(void)watchForEvents { // this is called from application:didFinishLaunchingWithOptions
id class = NSClassFromString(@"CLBeaconRegion");
if (!class) {
return;
}
CLBeaconRegion * rflBeacon = [[CLBeaconRegion alloc] initWithProximityUUID:kBeaconUUID identifier:kBeaconString];
rflBeacon.notifyOnEntry = YES;
rflBeacon.notifyOnExit = NO;
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
[self.locationManager startRangingBeaconsInRegion:rflBeacon];
[self.locationManager startMonitoringForRegion:rflBeacon];
}
-(void)locationManager:(CLLocationManager *)manager didRangeBeacons:(NSArray *)beacons inRegion:(CLBeaconRegion *)region {
if (beacons.count == 0 || eventRanged) { // breakpoint set here for testing
return;
}
eventRanged = YES;
if (backgroundMode) { // this is set in the EnterBackground/Foreground delegate calls
UILocalNotification *notification = [[UILocalNotification alloc] init];
notification.alertBody = [NSString stringWithFormat:@"Welcome to the %@ event.",region.identifier];
notification.soundName = UILocalNotificationDefaultSoundName;
[[UIApplication sharedApplication] presentLocalNotificationNow:notification];
}
// normal processing here...
}
回答1:
Monitoring can launch an app that isn't running. Ranging cannot.
The key to having monitoring launch your app is to set this poorly documented flag on your This can launch your app on a region transition even after completely rebooting your phone. But there are a couple of caveats:CLBeaconRegion
: region.notifyEntryStateOnDisplay = YES;
- Your app launches into the background only for a few seconds. (Try adding
NSLog
statements toapplicationDidEnterBackground
and other methods in your AppDelegate to see what is going on.) - iOS can take its own sweet time to decide you entered a
CLBeaconRegion
. I have seen it take up to four minutes.
As far as ranging goes, even though you can't have ranging wake up your app, you can make your app do both monitoring and ranging simultaneously. If monitoring wakes up your app and puts it into the background for a few seconds, ranging callbacks start up immediately. This gives you a chance to do any quick ranging actions while your app is still running.
EDIT: Further investigation proves that notifyEntryStateOnDisplay
has no effect on background monitoring, so the above should work regardless of whether you have this flag. See this detailed explanation and discussion of delays you may experience
回答2:
Code for iOS 9 to range beacons in the background, by using Location Updates:
Open Project Settings -> Capabilities -> Background Modes -> Toggle
Location Updates
andUses Bluetooth LE accessories
toON
.Create a
CLLocationManager
, requestAlways
monitoring authorization (don't forget to add theApplication does not run in background
toNO
andNSLocationAlwaysUsageDescription
in the app'sinfo.plist
) and set the following properties:locationManager!.delegate = self locationManager!.pausesLocationUpdatesAutomatically = false locationManager!.allowsBackgroundLocationUpdates = true
Start ranging for beacons and monitoring region:
locationManager!.startMonitoringForRegion(yourBeaconRegion) locationManager!.startRangingBeaconsInRegion(yourBeaconRegion) locationManager!.startUpdatingLocation() // Optionally for notifications UIApplication.sharedApplication().registerUserNotificationSettings( UIUserNotificationSettings(forTypes: .Alert, categories: nil))
Implement the
CLLocationManagerDelegate
and in yourdidEnterRegion
send bothstartRangingBeaconsInRegion()
andstartUpdatingLocation()
messages (optionally send the notification as well) and set thestopRangingBeaconsInRegion()
andstopUpdatingLocation()
indidExitRegion
Be aware that this solution works but it is not recommended by Apple due to battery consumption and customer privacy!
More here: https://community.estimote.com/hc/en-us/articles/203914068-Is-it-possible-to-use-beacon-ranging-in-the-background-
回答3:
Here is the process you need to follow to range in background:
- For any
CLBeaconRegion
always keep monitoring on, in background or foreground and keepnotifyEntryStateOnDisplay = YES
notifyEntryStateOnDisplay
callslocationManager:didDetermineState:forRegion:
in background, so implement this delegate call...
...like this:
- (void)locationManager:(CLLocationManager *)manager didDetermineState:(CLRegionState)state forRegion:(CLRegion *)region{
if (state == CLRegionStateInside) {
//Start Ranging
[manager startRangingBeaconsInRegion:region];
}
else{
//Stop Ranging
[manager stopRangingBeaconsInRegion:region];
}
}
I hope this helps.
回答4:
You are doing two separate operations here - 'ranging' beacons and monitoring for a region. You can monitor for a region in the background, but not range beacons.
Therefore, your implementation of locationManager:didRangeBeacons:inRegion:
won't get called in the background. Instead, your call to startMonitoringForRegion
will result in one / some of the following methods being called:
– locationManager:didEnterRegion:
– locationManager:didExitRegion:
– locationManager:didDetermineState:forRegion:
These will get called in the background. You can at that point trigger a local notification, as in your original code.
回答5:
Your app should currently wake up if you're just wanting to be notified when you enter a beacon region. The only background restriction I know of concerns actually hosting an iBeacon on an iOS device. In that case, the app would need to be physically open in the foreground. For that situation, you'd be better off just doing the straight CoreBluetooth CBPeripheralManager
implementation. That way you'd have some advertising abilities in the background.
回答6:
You can do ranging in background. However, there is a catch. The user has to bring the app to foreground, which is when you start ranging. Then, even if the user locks the screen and the app goes in the background, it will continue to range and call the didRangeBeacons
callback every 1 second forever.
To turn on background location updates, select your project, go to Info and add a row called "Required Background Modes". Then add relevant items: location updates, communicates / shares data via corebluetooth, etc.
Now, once you get a callback when you enter the region, your app will wake up in the background and notify the user. The user opens the app to bring the app in the foreground. At this point you can start the ranging process by calling startRangingBeaconsInRegion
. In addition, call the method startUpdatingLocation
which will update the location in background.
来源:https://stackoverflow.com/questions/19141779/ranging-beacons-only-works-when-app-running