How To Simulate Location in Xcode and Simulator | by Muralidharan Kathiresan | Dec, 2022

An in-depth guide to navigating the world

photo by Taras Shyapka Feather unsplash

When working on an app that needs to point it at a specific location or provoke custom location movement, it’s tedious to set it manually every time on the simulator or iOS device. We have a few ways to quickly sort them once so that you and your team can then point to multiple locations.

When we want to point to a custom constant location, we have two options:

  • default static location
  • custom static location

default static location

Xcode comes with some default cities to help with generating locations. This can be done in two ways:

(i) By editing the plans

Navigate to the edit scheme by tapping on the Product menu and in the Run menu under the Options tab, as shown in the screenshot below:

Make sure Allow location simulation is selected, and you can select a location from the given option to prompt the simulator/device to point to that location when the app is launched.

(ii) from the menu bar

Tap Debug from the Xcode menu bar and click Excite Location. The same default location options will appear, as seen in the screenshot below:

(iii) from the bottom debug bar

This option will be enabled if the app is enabled with location permission

custom static location

If we need to point our app to a particular location, for example, to point to my hometown, Tiruchirappalli (which is not available as part of the default list), we need the coordinates.

The coordinates of any place can be easily retrieved by right-clicking on Google Maps.

Once we’ve got them, go to the Simulator menu, tap Features → Custom Location, and enter the coordinates. The app will no longer start pointing to the custom location.

custom fixed location using GPX file

To excite a location, we can use a GPX file.

GPX, or GPS Exchange Format, is a xml schema designed as a common GPS Data formats for software applications. can be used to describe waypoint, the tracksand route.

How to create a GPX file?

(i) Xcode File menu → New → File (Ctrl+N), navigate to the Resources column, you may be able to find the GPX file option, click Next, define a name, and you’re done

The default GPX file is the location of the Apple Campus.

Comment: Create GPX file with only one waypoint () with a coordinate you want to simulate

Once done, select it under Edit plan location selection as shown below:

Comment: Here by default, Xcode created a waypoint with name and time, but they are not mandatory properties.

We can save any location points (coordinates) as GPX files and use them in Xcode.

If our app needs to encourage location movement like in a fitness app or a location tracking feature in an app, then just a custom coordinate is not enough. In those cases, we may need to use multiple Tag in GPX file!

Time (Here the tags are needed to simulate realistic movement, and Xcode will interpolate the motion at a rate of speed based on the elapsed time between each waypoint. If you don’t provide a time element, Xcode will use a fixed rate of motion.

is determined by the difference between the time taken from one point to another Element.

Time taken to go from P0 to P1 = (P1 time) — (P0 time)

In the above example, the time of point 1 is 2022–12–04T17:28:09Z and for point 2 2022–12–04T17:30:15Z, There is a difference of two seconds. Xcode will use two seconds to go from point 1 to point 2. During this period, the location will change, which means if you have applied

func locationManager(_ manager: CLLocationManager, 
didUpdateLocations locations: [CLLocation])

You will get updates between two seconds of Xcode emulating from point 1 to point 2.

Comment: Waypoints should be sorted in ascending order by time.

gpx tracker

There are many tools available to create GPX file with our dedicated input, one of them is GPXTracker One open source Tools I use:

Install the app and open it. Just long tap on the location points where you want, as shown below:

Once you have placed the point, you can tap on Save from the bottom bar with the desired name and drag and drop the saved one into Xcode.

Xcode comes with some default dynamic location simulations (predefined moments) that you can use when they are appropriate for debugging your app use case:

  1. city ​​run
  2. city ​​bike ride
  3. freeway drive

These three options will simulate a motion for walking, riding and driving, varying according to speed. This is great for quick debugging.

This is suitable for a quick test as you do not have control over the movement path. For finer control, you’ll need to simulate the motion in Xcode with a GPX file.

cons of location stimulation

These simulations are generally for testing/implementation purposes as they do not help with actual speed and accuracy.

I thank my colleague Daniel Sanchez Cisneros for introducing me to GPX trackers and walking me through location-stimulating techniques.

Hope the article helped you. If you like this article, please let me know by 👏 👏 👏

This Post Has One Comment

Leave a Reply