GPS location/Region monitoring recap + how to switch on/off the gps signal (icon) in background [iOS]

限于喜欢 提交于 2019-12-13 05:22:47

问题


I'l like to know

A) if it's possible to completely switch off the use of gps when the App is in background mode and I terminate it (long press on "home button", tap on the "X" of the corresponding task), even if "region monitoring" is active and there is the corresponding active flag in the App .plst .

Also I'd like to recap how you can monitor the device location; I think there are three ways: - [locationManager startUpdatingLocation] - [locationManager startMonitoringSignificantLocationChanges]; - [locationManager startMonitoringForRegion:region desiredAccuracy:accuracy];

B) i don't understand if they "can"/"may not"/"have to" run at the same time. for example: startUpdatingLocation and startMonitoringSignificantLocationChanges are mutually exclusive and that's ok, but what about startMonitoringForRegion?

C) Can i use only startMonitoringForRegion without startMonitoringSignificantLocationChanges?

D) if i use both at the same time, may i receive two notifications while entering/exiting a monitored region? How can I avoid it? I this case, think i should implement something like this:

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation   {
 BOOL crossedBoundary = [self checkIfNewLocationHasCrossedMyMonitoredRegionBoundary:newLocation ];
if (crossedBoundary) NSLog(@"Crossed the boundary");
}

AND

- (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region  {  NSLog(@"Crossed the boundary"); }

E) How can i turn off/on startMonitoringForRegion while the App is in background?

Thanks very much. I'm starting from "Regions" and "breadCrumb" Apple example https://developer.apple.com/library/ios/#samplecode/Breadcrumb/Introduction/Intro.html#//apple_ref/doc/uid/DTS40010048-Intro-DontLinkElementID_2

https://developer.apple.com/library/ios/#samplecode/Regions/Introduction/Intro.html


回答1:


Answering your questions in order

Ans A) You can't switch off location services as far as I have tried.

Ans B) startMonitoringForRegion is running a service outside of the app monitoring the region entry/exit status. It's location is not updated from the CLLocationManager that is updating the current location from the location services. It just stores the monitored regions with respect to the application.

Ans C) Yes you can use them separately because startMonitoringForRegion adds regions to monitor on the location services where as startMonitoringSignificantLocationChanges is the monitoring technique by which the larger changes in location are handled and updated to the CLLocationManager using locationManager:didUpdateLocations:. But as mentioned in the previous answer startMonitoringForRegion will add region for monitoring to the region monitoring location service

Ans D) Yes you will receive separate updates to location. But keep in mind startMonitoringSignificantLocationChanges updates the location to the CLLocationManger. Where as the startMonitoringForRegion updates only on boundary crossings and doesn't update location.

Ans E) You have to add stopMonitoringRegion to the appEnterBackground in the application's AppDelegate




回答2:


It will stop Calling GPS

-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation*) newLocation
           fromLocation:(CLLocation *)oldLocation
{
[manager stopUpdatingLocation];

}


来源:https://stackoverflow.com/questions/9307538/gps-location-region-monitoring-recap-how-to-switch-on-off-the-gps-signal-icon

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!