我是iOS开发的新手,我正在尝试创建一个允许区域监控的应用程序。类似于iOS Reminders App。
我有我的代码,我开始监控区域,它应该在我进入和离开区域时通知我。但是,它只告诉我我在该地区的时间。当我离开时,它并没有告诉我。
就是你知道,我正在我的iPhone上测试这个,并且绝对超出了100米范围(我开车离开我家10分钟)。它不停地传递消息说我在该地区,但在我离开该地区时从未改变过。我真的很难过我错了。有人可以帮忙吗?另外,出于好奇,因为我是iOS开发的新手,我是否正确地采用这种方式?或者有更有效的方法来实现这一目标吗?任何建议,教程,howto's将非常感谢。多谢你们!我的代码开始区域监控和方法如下。如果您需要任何其他代码,请与我们联系。
(IBAction)addRegion:(id)sender {
if ([CLLocationManager isMonitoringAvailableForClass:[CLBeaconRegion class]]) {
// Create a new region based on the center of the map view.
CLLocationCoordinate2D coord = CLLocationCoordinate2DMake(_mapView.centerCoordinate.latitude, _mapView.centerCoordinate.longitude);
region = [[CLCircularRegion alloc] initWithCenter:coord
radius:Radius
identifier:[NSString stringWithFormat:@"%f, %f", _mapView.centerCoordinate.latitude, _mapView.centerCoordinate.longitude]];
// Start monitoring the newly created region.
[_locationManager startMonitoringForRegion:(CLRegion *)region];
}
else {
NSLog(@"Region monitoring is not available.");
}
}
(IBAction)stopMonitoringRegion:(id)sender {
[_locationManager stopMonitoringForRegion:(CLRegion *)region];
}
(void)locationManager:(CLLocationManager *)manager
didDetermineState:(CLRegionState)state forRegion:(CLRegion *)reg {
if (state == CLRegionStateInside) {
//call didEnterRegion
//[_locationManager stopMonitoringForRegion:region];
[self locationManager:_locationManager didEnterRegion:region];
}
if (state == CLRegionStateOutside) {
//call didExitRegion
//[_locationManager stopMonitoringForRegion:region];
[self locationManager:_locationManager didExitRegion:region];
}
}
(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
NSLog(@"didFailWithError: %@", error);
}
(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
//requestStateForRegion
if (region == nil) {
//do nothing
NSLog(@"didUpdateToLocation %@ from %@", newLocation, oldLocation);
}else{
[_locationManager requestStateForRegion:region];
NSLog(@"didUpdateToLocation %@ from %@", newLocation, oldLocation);
}
}
(void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region {
NSLog(@"didEnterRegion %@ at %@", region.identifier, [NSDate date]);
}
(void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region {
NSLog(@"didExitRegion %@ at %@", region.identifier, [NSDate date]);
}
(void)locationManager:(CLLocationManager *)manager monitoringDidFailForRegion:(CLRegion *)region withError:(NSError *)error {
NSLog(@"monitoringDidFailForRegion %@: %@ Error: %@", region.identifier, [NSDate date], error);
}