我正在为Goefencing进行演示。
这是我的代码,
- (void) registerRegionForMonitoring {
if (self.locationManager == nil) {
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
self.locationManager.distanceFilter = kCLDistanceFilterNone;
if ([self.locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]) {
[self.locationManager requestAlwaysAuthorization];
}
[self.locationManager startUpdatingLocation];
}
if(![CLLocationManager isMonitoringAvailableForClass:[CLCircularRegion class]]) {
[Utilities showAlertWithTitle:@"Geofence" andMessage:@"This app requires region monitoring features which are unavailable on this device."];
return;
}
for(NSDictionary *dictionary in geofences) {
NSString *geofenceId = [NSString stringWithFormat:@"%@", [dictionary valueForKey:@"geofence_id"]];
CLLocationDegrees latitude = [[dictionary valueForKey:@"latitude"] doubleValue];
CLLocationDegrees longitude =[[dictionary valueForKey:@"longitude"] doubleValue];
CLLocationCoordinate2D centerCoordinate = CLLocationCoordinate2DMake(latitude, longitude);
CLLocationDistance regionRadius = [[dictionary valueForKey:@"radius"] doubleValue];
CLRegion *geofenceRegion = [[CLCircularRegion alloc] initWithCenter:centerCoordinate radius:regionRadius identifier:geofenceId];
[self.locationManager startMonitoringForRegion:geofenceRegion];
}
}
这是委托方法
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
CLLocation *latestLocation = [locations lastObject];
self.currentLocation = latestLocation;
}
- (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region {
[Utilities showAlertWithTitle:@"Geofence" andMessage:[NSString stringWithFormat:@"You are in region with identifire %@", region.identifire]];
}
- (void)locationManager:(CLLocationManager *)manager didStartMonitoringForRegion:(CLRegion *)region {
[self.locationManager requestStateForRegion:region];
}
- (void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region {
[Utilities showAlertWithTitle:@"Geofence" andMessage:[NSString stringWithFormat:@"You are out of region with identifire %@", region.identifire]];
}
- (void)locationManager:(CLLocationManager *)manager didDetermineState:(CLRegionState)state forRegion:(CLRegion *)region {
if (state == CLRegionStateInside) {
// if user already in the region, when monitoring is started
[Utilities showAlertWithTitle:@"Geofence" andMessage:[NSString stringWithFormat:@"You are in region with identifire %@", region.identifire]];
}
}
我还在iOS 8+的
"NSLocationAlwaysUsageDescription"
文件中添加了密钥plist
还在下面添加了背景模式:
一切都可以在iOS 7上正常运行,也可以在iOS 8.4的iOS仿真器中运行
在设备(iOS 8.4,iPad和iPod上)均不起作用
我已经检查了wifi。
我还已在“设置”中打开了“常规”->“后台应用程序刷新”
我不知道我在想什么?
欢迎所有建议。提前致谢。
最佳答案
您是否尝试过检查区域是否真正开始跟踪?此方法将返回错误。
- (void)locationManager:monitoringDidFailForRegion:withError:
如果您在从GPS获得好的位置之前调用start
monitoringForRegion
,则可能会收到错误消息。另外,您可能还想检查
CLRegionStateUnknown
中的didDetermineState
状态,因为这也是一个选项。关于ios - 在iOS 8.4(设备)上未调用CLLocationManager didEnterRegion,didExitRegion和didDetermineState的委托(delegate)方法,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31381500/