Sunday, March 28, 2010

Mocking Core Location

One of my current project is a location aware iPhone App. It requires the device to "unlock" a location when the user/device is close enough (a couple of hundred meters).

One of the problems with iPhone development is that there is no way to simulate moving around with the device, or to feed it pre made coordinate data.

Anyone that has worked with the iPhone SDK and Core Location knows that CLLocationManager is the primary class that you need to interact with in order to get location updates.

What i wanted to do was to create a Mock Core Location Manager, that would allow me to create a text file full of coordinates, set a update delay, and then call my Core Location delegate methods, so i can manipulate my app as i please.

If your like me you would think that this should be as simple as creating a CLLocationManger category, and overloading some methods, namely startUpdatingLocation/Heading & stopUpdatingLocation/Heading. 

Now this works for most applications, however it doesn't work with the Iphone's native MapKit's MapView. For that we need to use a different callback then the publicly documented method.

There is a little app called Class Dump that generates header files from compiled binaries. Now thanks to the big drive by hackers to develop for jail broken iphones, you can find copies of the headers without having to generate them yourself.

MKMapView uses a class called MKLocationManager as it's CoreLocation Delegate. You notice that it adheres to the CLLocationMangerDelegate as you would suspect, but along with the public callback method 

locationManager:didUpdateToLocation:fromLocation: 

is also has a very similar method called

locationManager:didUpdateToLocation:fromLocation:usingSupportInfo: 

This is the winner. MKLocationManger will not respond to the former, but will respond to the later. I don't yet know what the usingSupportInfo parameter does. But my assumption is that it is a NSDictionary, however in the example i've posted i simply pass it nil.

So time for some code ...



This is fresh out of the oven, i plan to add a timer and exec the sendUpdate method after a set delay and read the locations from a text file. I'll post it up on git hub if i ever get it done


Update 29/03/10
Since writing this post i've put up a working version of the code on GitHub, Fork away

2 comments:

Anonymous said...

Great. This is what I am looking for.

Anonymous said...

Thanks! Very helpful