问题
I have a problem with region monitoring while the app is in background. Region enters and exits get called if the app is in foreground, but doesn't while in the background (sometimes they fire but very rarely).
How does beacon region monitoring work for iOS 8.1.1 ? Should region enters/exit fire instantly when in Beacon proximity while in background ?
What should I do to make sure it works ?
Do Background Modes
: Location Updates
or Uses Bluetooth LE accessories
have to be on for background beacon monitoring to work ? GeoFencing worked for me without having these on.
What I've already done:
set these for every region:
beaconRegion.notifyOnExit=YES; beaconRegion.notifyOnEntry=YES; beaconRegion.notifyEntryStateOnDisplay = YES;
- Made sure that everything in settings is in order (app refresh etc.)
EDIT:
I've created a new project and it still doesn't work. Here's the code:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
_locationManager = [[CLLocationManager alloc] init];
_locationManager.pausesLocationUpdatesAutomatically = NO;
_locationManager.desiredAccuracy = 25;
_locationManager.delegate = self;
[_locationManager requestAlwaysAuthorization];
[_locationManager startUpdatingLocation];
CLBeaconRegion* reg =[self prepareBeacon:@"here i put my UUID" :446 :2196];
[_locationManager startMonitoringForRegion:reg];
[_locationManager startRangingBeaconsInRegion:reg];
return YES;
}
-(CLBeaconRegion*)prepareBeacon:(NSString*)uuid :(int)maj :(int)min
{
NSString* identifier = [NSString stringWithFormat:@"%@,%d,%d", uuid, maj, min];
CLBeaconRegion *beaconRegion = [[CLBeaconRegion alloc] initWithProximityUUID:[[NSUUID alloc] initWithUUIDString:uuid] major:maj minor:min identifier:identifier];
beaconRegion.notifyOnExit=YES;
beaconRegion.notifyOnEntry=YES;
beaconRegion.notifyEntryStateOnDisplay = YES;
return beaconRegion;
}
-(void)locationManager:(CLLocationManager *)manager didDetermineState:(CLRegionState)state forRegion:(CLRegion *)region
{
}
-(void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region
{
}
-(void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region
{
}
-(void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status
{
}
-(void)locationManager:(CLLocationManager *)manager didRangeBeacons:(NSArray *)beacons inRegion:(CLBeaconRegion *)region
{
}
some additional info:
- Except creating a fresh iOS8 project and adding code I've added
NSLocationAlwaysUsageDescription
to *.plist file. - I put breakpoints in
didEnterRegion
anddidExitRegion
. It works in foreground, doesn't work while in background (iPhone in home screen or locked) - Testing on 4S, iOS 8.1.1
回答1:
You need to make sure you have called the method under CCLocationManager
- (void) requestAlwaysAuthorization
This allows your app to be updated about changes in the foreground and background compared to the line below that only allows your app to be notified while in the foreground:
- (void) requestWhenInUseAuthorization
Once the user responds to the request the following method will be called with the updated authorization status:
- (void) locationManager: (CLLocationManager*) manager
didChangeAuthorizationStatus: (CLAuthorizationStatus) status
Source: https://developer.apple.com/library/ios/documentation/CoreLocation/Reference/CLLocationManager_Class/index.html#//apple_ref/occ/cl/CLLocationManager
回答2:
The answer is: background region monitoring is a mystery to us users. Sometimes it fires up in a second, sometimes it takes longer. It depends on a lot of factors but my main problem was that I was using iPhone 4s.
Maybe this will help anyone without losing so much time: 4s sucks at background beacons scanning
source: tested on two 4S phones with latest iOS and iPhone 6. iPhone6 gets beacon notifications in a matter of seconds.
来源:https://stackoverflow.com/questions/27380241/region-monitoring-beacon-regions-in-background-not-working