Articles

Simulating Locations with Xcode

By Brandon Alexander

We recently shipped an application that required Location Services for an event that was happening in Atlanta. Since our team is distributed across the country, it simply wasn't feasible to send everyone to Atlanta for a couple weeks to test things out. I happen to live in the Atlanta area and getting down to the Georgia Dome is a day trip for me. I really wanted to find a way to limit my time in downtown Atlanta.

This is where things get exciting. Apple's engineers have given us the ability to simulate locations within our applications. This simulation works for both the device and the simulator.

Before we get started, we need to go over one important thing. When you simulate a location on a device, Location Services has been hijacked until you deploy your app with location simulation turned off. Be sure to keep that in mind before you start testing on a personal device (you know you do it...) and wreaking havoc on all of your location based apps.

GPX Files

Xcode uses a standardized file type to simulate locations. The GPX spec defines many different ways to define a path or a set of geolocation coordinates. Xcode only uses the <wpt> tag so if you find a tool that generates <rte> or <trk> based gpx files, Xcode won't be able simulate your location properly.

You can find more information about GPX files here: http://en.wikipedia.org/wiki/GPSeXchangeFormat

Generating GPX Files

To create valid GPX files Xcode can use, I have found two great resources.

If I need to have one location simulated, I'll head over to http://gpx-poi.com and create a single waypoint file. Click download and save it in a convenient location on your filesystem.

gpx-poi.png

Things get a little more complicated if I want to simulate a series of waypoints. To accomplish this, I found this site: http://www.elsewhere.org/journal/gmaptogpx/

If you follow the instructions for creating the bookmarklet and go to Google Maps, pull up some driving directions, and activate the bookmarklet, you'll see a screen similar to this one:

gmaptogpx.png

Selecting the "Points" option will generate the proper type of GPX file. Once the file is generated, copy the text and paste it into a new document and name it something meaningful with the gpx extension.

We're now ready to simulate our location.

Now to Xcode

For the following we're going to use the project found on GitHub: https://github.com/whilethis/location-simulation. You'll need Xcode 4.2 or newer to use this feature. I was using Xcode 4.6.2 just in case your screens look slightly different.

Using Xcode, we can simulate our locations two ways.

The first way is to have the location simulated from application launch. To do this, we need to open up the Scheme Editor (Product->Scheme->Edit Scheme or Command+Shift+,) and make sure the "Allow Location Simulation" checkmark is checked. To import the freshly created gpx file, open the dropdown control and select "Add GPX File to Project...". Navigate to the file and select it and have Xcode copy the file into the destination group's folder.

scheme.png

You might have noticed a large list of cities you can also select to simulate. These are the same as a single waypoint GPX file.

The second way to simulate a location is through the debugger. To do this, you need to have location simulation enabled through the scheme editor and any location (or no location) chosen in the dropdown control. Set a breakpoint in the project (for the sample project, somewhere in application:didFinishLaunchingWithOptions: will suffice). Run the application and when the breakpoint is triggered you'll see the Location Services icon in the jump bar of the debug area.

debugger.png

Select your location and then run the application and Location Services will now run through the simulated locations in the GPX file.

Nick Arnott, QA lead at Double Encore, recently wrote a similar article on the topic. While the tools I regularly use do the job, I'd love to have some better tools for generating GPX files based on actual routes.

"Hey, I have this idea for an app..."

Brandon Alexander

Brandon Alexander

Brandon is an Associate Staff Developer at Black Pixel. He tweets at @balexander.