iOS Geofence,监控启动时如何处理内部区域?

iOS Geofence,监控启动时如何处理内部区域?,第1张

概述我一直无法解决如何在调用startMonitoringForRegion时处理手机已经在某个区域内的情况?其他问题建议在didStartMonitoringForRegion中调用requestStateForRegion,然后调用方法didDetermineState:forRegion:.所以代码看起来像这样: - (void)viewDidLoad { //location mana 我一直无法解决如何在调用startMonitoringForRegion时处理手机已经在某个区域内的情况?其他问题建议在dIDStartMonitoringForRegion中调用requestStateForRegion,然后调用方法dIDDetermineState:forRegion:.所以代码看起来像这样:
- (voID)vIEwDIDLoad {    //location manager set up etc...    for (Object *object in allObjects){        CLRegion *region = [self geofenceRegion:object];        [locationManager startMonitoringForRegion:region];     }}- (voID)locationManager:(CLLocationManager *)manager dIDStartMonitoringForRegion:(CLRegion *)region {    [self.locationManager requestStateForRegion:region];    [self.locationManager performSelector:@selector(requestStateForRegion:) withObject:region afterDelay:5]; }- (voID)locationManager:(CLLocationManager *)manager  dIDDetermineState:(CLRegionState)state forRegion:(CLRegion *)region {    if (state == CLRegionStateInsIDe){        [self locationManager:locationManager dIDEnterRegion:region];    }  }

现在显然geofenceRegion方法是我自己的并且它工作正常,并且对象包含lat long和radius之类的东西,并且一切都很好,所以这不是问题.

无论如何,上面代码的问题在于,如果用户在将区域添加到其设备时已经在区域内(即完成了dIDEnterRegion),它确实有效.但问题是,每次根据apple docs划分其中一个边界区域时,方法dIDDetermineState:forRegion:也被调用:

The location manager calls this method whenever there is a boundary Transition for a region. It calls this method in addition to calling the locationManager:dIDEnterRegion: and locationManager:dIDExitRegion: methods. The location manager also calls this method in response to a call to its requestStateForRegion: method,which runs asynchronously.

现在因为每次输入一个区域,都会自动调用dIDEnterRegion,然后再次调用它,因为dIDDetermineState:forRegion:也会根据apple docs自动调用,这会导致再次调用dIDEnterRegion,因此当两次输入区域时我只希望它输入一次.我怎能避免这种情况?

谢谢你的帮助.

解决方案真的很简单,我只是以错误的方式去做.我必须选择使用2个方法dIDEnterRegion:和dIDExitRegion或使用dIDDetermineState:forRegion并创建我自己的方法来进入和退出该区域,两者都不应该使用.

所以我选择只使用dIDDetermineState:forRegion方法,我的代码现在看起来像这样:

请注意,使用此方法,如果不在内部,将为区域调用退出区域,如果像我一样,您只想在输入发生后退出,则需要某种方法来检查区域是否已输入(我自己使用核心数据,因为我已经使用它来存储区域的其他方面).

- (voID)vIEwDIDLoad {    //location manager set up etc...    for (Object *object in allObjects){        CLRegion *region = [self geofenceRegion:object];        [locationManager startMonitoringForRegion:region];     }}- (voID)locationManager:(CLLocationManager *)manager dIDStartMonitoringForRegion:(CLRegion *)region {    [self.locationManager performSelector:@selector(requestStateForRegion:) withObject:region afterDelay:5];}- (voID)locationManager:(CLLocationManager *)manager  dIDDetermineState:(CLRegionState)state forRegion:(CLRegion *)region {    if (state == CLRegionStateInsIDe){        [self enterGeofence:region];    } else if (state == CLRegionStateOutsIDe){        [self exitGeofence:region];    } else if (state == CLRegionStateUnkNown){        NSLog(@"UnkNown state for geofence: %@",region);        return;    }}- (voID)enterGeofence:(CLRegion *)geofence {    //whatever is required when entered}- (voID)exitGeofence:(CLRegion *)geofence {    //whatever is required when exit}
解决方法 只是不要使用locationManager:dIDEnterRegion:作为locationManager:dIDDetermineState:forRegion:为您提供触发入门代码所需的所有信息,顺便说一句,它不应该是locationManager:dIDEnterRegion :,使用您的自己的选择器,它不是CLLocationManagerDelegate协议的一部分.

另一种方法是在开始监视区域时测试区域内的位置.这个解决方案听起来不是那么简单:你需要先通过调用startUpdatingLocation更新当前位置,因为只读取locationManager的location属性可能会给你陈旧或极不准确的读数.

总结

以上是内存溢出为你收集整理的iOS Geofence,监控启动时如何处理内部区域?全部内容,希望文章能够帮你解决iOS Geofence,监控启动时如何处理内部区域?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1103586.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-28
下一篇2022-05-28

发表评论

登录后才能评论

评论列表(0条)

    保存