How To Simulate GPS Location for iOS Testing
I love when I have a chance to learn something new as a tester - a new skill to add to my bag of tricks to be used to break something in a fun new way or to help test something that someone thought couldn't be done. That happened to me when I had to figure out how to pretend to be in various countries around the world as a tester. Sure, I could confirm that I'm in America, and use some dev debug options to force the app to think it was in other places...but that doesn't fully test the code or the app. And what fun is that?
Xcode uses the GPS Exchange Format (GPX) to reference geolocation points to use in simulation. Essentially these are just files with specific latitude and longitude formats.
The formatting is fairly basic. Here's an example of a GPX file for simulating China:
<gpx version="1.1" creator="Guy McCooldude"> <wpt lat="31.230416" lon="31.230416"> <name>China</name> </wpt> </gpx>
This would just be saved as China.gpx (attached for reference). You can create your own just by replacing the lat number with a locations latitude, long with a locations longitude and swapping out a name within <name>. An easy lookup here is http://www.latlong.net/.
What To Do in Xcode
There are two general ways of simulating location on iOS - at runtime and at launchtime. I have found that launchtime is FAR more of a reliable and enjoyable experience, so that's what I'm covering here. Runtime seems to be flakey, sometimes works, sometimes doesn't, etc. As a tester, I like a solid repro. This one works out.
1. Choose the device you are building to from the scheme section of the toolbar:
- If you are building to a simulator, I've found that it's helpful to reset the content and settings each time between location changes. That resets all of the permissions issues that you might be testing in the end, as well as setting everything else back to zero.
- If you are building to a real device (particularly your own) make sure to do a rebuild with location simulation off, and restart your phone before heading home. Otherwise you might be trying to get directions to dinner from France.
2. Edit your current Scheme.
- ⌘ < or Product > Scheme > Edit Scheme
3. From the Run panel, choose Options.
4. Check the checkbox next to Allow Location Simulation.
5. Choose your desired location to simulate.
- If you have created your own GPX file to use, you can import it here at the bottom of the menu - 'Add GPX File to Workspace'
6. Choose Close from the bottom right of the Scheme window.
7. Do a Build (⌘B) to deploy with changed location settings.
- If only deploying to a simulator you can choose to Run Without Building (^⌘R).
8. When the build is fully done and your app has successfully launched, your location should be changed. To make sure, background your app, and open Apple Maps.
9. Use the location tracking arrow to find your current location. It may ask for location services permission if this is its first use.
- I've found it can take a few attempts of pressing the location tracking arrow to catch when simulating.
- If the Apple Maps current location does not match your expected Scheme location, simulating has not worked.
10. Once you have confirmed your location in Apple Maps, move back to your app and test away with a simulated location.
Just a note again to make sure to Reset Content and Settings in your simulator before your next spoof and get your real device back to a real location before finishing. I was warned about this and thought I was covered. I went out for drinks after work and ended up getting super weird Yelp results. Turns out that my phone thought I was in Ireland still.
Even while writing this post this got me. Lately I've been curious about personal analytics, so have been trying out fabric.me to track my own location. I think I broke some speed records!