2
votes

Have a simple app that gets gps location. I would like to get a gps location before the app initialises. It seems for me that GPS location is updated asynchronously and I do get the app screen on the background loading while the question pops up: "allow appliction to use your current location?"

Well, how can I make a callback (sorry, coming from Ruby / JS background) to wait until I get the current location.

The code:

- (void)viewWillAppear:(BOOL)animated
{
    // Set the main view to utilize the entire application frame space of the device.
    // Change this to suit your view's UI footprint needs in your application.
    CLController = [[CoreLocationController alloc] init];
    CLController.delegate = self;
    [CLController.locMgr startUpdatingLocation];

    self.view.frame = [[UIScreen mainScreen] applicationFrame];
    [super viewWillAppear:animated];
}

in CoreLocationController.m

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
    NSLog(@"Update location");
    if([self.delegate conformsToProtocol:@protocol(CoreLocationControllerDelegate)]) {
        [self.delegate locationUpdate:newLocation];
        [self.locMgr stopUpdatingLocation];
    # => wait initialisation until this point and than continue
1
Do you need to do this before the app first loads and before the user allows your app to use their location? And why exactly do you need this (just curious)?Sam Spencer
I need to do it before the screen loads, as I'm using sencha touch, and the app heavily dependant on the map, so the question comes first while the app is loading on the background. If the users waits more than 2 seconds the app crashes :( Does that make sense?Jackie Chan
Hmmm this sounds like a poor design. First of all, why would you force your app to crash, at all, ever? Also keep in mind that iOS will NOT give your app location information if the user has not explicitly allowed your app to use their location. You'll also need to handle the possibility of a user denying location access. However, iOS does allow you to get the user's location even if your app is not in the foreground (if the user allows).Sam Spencer

1 Answers

3
votes

You really just need to get your app up and running.

There are two things I would try (simultaneously) to handle the delay:

  • misdirection! Start with another screen open. Get the user interested in something else. One tap's worth of delay might be all you need to get a fix particularly if this is not a first run. On the first run when you know it will take a little time, run a little tutorial that will occupy the time.

  • start with the last location - this may not be right for you if inaccurate position information could be harmful but you could store the last location received when the app exits and use it as your current position when you start the app again. Of course you want to indicate that the location is a guess at best so you might want to draw your own position dot with uncertainty circle, indicating that the whole map is the uncertain area - until you get your first GPS fix and then you can begin drawing real position updates.

I think an artificial delay where you do nothing while waiting for the location to update is going to be a worse user experience than accepting that it will take a short time and trying to do as much as you can in that time.