Testing CoreLocation on iPhone Simulator

2019-01-04 09:14发布

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.

8条回答
等我变得足够好
2楼-- · 2019-01-04 09:49

I think there's another (better IMHO) approach here than subclassing CLLocationManager like in

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

In ObjectiveC it seems to be possible to replace an existing method from a class without overriding it. This is often called "method swizzling" : you define your own category for an existing class an implement an existing method in it.

From the client perspective, everything is transparent : he has the feeling he's dealing with the real CLLocationManager but actually, you "took the control from it". So that he doesn't need to deal with any special subclass or any special delegate protocol : he keeps on using the same class / protocol as the one from CoreLocation.

Here's an example to took the control over the delegate a client would inject :

 
@implementation CLLocationManager (simulator) 

-(void) setDelegate:(id)delegate { //your own implementation of the setDelegate... } -(id)delegate { //your own implementation of the delegate.... } -(void)startUpdatingLocation { } -(void)stopUpdatingLocation { } //.... //same for the rest of any method available in the standard CLLocationManager @end

Then in this implementation, you're free to deal with a pre defined set of coordinates (coming from a file of whatever) that will be "pushed" to the delegate using the standard CLLocationManagerDelegate protocol.

查看更多
祖国的老花朵
3楼-- · 2019-01-04 09:51

Testing CoreLocation on iPhone Simulator

1) To test the location in simulator,best way is to use GPX files,just go to Files -> New -> Resource -> GPX File.

2) After Adding the GPX file update the location coordinates as desired.

3) once the GPX file is added to the project,Select the Scheme -> Edit Scheme -> Run -> Allow Location Simulation.tick the location simulation and select the name of the GPX file you just created.

this way simulator will always pick your desired coordinates,that we have added in our GPX File.

Create the GPX file

查看更多
登录 后发表回答