【发布时间】:2015-05-22 12:49:22
【问题描述】:
当iOS设备范围内有信标时,我会收到通知(CLRegionStateInside)并可以开始测距。这工作正常。但是,当测距开始并且 iOS 设备不再在范围内时,我不会收到通知(状态不会更改为 CLRegionStateOutside)。不在前台或后台。
还有didEnterRegion 和didExitRegion 永远不会被调用。当状态为CLRegionStateInside 时,我开始在didDeterminState 范围内。
- 我确实为我的应用启用了后台刷新设置。
- 当我第一次启动应用程序时,我确实收到了一条询问位置许可的警报。
所以基本上,我可以开始测距,但我无法停止测距,因为状态不会更改为CLRegionStateOutside。
我在 iPhone 4s 上使用 Xcode 6.3.1、iOS 8.3。
这是我的代码:
初始化:
- (id)init{
self = [super init];
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
if ([self.locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]) {
[self.locationManager requestAlwaysAuthorization]; //or requestWhenInUseAuthorization
}
NSUUID *uuid = [[NSUUID alloc] initWithUUIDString:@"B75FA2E9-3D02-480A-B05E-0C79DBB922AD"];
self.myBeaconRegion = [[CLBeaconRegion alloc] initWithProximityUUID:uuid
identifier:@"TESTREGIO"];
[self.locationManager startMonitoringForRegion:self.myBeaconRegion];
[self.locationManager requestStateForRegion:self.myBeaconRegion];
self.myBeaconRegion.notifyEntryStateOnDisplay = YES;
self.myBeaconRegion.notifyOnEntry = YES;
self.myBeaconRegion.notifyOnExit = YES;
return self;
}
确定:
- (void)locationManager:(CLLocationManager *)manager didDetermineState:(CLRegionState)state forRegion:(CLRegion *)region {
if (state == CLRegionStateInside)
{
[self.locationManager startRangingBeaconsInRegion:self.myBeaconRegion];
}else{
[self.locationManager stopRangingBeaconsInRegion:self.myBeaconRegion];
}
}
DIDENTERREGION 和 DIDEXITREGION:
- (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion*)region
{
NSLog(@"DidenterRegion================================================");
}
- (void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region {
NSLog(@"DidexitRegion================================================");
}
【问题讨论】:
标签: ios objective-c monitoring ibeacon