简体   繁体   English

如何禁用使用 -startMonitoringForRegion 注册的任何 CLRegion 对象?

[英]How can I disable any CLRegion objects registered with -startMonitoringForRegion?

I am using a NavigationController to display a list of geo-fences available to the user.我正在使用 NavigationController 来显示用户可用的地理围栏列表。 At the top there is a global on/off switch that I would like to use to disable any fences registered with CoreLocation -startMonitoringForRegion.在顶部有一个全局开/关开关,我想用它来禁用使用 CoreLocation -startMonitoringForRegion 注册的任何栅栏。

My fences seem to be registering ok and working for the most part, but no matter how many times I disable the fences individually, I'm still getting the purple location arrow indicating that the system is still monitoring my location and/or fences.我的围栏似乎注册正常并且大部分时间都在工作,但无论我单独禁用围栏多少次,我仍然收到紫色位置箭头,表示系统仍在监控我的位置和/或围栏。

When I disable my fences individually, this is how I'm doing it.当我单独禁用我的栅栏时,我就是这样做的。

CLLocationCoordinate2D coord;
coord.latitude = [[settingsData valueForKey:@"latitude"] doubleValue];
coord.longitude = [[settingsData valueForKey:@"longitude"] doubleValue];
CLLocationDistance radius = [[settingsData valueForKey:@"radius"] intValue];
CLRegion *region = [[CLRegion alloc] initCircularRegionWithCenter:coord radius:radius identifier:[settingsData valueForKey:@"name"]];

// remove this fence from system monitoring
[locationManager stopMonitoringForRegion:region];
[region release];

I've gone through all of Apple's documentation on CoreLocation and use of these methods and I'm at the end of my rope.我已经阅读了所有关于 CoreLocation 的 Apple 文档以及这些方法的使用,但我已经走到了尽头。

I've tried calling [locationManager monitoredRegions];我试过打电话[locationManager monitoredRegions]; but it only returns the active fence and only when I have my detail view loaded up.但它只返回活动围栏,并且只有当我加载了我的详细视图时。 I'm not able to call it any other place in my program and get it to return any of my fences, even though I know they should be active.我无法在程序中的任何其他地方调用它并让它返回我的任何栅栏,即使我知道它们应该处于活动状态。 If anyone has any advice where to go next, I'm all ears.如果有人对接下来的 go 有什么建议,我会全力以赴。

Or, a more simple solution:或者,更简单的解决方案:

for (CLRegion *monitored in [locationManager monitoredRegions])
    [locationManager stopMonitoringForRegion:monitored];

Ok, I think I can answer my own question here finally.好的,我想我终于可以在这里回答我自己的问题了。 Does this mean I can claim my own bounty?这是否意味着我可以索取自己的赏金? :D :D

First off, the lingering location arrow seems to be an iOS bug.首先,挥之不去的位置箭头似乎是一个 iOS 错误。 I have found multiple stories out there with the very same issue.我在同一个问题上发现了多个故事。 So there won't be much I can do about that for now.所以我现在对此无能为力。

As far as removing all of my regions at once, I got this down pat now.至于一次删除我所有的区域,我现在已经成功了。

NSArray *regionArray = [[locationManager monitoredRegions] allObjects]; // the all objects is the key
for (int i = 0; i < [regionArray count]; i++) { // loop through array of regions turning them off
     [locationManager stopMonitoringForRegion:[regionArray objectAtIndex:i]];
}

I was able to display my array and proved they were all in there.我能够展示我的阵列并证明它们都在里面。 Another check after removing shows they are all gone.删除后的另一次检查显示它们都消失了。 Whew.. The issue of the location arrow remains depending on which version of iOS you are running, I can't that I guess.唷.. 位置箭头的问题仍然取决于您正在运行的 iOS 版本,我猜不出来。 If you have users, make sure you inform them the purple arrow is not your fault.如果您有用户,请确保告知他们紫色箭头不是您的错。 For more info on the issue, you can start here.有关该问题的更多信息,您可以从这里开始。 GetSatisfaction Good luck. GetSatisfaction祝你好运。

Yes it is an iOS bug.是的,这是一个 iOS 错误。 Deleting and reinstalling the app does not help.删除并重新安装应用程序没有帮助。 The only way I managed to get rid of the problem is by resetting location warnings我设法摆脱问题的唯一方法是重置位置警告

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM