I am trying to write a method to return the current location. While I’m waiting a period of 30sec, 1 min, or 2 minutes (set in settings) I display an alert view. I allow the user to bypass the timer wait with an alertView button accepting the current accuracy which I will display and update in the alert view. My code has a couple of holes in it I need help with. Essentially, it is to know how to have the getCurrentLocation method wait on the timer. I can’t just use a delay because I plan to force the timer to expire if the user hits the button or if the location accuracy is met (this is checked in timer). Here is the code:
- (void)timerFireMethod {
static int elapsedTime = 0;
elapsedTime++;
if (elapsedTime >= gpsTimeout) {
[locationManager stopUpdatingLocation];
elapsedTime = 0; // reset static variable
// !!! How do I do the following !!!
// do something to allow getCurrentLocation to return
}
if ((currentLocation.horizontalAccuracy <= gpsDesiredAccuracy) & (currentLocation.verticalAccuracy <= 2*gpsDesiredAccuracy)) {
[locationManager stopUpdatingLocation];
elapsedTime = 0; // reset static variable
// do something to allow getCurrentLocation to return
}
}
- (CLLocation *)getCurrentLocation {
// check if current accuracy is good enough and return if true
if ((currentLocation.horizontalAccuracy <= gpsDesiredAccuracy) & (currentLocation.verticalAccuracy <= 2*gpsDesiredAccuracy)) {
[locationManager stopUpdatingLocation];
return currentLocation;
} else {
// show alert with count down timer
UIAlertView *gpsAlertView = [[UIAlertView alloc] initWithTitle:nil message:@"tbd put in timer and list location accuracy updates" delegate:self cancelButtonTitle:@"Continue With Current Accuracy" otherButtonTitles:nil];
[gpsAlertView show];
// start timer
NSTimer *gpsTimer = [NSTimer scheduledTimerWithTimeInterval:1.0
target:self selector:@selector(timerFireMethod:)
userInfo:nil repeats:YES];
// !!! How do I do the following !!!
// wait for when timer expires,
// the user to press "Continue With Current Accuracy" button (can force timer to expire?)
return currentLocation;
}
}
The way to wait on any timer or for any accelerometer or user event is to simply exit the current method back to the UI run loop by using a return statement.
Anything you want done after the wait can go in the timer or event callback method.