使用Apple's documentation,Stackoverflow,Google尝试了一段时间后,当我进入或退出我正在监控的区域时,我仍然无法显示简单的UIAlertView。
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.distanceFilter = kCLDistanceFilterNone;
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
self.locationManager.delegate = self;
[locationManager requestAlwaysAuthorization];
for (CLRegion *monitored in [self.locationManager monitoredRegions])
[locationManager stopMonitoringForRegion:monitored];
CLLocationCoordinate2D coord;
coord.latitude = 31.521603;
coord.longitude = 33.01190;
CLCircularRegion *region = [[CLCircularRegion alloc]initWithCenter:coord radius:20 identifier:@"work"];
- (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region
{
[self showMessage:[NSString stringWithFormat:@"EnterRegion: %@",region.identifier] andMessage:@"Welcome Home" andButton:@"Ok"];
}
- (void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region
{
[self showMessage:[NSString stringWithFormat:@"ExitRegion: %@",region.identifier] andMessage:@"Bye Bye" andButton:@"Ok"];
}
-(void)locationManager:(CLLocationManager *)manager didStartMonitoringForRegion:(CLRegion *)region {
NSLog(@"Now monitoring for %@", region.identifier);
}
- (void)locationManager:(CLLocationManager *)manager monitoringDidFailForRegion:(CLRegion *)region withError:(NSError *)error{
[self showMessage:@"msg" andMessage:[NSString stringWithFormat:@"Region Failed:%@",region.identifier] andButton:@"ok"];
}
- (void)locationManager:(CLLocationManager *)manager
didDetermineState:(CLRegionState)state forRegion:(CLRegion *)region
{
[self showMessage:[NSString stringWithFormat:@"Did Determine State: %@. state:%@",region.identifier, (state == CLRegionStateUnknown) ? @"Unknown" : (state == CLRegionStateInside) ? @"INSIDE" : @"OUTSIDE"] andMessage:@"Determined" andButton:@"Ok"];
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{ }
showMessage
只是向用户弹出的UIAlertView。
有时候,当我设置区域时,我会得到monitoringDidFailForRegion
。
我在想是否应该将我的应用标记为使用"背景模式"对于位置更新,但这没有改变。好像它应该工作,但我永远不会得到didEnterRegion或didExitRegion被调用。
我应该指出,我使用所需的密钥更新了info.plist
:
<key>NSLocationAlwaysUsageDescription</key>
<string>Location is required for geofence</string>
Background app refresh
设置为On
,已启用位置。在外面,物理上进行测试,我无法获得要调用的功能
什么是错误的?是不是太短了20米来定义一个地区?
可能是区域监控不适用于前台的应用程序吗?沉默回调?