I am using the Core Location framework to locate the device and once locationManager:didUpdateToLocation:fromLocation: method is called with a location, I stop tracking the user.
However the first time I launch the app (from a fresh install). When I message startUpdatingLocation of my Location Manager, the user gets the alert to accept or refuse location service.
When I accept the tracking doesn't begin, it's only when I go away and come back to this view controller when startUpdatingLocation is again called that notifications start coming in.
I am implementing locationManager:didChangeAuthorizationStatus: thinking that this would get messaged when the user accepts (or refuses) location services, but it doesn't.
Can anyone point me in the right direction for updating location as soon as the location services message has been dismissed ?
Thanks.
UPDATE WITH CODE SAMPLE
I've got a singleton class which encapsulates my logic, the idea is when the user location is requested, a check on the CLLocation's timestamp is performed and if it's too old, start tracking is messaged, which lazy loads my CLLocationManager iVar,
-(void)startTracking{
if(!self.locationManager)
_locationManager = [[CLLocationManager alloc] init];
if([CLLocationManager authorizationStatus] == kCLAuthorizationStatusDenied){
[self invalidateUserLocation];
}else{
self.locationManager.delegate = self;
[self.locationManager startUpdatingLocation];
}
}
New location received:
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
_userLocation = [newLocation retain];
NSDictionary * info = [NSDictionary dictionaryWithObject:self.userLocation
forKey:@"userLocation"];
[[NSNotificationCenter defaultCenter] postNotificationName:kUserLocationFound
object:self
userInfo:info];
[self stopTracking];
}
Stop tracking:
-(void)stopTracking{
if(!self.locationManager)
return;
[self.locationManager stopUpdatingLocation];
self.locationManager.delegate = nil;
}
When I have a view controller which needs the users location I call userLocation on my singleton object like so. If it's recent, I return the CLLocation, otherwise I return nil and start again. Notice I stop tracking when I receive the first update. But the first time this runs and I get the alert view, nothing is tracked at all.
- (CLLocation*)userLocation
{
if(_userLocation.coordinate.latitude == 0 && _userLocation.coordinate.longitude == 0){
[self startTracking];
return nil;
}else{
NSDate* timeNow = [NSDate date];
NSTimeInterval interval = [timeNow timeIntervalSinceDate:_userLocation.timestamp];
if(interval >10)
[self startTracking];
return _userLocation;
}
}
Did you try calling – locationManager:didChangeAuthorizationStatus:
from CLLocationManagerDelegate
?
I'm going to guess that you call startTracking
when the view controller loads. This is circumvented by the alert which ask for if it's okay. At that point, the start locating message won't be called again so by calling didChangeAuthorizationStatus
, you can call your startTracking
method.
Something like:
-(void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status {
if (status == kCLAuthorizationStatusDenied) {
//location denied, handle accordingly
}
else if (status == kCLAuthorizationStatusAuthorized) {
//hooray! begin startTracking
}
}
If that's not the case, let me know.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With