views:

5078

answers:

10

Is there anyway to test CoreLocation on the iPhone Simulator?

All I require is to be able to set the location myself and have CoreLocation return it.

A: 

No. CoreLocation on the iPhone simulator is fixed at 37.3317 North, 122.0307 West (Apple HQ). If you need to test CoreLocation, you have to do it on the iPhone device itself.

Bob Aman
+2  A: 

Trigger the Core Location callbacks from a test class, if you need to set a location other than the one the simulator gives you.

Kendall Helmstetter Gelner
+8  A: 

Use a filtering function to swap in a test instance when running on the simulator. Wherever you previously received the location (delegate call, etc), pass it through this:

+ (CLLocation *) wakkawakka: (CLLocation*) loc {
#ifdef TARGET_IPHONE_SIMULATOR
    /* replace with a test instance */
    return [[CLLocation alloc] initWithLatitude:10.0 longitude:20.0];
#else
    return loc;
#endif
}

Memory management issues aside...

Nick Veys
In many situations this is by far the easiest solution!
Emil
Just out of curiosity, how did you know wakkawakka back in 2009 april?
Raj
I'm not sure what you mean, is wakkawakka significant? I just used a nonsense phrase. :)
Nick Veys
+11  A: 

Thanks for the great feedback, it has prompted me to find a robust solution.

All the code can be found here:

http://code.google.com/p/dlocation/

It is very messy but as I use it it will be become much better.

The solution was to subclass CLLocationManager and define a new delegate @protocol, called DLocationManagerDelegate.

It is designed to be a simple drop-in replacement for CLLocationManagerDelegate that compiles down to a very thin layer when deployed on an actual device.

When running on the device it will return data as normal using CoreLocation, but in the simulator it will read latitude and longitude from a text file (defined in the DLocationManager.h file).

I hope this helps, the implementation is on the simple side and you have to startUpdatingLocation and stopUpdatingLocation to update the display.

Comments and feedback will be gratefully received.

rjstelling
The link provided doesn't work for me. I ended up writing my own simulator by subclassing CLLocationManager. I put a [copy on github here](http://github.com/progrmr/CLLocationManager-simulator).
progrmr
+7  A: 
yonel
A: 

Hi yonel!can you explain with more detail your idea?please. I'm a beginner and don't understand very well your idea.

jaime
+2  A: 

Here is my simple hack that forces the CLLocationMager to return the geocoords of Powell's Tech Bookstore only on the simulator:

#ifdef TARGET_IPHONE_SIMULATOR 

@interface CLLocationManager (Simulator)
@end

@implementation CLLocationManager (Simulator)

-(void)startUpdatingLocation {
    CLLocation *powellsTech = [[[CLLocation alloc] initWithLatitude:45.523450 longitude:-122.678897] autorelease];
    [self.delegate locationManager:self
               didUpdateToLocation:powellsTech
                      fromLocation:powellsTech];    
}

@end

#endif // TARGET_IPHONE_SIMULATOR
wcochran
A: 

So does that mean didUpdateToLocation will get called normally... even on the simulator? (Just with the 1 location always being returned.)

I can't seem to EVER get didUpdateToLocation to be called on the simulator. (Same code works 100% fine on the device, though.)

Patricia
A: 

the locationManager:didUpdateToLocation and locationManager:didFailedWithError overloaded callbacks are never called in the iphone simulator, that's kinda strange, all i get is 0.0000 for lat., and 0.0000 for lon. as the position. In the situation you develop something, that's kinda hard to implement all the possible situations that can occur during the location handling, using only simulator environment.

A: 

If you're interested in updating the blue userLocation dot in a MKMapView with the simulated location updates, check out my FTLocationSimulator at http://github.com/futuretap/FTLocationSimulator

It reads a KML file generated by Google Earth to provide continuous location updates.

Ortwin Gentz