How to Set the Location for iOS Simulator in Your Swift App

By Jesse Feiler

iOS Simulator is built into Xcode. It lets you run your Swift app as if it were running on a device such as iPhone or iPad. It’s a key tool in app development.

iOS Simulator can’t do certain things that an actual device can do. For example, it can manage iCloud synchronization, but in iOS Simulator this is done with a menu command (Debug→Trigger iCloud Sync).

Locatapp uses the location manager tools that are built into iOS. When you’re running on iOS Simulator, those basic tools are available, but the location that they manage is not the location of the simulated device. In order to test your Locatapp code, you must manually set the location you want to use in iOS Simulator. There are two steps to this: setting your device(s) and setting the location to use.

This process can be a little tricky because the location settings for iOS Simulator may have been set by you or someone else in testing apps on the Mac you’re using and you may be mystified by why iOS Simulator thinks it’s somewhere strange to you.

When you are testing with iOS Simulator, you don’t set your actual location. Rather, you set a specific location — that is, the location returned when you query iOS Simulator. This makes it easier to test your code because you know what the location should be.

To set your iOS Simulator location, follow these steps. (Note that these steps also include information about how to check if this setting has already been done.):

  1. To check to see if you have devices that are already configured for iOS Simulator, click the pop‐up menu at the left of the toolbar, as shown.

    image0.jpg

    If the list there contains only the iOS Device command, you don’t have any simulators installed. Proceed to the next step.

    If you do have any simulators installed, you can use iOS Simulator for that device, but if you want to simulate a different device, check to see that that device is installed in Xcode. If it isn’t, proceed to the next step.

  2. In Xcode, choose Window→Devices, as shown.

    image1.jpg

    This opens the Devices window.

  3. In the Devices window, you’ll see the devices you are using along with the simulators you have installed.

    image2.jpg

    The simulators section will probably be blank; if it has any entries, they may not be for the simulator(s) you want to use.

  4. To add a new device, click the + at the bottom of the left sidebar of the Devices window.

    This opens the Create a new simulator sheet.

    image3.jpg

  5. Choose your device type from the pop‐up menu on the sheet and give it a simulator name of your choosing. Select the appropriate iOS version as well.

    This allows you to simulate not only a device but also a version of the operating system.