Location services don't stop when application is terminated

前端 未结 7 1312
感动是毒
感动是毒 2021-01-04 18:28

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 e

相关标签:
7条回答
  • 2021-01-04 18:45

    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.

    0 讨论(0)
  • 2021-01-04 18:48

    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)

    0 讨论(0)
  • 2021-01-04 18:50

    Sounds like you're app is going into the background and still using CLLocation. You can stop CLLOcationManager when you receive notification that you're app is resigning active, that's the best way. Then resume when it becomes active. The answer in this question show how to do that here

    [EDIT] When your app goes into the background or resigns active for any reason (ie: phone call) you should stop location services at that time. You need to subscribe to the notifications and provide a method to stop and start location services, something like this:

    -(void)appDidBecomeActiveNotif:(NSNotification*)notif
    {
        [locationManager startUpdatingLocation];
    }
    
    -(void)appWillResignActiveNotif:(NSNotification*)notif
    {
        [locationManager stopUpdatingLocation];
    }
    
    -(void)viewDidLoad
    {
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appDidBecomeActiveNotif:) name:UIApplicationDidBecomeActiveNotification object:nil];         
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appWillResignActiveNotif:) name:UIApplicationWillResignActiveNotification object:nil]; 
    }
    
    -(void)dealloc
    {
        [[NSNotificationCenter defaultCenter] removeObserver:self];
        [super dealloc];
    }
    
    0 讨论(0)
  • 2021-01-04 18:52

    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;

    0 讨论(0)
  • 2021-01-04 19:00

    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.

    0 讨论(0)
  • 2021-01-04 19:05

    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.

    0 讨论(0)
提交回复
热议问题