简体   繁体   English

iOS:未调用 CLLocationManager 的 didEnterRegion

[英]iOS: CLLocationManager's didEnterRegion not called

I have successfully managed to register a region or monitoring, I know this because the didStartMonitoringForRegion method was fired.我已经成功注册了一个区域或监控,我知道这是因为didStartMonitoringForRegion方法被触发了。

I have a simple test app with one MKMapView where I can see my current location, I also have my region drawn on the map as a red circle so I can actually 'physically' see at what point I am entering or exiting this region.我有一个带有MKMapView的简单测试应用程序,我可以在其中查看我的当前位置,我还在地图上将我的区域绘制为一个红色圆圈,这样我实际上可以“物理地”看到我在什么时候进入或离开这个区域。 In the didEnterRegion and didExitRegion methods I have a UIAlertView that fires.didEnterRegiondidExitRegion方法中,我有一个触发的UIAlertView

A little while ago I decided to leave the office and take a walk with my iPhone 4, to see if this would work.不久前,我决定离开办公室,带着我的 iPhone 4 散步,看看这是否可行。 I started in the middle of the region (which is about 200m in radius), my app was in the foreground the whole time.我从该区域的中间(半径约 200m)开始,我的应用程序一直处于前台。 I walked a couple blocks and exited the region, nothing happened.我走了几个街区,离开了该地区,什么也没发生。

I read on SO (I think) that sometimes it only works when you exit the region by a certain distance, so I kept on walking.我在 SO(我认为)上读到,有时它仅在您离开该地区一定距离时才有效,因此我继续步行。

After another 200m or so I gave up as the UIAlertView still wasn't showing, and started to walk back.又过了 200m 左右,我放弃了,因为UIAlertView仍然没有显示,然后开始往回走。 Obviously, upon re-entering the region, didEnterRegion wasn't firing either.显然,在重新进入该区域时, didEnterRegion也没有触发。

I am not calling [locationManager startUpdatingLocation] anywhere in my code, should I be??我没有在我的代码中的任何地方调用[locationManager startUpdatingLocation] ,我应该吗? I'm only really creating the region and calling [locationManager startMonitoringForRegion] .我只是真正创建区域并调用[locationManager startMonitoringForRegion]

The CLLocationManagerDelegate is my app delegate, and it seems fine as my didStartMonitoringForRegion is firing properly. CLLocationManagerDelegate是我的应用程序委托,看起来不错,因为我的didStartMonitoringForRegion正在正确触发。

Am I missing anything else?我还缺什么吗?

The documentation states that region monitoring works independently from the other location services.该文档指出,区域监控独立于其他位置服务工作。 That means it should be enough to call startMonitoringForRegion.这意味着调用 startMonitoringForRegion 就足够了。

Make sure:确保:

  • [CLLocationManager regionMonitoringAvailable] returns YES [CLLocationManager regionMonitoringAvailable] 返回 YES
  • CLLocationManager.monitoredRegions contains valid regions CLLocationManager.monitoredRegions 包含有效区域

NOTE: The doc specifies that events make take between 3-5 minutes to fire.注意:该文档指定事件需要 3-5 分钟才能触发。

In iOS 6, regions with a radius between 1 and 400 meters work better on iPhone 4S or later devices.在 iOS 6 中,半径在 1 到 400 米之间的区域在 iPhone 4S 或更高版本的设备上效果更好。 (In iOS 5, regions with a radius between 1 and 150 meters work better on iPhone 4S and later devices.) On these devices, an app can expect to receive the appropriate region entered or region exited notification within 3 to 5 minutes on average, if not sooner. (在 iOS 5 中,半径在 1 到 150 米之间的区域在 iPhone 4S 和更高版本的设备上效果更好。)在这些设备上,应用程序可以期望在平均 3 到 5 分钟内收到适当的区域进入或区域退出通知,如果不早点。

And

Apps can expect a notification as soon as the device moves 500 meters or more from its previous notification.只要设备从之前的通知移动 500 米或更远,应用程序就会收到通知。 It should not expect notifications more frequently than once every five minutes.它不应期望比每五分钟更频繁地收到通知。 If the device is able to retrieve data from the network, the location manager is much more likely to deliver notifications in a timely manner.如果设备能够从网络检索数据,位置管理器就更有可能及时发送通知。

I hope this helps.我希望这有帮助。

One thing to note down (If you are simulating the location):需要注意的一件事(如果您正在模拟位置):

If entering and exiting is never called even though you think you have done everything right in the code make sure to check if you disabled location simulation by:如果即使您认为代码中的所有操作都正确但从未调用进入和退出,请确保通过以下方式检查是否禁用了位置模拟:

Going to the Product -> Scheme -> edit scheme -> Options tab -> Disable location simulation.转到产品 -> 方案 -> 编辑方案 -> 选项选项卡 -> 禁用位置模拟。

I was trying to test my region monitoring by physically walking out and in the region while simulating location and it was not working for me precisely because of this reason.我试图通过在模拟位置的同时实际走出和进入该区域来测试我的区域监控,正是由于这个原因,它对我不起作用。

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

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