我正致力于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
还在以下添加的背景模式下:
应用程序注册位置更新
App使用CoreBluetooth共享数据
App使用CoreBluetooth进行通信
应用下载内容以响应推送通知
iOS 7上的一切正常,也适用于iOS 8.4的iOS模拟器
它无法在设备上运行(iOS 8.4,iPad和iPod)
我已经通过wifi检查了。
我也开启了'General' - > '后台应用刷新'在设置
我不知道自己错过了什么?
欢迎所有建议。提前谢谢。
答案 0 :(得分:0)
您是否尝试过检查该地区是否真正开始跟踪?此方法将返回错误。
- (void)locationManager:monitoringDidFailForRegion:withError:
如果在从gps获得好位置之前调用start monitoringForRegion
,则可能会出错。
此外,您可能需要检查CLRegionStateUnknown
中的状态didDetermineState
,因为这也是一个选项。