CLLocationManager not working all the time (iOS 8, Xcode 6)
Asked Answered
T

2

8

Basically half the time the delegate method

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations;

is not called at all. But the other half of the time it works perfectly! I've found that it usually happens when I first start up Xcode after closing and quitting it, but then after that or a couple runs after that it seems to work fine. I'm not 100% sure if it's just an Xcode problem or what, I'll be getting a developer's license soon so I'll see if it will work fine on an actual device.

Starting from the viewDidAppear (tried in viewDidLoad also, made no difference), I run a method to init my locationManager stuff:

locationManager = [[CLLocationManager alloc]init];

[locationManager setDelegate:self];
locationManager.distanceFilter = 20.0f;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
locationManager.pausesLocationUpdatesAutomatically = NO;

if ([locationManager respondsToSelector:@selector(requestAlwaysAuthorization)])
    [locationManager requestAlwaysAuthorization];

[locationManager startUpdatingLocation];

Sometimes this works, sometimes it doesn't. I even made a timer to re-run this every so-and-so seconds, and this doesn't work.

Is there something else I should do? Is there any answer to this problem?

Thanks.

Theta answered 28/9, 2014 at 4:35 Comment(8)
Location manager is really just meant to work on a device. In the simulator, have you tried setting the location?Wondawonder
What do you mean by half the time? didUpdateLocations is called when the OS has location updates to deliver. Sometimes it does, other times (less often) it doesn't.Murk
Few things, one do you have NSLocationAlwaysUsageDescriptionadded to your apps info.plist file? Two have you set a location in the simulator? (Debug->Location)Roanna
Yes, I have. Like I said, it works flawlessly around half the time. The location is set, the the thing is added to the plist, and everything. I've found that when I first start up Xcode and run the app, it doesn't run the delegate method at all, even if I change the location etc. Other times, it works as it should and runs right when the app is started and when the location is changed. I realize there isn't much to do but I was wondering if maybe this was something that was already known or something. Thanks thoughTheta
I have the exact same issue. For me it's working with iPhone 6 simulator all the time but iPhone 5 is not working half time. I'm investigating tooSteamheated
Kind of glad to see I'm not the only one. I just purchased the developer license today, so I'll test it out tomorrow and see if anything is different and let you know. And if you find out what it is please post it here! hahaTheta
I too have the same issue. I have to restart the device once I Disconnect it from xcode.Calcium
I forgot to update this, but my problem kind of solved itself out when I put it on my device. Not sure what the dealio is with you guys, I wish I could help more.Theta
D
1
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations;  

This delegate method is invoked only if new locations are available. Sometimes gps will not get satellite signal hence locations cannot be obtained. So in these situations the above mentioned method will not get triggered. Since you are testing in simulator,you should change or set the location. I think it will work fine on an actual device.

Dendro answered 4/3, 2015 at 9:47 Comment(0)
A
0

add in viewdidappear

_locamangr = [CLLocationManager new];
    _locamangr.delegate = self;
    //    _locamangr.distanceFilter = kCLDistanceFilterNone;
    _locamangr.desiredAccuracy = kCLLocationAccuracyBest;
    if ([_locamangr respondsToSelector:@selector(requestAlwaysAuthorization)]) {
        [_locamangr requestAlwaysAuthorization] ;
        [_locamangr requestWhenInUseAuthorization];
    }
    [_locamangr startUpdatingLocation];

and set in infoplist. NSLocationWhenInUseUsageDescription NSLocationAlwaysUsageDescription

Anzus answered 29/12, 2014 at 11:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.