我正在为 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
下面还添加了背景模式:
应用程序注册位置更新
应用程序使用 CoreBluetooth 共享数据
应用程序使用 CoreBluetooth 进行通信
应用程序下载内容以响应推送通知
在 iOS 7 上一切正常,在 iOS 8.4 的 iOS 模拟器中也能正常工作
它不适用于设备(iOS 8.4,iPad 和 iPod)
我已经检查了wifi是否打开。
我还在设置中打开了“常规”->“后台应用刷新”
我不知道我错过了什么?
欢迎所有建议。提前致谢。