我可以使用CoreLocation框架获取当前位置。我想注册我当前的位置,以便我想知道谁进入和退出该地区。为此,我使用Apple记录的方法来注册- (void)registerRegionWithCircularOverlay:(MKCircle*)overlay andIdentifier:(NSString*)identifier { }
的区域。我的问题是这个方法何时被调用?
任何帮助?
答案 0 :(得分:1)
您必须注意访问用户设备位置的权限。因此该应用程序能够跟踪用户位置。
您可以通过以下方式执行此操作:
NSLocationWhenInUseUsageDescription
。这个键的值为String
类型。您必须注意不要使用String
以外的其他类型。
在访问用户位置之前询问用户,添加以下行。
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
[self.locationManager requestWhenInUseAuthorization];
下面的代码将开始监控区域:
// Tell location manager to start monitoring for the region
[self.locationManager startMonitoringForRegion:self.myBeaconRegion];
还覆盖以下方法以确保发现该区域:
- (void)locationManager:(CLLocationManager*)manager didEnterRegion:(CLRegion *)region
{
// We entered a region!
NSLog(@"Entered in region");
}
-(void)locationManager:(CLLocationManager*)manager didExitRegion:(CLRegion *)region
{
// Exited the region
NSLog(@"Exited from region");
}