Taranfx
Taranfx

Reputation: 10447

iPhone CLLocation Simulator didUpdateToLocation never called

I know iPhone simulator is defaulted to a location in US, but does that mean that didUpdateToLocation will never be called? All that gets called is didFailWithError, that too after a long time. What is the timeout for this method to be called?

p.s. Android is much better at this, lets you fake location.

Upvotes: 4

Views: 3929

Answers (5)

nenchev
nenchev

Reputation: 2077

I found a weird bug with the didUpdateLocations not being called on the simulator. If you're using a custom location (like I am), after starting a fresh instance of the simulator, iOS fails to return back the custom location until you do:

Select "Apple" as your location: Debug -> Location -> Apple

Then switch back to your custom location: Debug -> Location -> Custom Location

Upvotes: 1

Vincent
Vincent

Reputation: 4409

You can get didUpdateToLocation updates also in the simulator.

  • However you have to include the code on a thread with an active run loop, such as your application’s main thread. This has the advantage that the GPS has time to update while the user is clicking around in your APP. :)
  • Configure the simulator (tested on 5.1) You can configure the simulator (Debug > Location), e.g. with your own location. Visit GoogleMaps for that. Tip: close simulator to get the last changes applied!

I have added this in the viewDidLoad of main loop:

    self.locationManager = [[CLLocationManager alloc] init];
    locationManager.delegate = self;
    locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    [locationManager startUpdatingLocation];

And the catch of the result:

- (void)locationManager:(CLLocationManager *)manager
    didUpdateToLocation:(CLLocation *)newLocation
           fromLocation:(CLLocation *)oldLocation {
       // Display location in Log
       NSLog(@"Lat= %s Lon=%f", newLocation.coordinate.latitude,
             newLocation.coordinate.longitude);
       // Stop updating, when one is enough
       [locationManager stopUpdatingLocation];
}

Hope this helps.

Upvotes: 3

karlbecker_com
karlbecker_com

Reputation: 959

iOS 5 now has location as a modifiable parameter in the simulator. In the Simulator, select the menu Debug -> Location, and choose one of the static or dynamic location routes.

The Xcode debugger also allows you to change the location - look for the little location arrow beside the play/pause/step buttons to change the location from right within Xcode.

Upvotes: 1

Sudhanshu
Sudhanshu

Reputation: 3960

Yes in case if you are using iPhone simulator then this didUpdateToLocation will never called because location updates are not possible in case of simulator,i was facing the same situation in one of my application(simulator testing become difficult),so i customized the longitude and latitude in case of simulator in the didFailWithError method itself.Like below

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError*)error{
if( [(NSString *)[[UIDevice currentDevice] model] isEqualToString:@"iPhone Simulator"] ){

        lat = 50.748129f;
        log = -2.970836f;

        return;
    }
[[LocationManager sharedManager].locationManager stopUpdatingLocation];
    [LocationManager sharedManager].isUpdating = NO;
}

Now you can test your app with this location likewise you do in case of device when your current location is searched.

Hope you got what i want to say

Good Luck!

Upvotes: 6

Bogatyr
Bogatyr

Reputation: 19333

You don't get location updates on the iphone simulator.

Upvotes: 1

Related Questions