【发布时间】:2016-04-29 07:06:55
【问题描述】:
我正在尝试编写一个应用程序,在用户进入特定区域时通知用户(我有 20 个区域正在根据用户的当前位置进行更新)。
我在-locationManager: didEnterRegion: 上添加了一个断点,但它从未被调用过,尽管用户的当前位置在该区域内(使用模拟器)。
如果有人能够帮助我发现问题,那真的会对我有所帮助。
谢谢!
我的代码:
remindersViewController.m:
-(void)startMonitoringClosestStores
{
[self stopMonitoringStores];
if (![CLLocationManager isMonitoringAvailableForClass:[CLCircularRegion class]]) {
NSLog(@"Monitoring is not available for CLCircularRegion class");
}
for (Store *currentStore in self.twentyClosestStores) {
CLCircularRegion *region=[currentStore createCircularRegion];
[self.locationManager startMonitoringForRegion:region];
}
}
-(void)stopMonitoringStores
{
for (Store *currentStore in self.twentyClosestStores) {
CLCircularRegion *region=[currentStore createCircularRegion];
[self.locationManager stopMonitoringForRegion:region];
}
}
-(void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region
{
NSLog (@"Enetred"); //Never called
}
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations
{
self.userLocation=[locations lastObject];
[self sortClosestStores];
}
Store.m:
-(CLCircularRegion*)createCircularRegion
{
CLCircularRegion *region=[[CLCircularRegion alloc] initWithCenter:self.geoPoint radius:1000 identifier:self.identifier];
region.notifyOnEntry=YES;
return region;
}
AppDelegate.m:
-(void)handleRegionEvent:(CLRegion*)region
{
NSLog(@"Geofence triggered");
}
-(void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region
{
NSLog(@"Entered");
}
【问题讨论】:
-
是否调用了剩余的方法?你在模拟器中模拟了当前位置吗?
-
请注意模拟器无法检测到您当前的位置。您必须模拟位置模拟器。
-
@Mr.T 实际上,现在我看到(CLLocationManager 的)另一个委托方法也没有被调用。但是
self.locationManager.delegate设置为self。那么可能是什么问题呢? -
你在接口部分实现了委托吗?它应该类似于
-
@Mr.T 是的,我做了(在 .h 文件上)。
标签: ios objective-c cllocationmanager geofencing