【发布时间】:2015-10-01 14:34:18
【问题描述】:
我正在为 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+ 的 plist 文件中添加了密钥 "NSLocationAlwaysUsageDescription"
下面还添加了背景模式:
应用注册位置更新
应用使用 CoreBluetooth 共享数据
应用使用 CoreBluetooth 进行通信
应用下载内容以响应推送通知
在 iOS 7 上一切正常,在 iOS 8.4 的 iOS 模拟器中也能正常工作
无法在设备上运行(iOS 8.4,iPad 和 iPod)
我已经检查了 wifi 是否打开。
我还在设置中打开了“常规”->“后台应用刷新”
我不知道我错过了什么?
欢迎所有建议。提前致谢。
【问题讨论】:
-
您能解释一下在设备上运行时您是如何测试应用程序的吗?它是通过 Xcode 模拟的跑腿工作还是位置(提示:结果可能不同)
-
@AlexPavlov :感谢您的即时回复。我所做的是,我添加了半径为 1000m 的当前位置坐标来创建区域。然后添加该区域进行监控。所以,它应该调用determineState 方法。但它没有。虽然同样的事情在我的 iOS 模拟器 8.4 上工作
标签: ios objective-c cllocationmanager geofencing ios8.4