Location services don't stop when application

2019-02-08 18:59发布

I'm currently developing an iPhone application which needs location services for various use including AR.

I test everything on simulator and on my iPhone 3GS and everything went well.

I recently tested on iPhone4 and on iPad2 and the location service (the little icon in status bar) keeps displaying even when I manually kill the app! The only way to disable this icon is to manually stop the location service for my app in the settings.

Does anyone know something about this? If needed I can post my code.

Thank you in advance

Edit :

When I kill the application, go to location services, switch off my app the location icon disappears. But when I switch it back on, it reappears! Is that normal?

7条回答
男人必须洒脱
2楼-- · 2019-02-08 19:19

Presumably this is so users don't need to stare at the bar to notice some mischievous app is using location services. That icon appears when you use any location services and remains for some indeterminate time afterwards.

This is intentional behavior Apple wants users to know which apps are using their locations. It seems this is sensitive data, wouldn't you agree?

查看更多
冷血范
3楼-- · 2019-02-08 19:19

This is the solution which fixed this problem for me.

Just stop monitering location changes in

- (void) applicationDidEnterBackground: (UIApplication *)application
{
    [locationManager stopMonitoringSignificantLocationChanges];
    locationManager.delegate = nil;
}

not in applicationWillEnterForeground: .Still,it takes a few seconds to disappear locating icon.

I don't know why it isn't working in the latter method.

查看更多
虎瘦雄心在
4楼-- · 2019-02-08 19:26

I've found the answer! It came from region monitoring, which I enabled before, but removed all code using it weeks ago.

As I had already tested on the iPad, and even if I deleted and re-installed the app, the system seems to have kept information on region I monitored.

Thus, as described by the documentation, the iOS kept on locating for my App, just as startMonitoringSignificantLocationChanges.

Thanks for you answers, it gave me a better understanding of the location system and how to efficiently use it (in particular thanks to progrmr and Bill Brasky)

查看更多
Anthone
5楼-- · 2019-02-08 19:26

I have been battling with this problem for a while, and I think I've finally gotten to the bottom of it..

The reason that the location service doesn't stop when you ask it to is not because you haven't stopped it or released it properly. It's actually caused by releasing and re-allocating the CLLocationManager itself, in my experience.

If you have code which releases your CLLocationManager in applicationDidEnterBackground, and then you allocate a brand new one in applicationDidEnterForeground, etc., then you'll probably have this problem.

The solution is this:

  1. Only create your CLLocationManager object once, in applicationDidFinishLaunching.
  2. To start, call startUpdatingLocation, startMonitoringSignificantLocationChanges etc. as normal.
  3. To stop updates, call the appropriate stopUpdatingLocation, stopMonitoringSignificantLocationChanges etc. as normal.
  4. Never, ever release your CLLocationManager or set its' reference to nil (except possibly in applicationWillTerminate, but that probably won't make any difference).

This way, I went from having my app continue to use location services for up to 12 hours after putting my app away in the background, to the location services arrow disappearing within 10 seconds of backgrounding with this new approach.

Note: Tested on iPhone 4S running iOS 5.1.1. To get accurate results on your app's performance in this regard, make sure you go into Settings->Location Services->System Services and turn off the Status Bar Icon switch. That way, the status bar arrow will accurately reflect usage by apps alone.

查看更多
Animai°情兽
6楼-- · 2019-02-08 19:28

I've run into that problem a while ago and found it useful to apply only one method of applicationDelegate object

- (void)applicationWillEnterForeground:(UIApplication *)application;

If you'll stop your CLLocationManager from receiving updates inside that call, you'll be alright. Of course you'll need to start updating somewhere else, and - (void)applicationDidBecomeActive:(UIApplication *)application; will be a good choice. Also you need to note, that there are two methods of location awareness

  • the gps based -(void)start/stop_UpdatingLocation;

  • and the 3g/wi-fi based -(void)start/stop_MonitoringSignificantLocationChanges;

查看更多
狗以群分
7楼-- · 2019-02-08 19:35

I ran into this same exact issue when using the region monitoring tools. It didn't matter what I did to disable the regions, the arrow remained. I did finally solve the issue by cleaning up the calls to locationManager. If you are closing your view and don't need the location manager, set it to nil and/or release it. If you are monitoring location in the background, it will stay up there, but if not, make sure you are cleaning up all your location monitoring.

It seems like it is a bug, but as I found out, it is not. Just requires a bit more cleanup.

查看更多
登录 后发表回答