开发者

GPS won't stop updating

I'm writing an iPhone application that uses GPS data. My problem is that the GPS never stops updating the locations (the arrow stays on the status bar) even if I kill the actual process. The only way to make it disappear is uninstalling the app, or disallowing it to use location in system settings.

This is how I create the location manager:

    // Create the location manager if this object does not
    // already have one.
    if (nil == locationManager)
        locationManager = [[CLLocationManager alloc] init];

    locationManager.delegate = regionTracker;

    locationManager.desiredAccuracy = kCLLocationAccuracyBest;

    // Set a movement threshold for new events.
    locationManager.distanceFilter = 1;

    [locationManager startUpdatingLocation];

In my app delegate, I run these two rows both in applicationWillTerminate: and applicationDidEnterBackground:

   [self.viewController.locationManager stopUpdatingLocation];
   [self.viewController.locationManager setDelegate:nil];

I nil the delegate because I saw some example where they开发者_如何转开发 did that, but should it really be needed?


Same as milonet mentioned, in my case I needed to call stopMonitoringSignificantLocationChanges in addition to the stopUpdatingLocation.


I suspect something funny is going on with the retain/release lifecycle of your CLLocationManager object.

In the delegate, you're treating it like a property. In the viewController you're treating it like an iVar.

I'd rather you assign it to self.locationManager rather than locationManager. Assuming that's a named property with the retain setter semantic, and then you release it in your viewController's -dealloc method (possibly after calling stopUpdatingLocation on it), you should be good to go.

I've seen this same behavior myself, though, the first time I played around with CoreLocation right after 4.0 and backgrounding came along. I thought there was something wrong with my battery, because I'd just gotten an iPhone 4. Turns out I was just churning CoreLocation whether my app was running or not.


I found out the cause of this. In an earlier stage of the project I had used the region function of iOS 4, which registered global regions of the system associated with the application identifier.

Solution - make a new project.


i have the same problem, the location manager stay on always! i have try to dealloc all but the gps stay on.. the only solution for turn off the gps is delete the app!


Something similar happened to me. I was using a mapView with userlocation activated.I deallocated locationmanager as usual and the GPS still was active. I disabled that option and the gps stopped when it was supposed to.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜