Ios 开始监视区域不工作,但已正确实施

Ios 开始监视区域不工作,但已正确实施,ios,core-location,geofencing,Ios,Core Location,Geofencing,我一直在努力让地理围栏发挥作用,但似乎没有发生 我得到了didStartMonitoringForRegion委托方法,但是didEnterRegion和didExitRegion在应该调用的时候没有被调用。如果我把模拟器设置在1000公里以外的地方,我就可以让didExitRegion开火,但显然这不够精确 我想我一定是错误地实现了它,所以我从本教程下载了源文件: 但是,我得到了完全相同的行为(实现时didStartMonitoringForRegion激发,但进入/退出不激发),这让我认为

我一直在努力让地理围栏发挥作用,但似乎没有发生

我得到了didStartMonitoringForRegion委托方法,但是didEnterRegion和didExitRegion在应该调用的时候没有被调用。如果我把模拟器设置在1000公里以外的地方,我就可以让didExitRegion开火,但显然这不够精确

我想我一定是错误地实现了它,所以我从本教程下载了源文件:

但是,我得到了完全相同的行为(实现时didStartMonitoringForRegion激发,但进入/退出不激发),这让我认为我的问题不在于代码。我在模拟器和实际设备上都试过了,但都不起作用


我不知道我错过了什么

首先,您应该在要添加区域的类中实现此委托:

- (void)locationManager:(CLLocationManager *)manager monitoringDidFailForRegion:(CLRegion *)region withError:(NSError *)error
因此,您可以知道某个位置是否注册失败(可能是因为您提供的坐标无效,或者您超出了应用程序可以注册的最大区域数)

第二,当你在模拟器上测试它时,你需要使用一个GPX文件来模拟你向一个区域移动,而不仅仅是改变模拟器的位置

下面是一个走向曼哈顿中心的gpx文件示例:

<?xml version="1.0" encoding="UTF-8"?>
<gpx>
    <wpt lat="40.737181" lon="-73.98"></wpt>
    <wpt lat="40.733604" lon="-73.992110"></wpt>
</gpx>
常量
kSystemVersion
对于iOS版本来说只是一个常量,正如苹果建议的那样,根据操作系统版本的不同,使用不同的精度级别


请尝试我的建议,看看是否有更新,如果没有,请发布整个区域监控代码,因为您可能做错了什么,从您刚才说的“我无法检测”

您的设备设置中是否启用了后台应用程序刷新?失败的另一个原因是cllocation manager的委托,请确保在实现did enter/did exit的位置设置了location manager的委托。或者只需重新检查CLRegion。如果你从不从内到外或反向,你将永远不会被告知这一点。
- (void)registerRegionWithenterPoint:(CLLocationCoordinate2D)centerPoint regionRadius:(CLLocationDistance)radius andIdentifier:(NSString*)identifier {

    // If the radius is too large, registration fails automatically,
    // so clamp the radius to the max value.    
    if (radius > self.locationManager.maximumRegionMonitoringDistance)
        radius = self.locationManager.maximumRegionMonitoringDistance;

    // Create the region and start monitoring it.
    CLRegion* region = [[CLRegion alloc] initCircularRegionWithCenter:centerPoint radius:radius identifier:identifier];
    [self.locationManager startMonitoringForRegion:region desiredAccuracy:(kSystemVersion>=6)?400:100];
}