Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Testing CoreLocation on iPhone Simulator

UPDATE: As of iOS 5 and Xcode 4.1 is is now possible to test location in the simulator and even define routes. See http://developer.apple.com for more details.

Legacy Question

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.

like image 970
Richard Stelling Avatar asked Apr 29 '09 12:04

Richard Stelling


People also ask

How do you simulate location on iphone simulator?

How to simulate location in Simulator. To simulate location on a Simulator, select Features menu > Location, then you will see a list of location and movement options you can simulate.

How do you mock locations in simulation?

settings> system > developer optionsFind the option select mock location app Then select the app you downloaded from step 1.

How do you change location on iphone simulator?

in iOS Simulator menu, go to Debug -> Location -> Custom Location. There you can set the latitude and longitude and test the app accordingly.


2 Answers

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 
like image 118
wcochran Avatar answered Oct 11 '22 15:10

wcochran


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.

like image 33
Richard Stelling Avatar answered Oct 11 '22 15:10

Richard Stelling