CLLocationManager monitoredRegions (NSSet) is not correct, or maybe something else?

拥有回忆 提交于 2019-12-03 17:40:39
Tommie C.

Region Radius

Please review the discussion on the startMonitoringForRegion method below and note that Apple indicates that a value for the region up to 400 works best on later devices. They also indicate that one can expect responses within 3 to 5 mins on average. I am not sure how you are testing the entry/exit of the region but it may be that you are not allowing sufficient time to pass.

Region Monitoring

Additionally, I suggest you setup break points to examine the monitoredRegions property of the CLLocationManager at strategic points in your code (when you expect the regions to update). You might consider emptying the regions array (see the Namely: header below) before repopulating them with your custom code. I'd inspect that area very carefully to be certain that the expected behavior is occurring.

Filters

There are a number of filters that tell the CLLocationManager when a significant change in position has occurred. You should set these values and inspect to see whether they have an impact on the frequency of the updates. Please also note the other properties and methods that I am showing including the desiredAccuracy and the methods for initiating significant updates. Read the discussion for each of these to see whether they apply in your use case. Try to apply changes where needed to force the device to update when it is appropriate in your case.

Hit Testing User Location

You might want to implement periodic hit-testing of the specific CLCircularRegion using the user location. This should allow you to further refine location updates at predetermined times/locations in your code. For example you might want to refine the user updates using accuracy and distance filters and then in the delegate method call a query against the closest region(s) in your array. The net effect would be to force a region status check whenever the device moves significantly from the last known position.

Background Updates for Location Changes

I recommend you give a close re-read of the discussion related to the startMonitoringSignificantLocationChanges method (You must use this method to resolve the issue). Apple details the steps that you need to get the updates when the app is in the background. Basically you will have to intercept a key in your app delegate method. Fortunately this SO question has sample code. But before you jump to the code, please review the discussion as recommended since it will give you a much clearer idea of how to master this behavior.

Partial Answers

Like many questions. This one requires a number of partial answers to resolve the functionality you need. First make sure you have checked the notes provided above, then try some of this links for partial answers to specific required tasks.

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

Namely:

Useful responses on how to refine geofencing

A simplified tutorial of geofencing

(Did you use requestAlwaysAuthorization() on the location manager?)

Nota Bene

I recommend that you read the discussions of the methods and properties to see hints from Apple. You should also feel free to cmd-click on the methods and properties in code editor and review the comments in the header files. These will provide more insight from Apple that is not available in the documentation or Quick Help.

As a quick example the following header comments seem relevant to your objective. You might want to implement and monitor the delegate method and the requestStateForRegion method mentioned in the comment header:

/*
 *  locationManager:didDetermineState:forRegion:
 *
 *  Discussion:
 *    Invoked when there's a state transition for a monitored region or in response to a request for state via a
 *    a call to requestStateForRegion:.
 */
@available(iOS 7.0, *)
optional public func locationManager(manager: CLLocationManager, didDetermineState state: CLRegionState, forRegion region: CLRegion)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!