I’m trying to get a recent location using CLLocationManger. If I cannot get one within 30 seconds, I want an alert to pop up saying that “we’re having problems right now” or something along those lines. In my CLLocationManager Delegate, I do this:
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
NSDate *eventDate = newLocation.timestamp;
NSTimeInterval howRecent = [eventDate timeIntervalSinceNow];
NSTimer *timer;
if (abs(howRecent) < 1.0) { // process time if time is less than 1.0 seconds old.
timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateCount:) userInfo:nil repeats:NO];
return;
}
My updateCount method:
- (void)updateCount:(NSTimer *)aTimer {
count++;
NSLog(@"%i", count);
if (count == 30) {
[aTimer invalidate];
NSLog(@"Timer invalidated");
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Server Down" message:@"Sorry, but we cannot find your location at this time. Please try again later." delegate:self cancelButtonTitle:@"Ok" otherButtonTitles: nil];
[alert show];
[alert release];
}
}
But I don’t see my alert get fired. I tried with a repeating timer, but after it hits 30 seconds, and the alert is shown, it repeats again. So I’m not quite sure what I’m doing wrong here. Thanks!
just a small update to updateCount method :
Don’t use
NSTimer *timer;in locationManager method. instead directly call timer = [[NSTimer….]] method and I think because there is one argument in updateCount method i.e. NSTimer and we are using different NSTimer instance so here is basically problem. Try without using argument in updateCount Method and invalidate the timer that is declared in .h file.