Getting Started in iOS Simulator

The iOS Simulator app, available within Xcode, presents the iPhone or iPad user interface in a window on your Mac computer. You interact with iOS Simulator by using the keyboard and the mouse to emulate taps, device rotation, and other user actions.

This chapter explains the basic capabilities of iOS Simulator. You will use an iPhone app to gain some hands-on experience with iOS Simulator, allowing you to become familiar with the tool. If you do not have an iPhone app to use, you can use the HelloWorld app. For more detailed information on interacting iOS Simulator and using it to test and debug your apps, refer to the later chapters in this book.

Access iOS Simulator from Xcode

There are two different ways to access iOS Simulator through Xcode. The first way is to run your app in iOS Simulator, and the second way is to launch iOS Simulator without running an app.

Running Your App in iOS Simulator

When testing an app in iOS Simulator, it is easiest to launch and run your app in iOS Simulator directly from your Xcode project. To run your app in iOS Simulator, choose iPhone Retina (4-inch) from the Xcode scheme pop-up menu, and click Run. Xcode builds your project and then launches the most recent version of your app running in iOS Simulator on your Mac’s screen, as shown in Figure 1-1.

Figure 1-1  Simulated iPhone running the HelloWorld app

Launching iOS Simulator Without Running an App

At times, you may want to launch iOS Simulator without running an app. This approach is helpful if you want to test how your app launches from the Home screen of a device or if you want to test a web app in Safari in iOS Simulator.

To launch iOS Simulator without running an app

  1. Launch Xcode.

  2. Do one of the following:

    • Choose Xcode > Open Developer Tool > iOS Simulator.

    • Control-click the Xcode icon in the Dock, and choose Open Developer Tool > iOS Simulator from the shortcut menu.

iOS Simulator opens and displays the Home screen of whichever simulated device was last used.

View the Installed Apps

From the Home screen, you have access to all of the apps that are installed in the iOS 7.1 simulation environment. There are two ways to access the Home screen in iOS Simulator from your app:

Much like the Home screen on an iOS device, the simulator’s Home screen has multiple pages. After clicking the Home button (or accessing the Home screen through the Hardware menu), you arrive at the second page of the Home screen. To get to the first page, where all of the preinstalled apps are found, swipe to the first Home screen by dragging to the right on the simulator screen.

On the Home screen, you see all of the apps that have been preloaded into iOS Simulator, as shown in Figure 1-2.

Figure 1-2  The iOS Simulator Home screen for a simulated iPhone in the iOS 7.1 simulation environment

The apps you see on the Home screen are specific to the simulated iPhone in the iOS 7.1 simulation environment. Because the Calendar app is available only in iOS simulator on iOS 7.0 and later, and because Passbook is available only for the iPhone, these apps won’t appear if you switch to a legacy simulator or to simulating an unsupported device type.

Use the installed apps to test your app’s interaction with them. For example, if you are testing a game, you can use iOS Simulator to ensure that the game is using Game Center correctly.

Use Safari to Test Web Apps

From the Home screen you can access Safari within iOS Simulator. Use Safari to test your iOS web apps directly on your Mac.

  1. From the Home screen, click Safari.

  2. In the address field in Safari, type and press the Return key.

If your Mac is connected to the Internet, Safari displays the Apple website. See Figure 1-3.

Figure 1-3  The Apple website running in Safari in iOS Simulator

Use Maps to Simulate Location Awareness

iOS Simulator provides tools to assist you in debugging your iOS app. One of the many features you can debug in iOS Simulator is location awareness within your app. Here’s an example of how to simulate a different location, which can be seen in the Maps app:

  1. From the Home screen, click Maps.

  2. Choose Debug > Location > Custom Location.

  3. In the window that appears, type the number 40.75 in the latitude field, and the number -73.75 in the longitude field.

  4. Click OK.

  5. Click the Current Location button in the bottom-left corner of the simulated device screen.

After completing this task, notice that the blue dot representing your location is in New York, NY, near the Long Island Expressway, as shown in Figure 1-4.

Figure 1-4  Running Maps and simulating a latitude and longitude in iOS Simulator

Change the Simulated Device and iOS Version

iOS Simulator provides the ability to simulate several devices:

In addition to simulating various hardware devices, you can also change the simulated software version. Each simulated software version is considered its own simulation environment, and each simulation environment has its own settings and applications. iOS Simulator supports the following software versions:

To change the device you’re simulating to an iPad running iOS 6.1, choose Hardware > Device > iPad > iOS 6.1. If iOS 6.1 is not listed as an option in the menu, you need to download the legacy simulator in Xcode and then relaunch iOS Simulator.

To download a legacy simulator

  1. In Xcode, choose Xcode > Preferences.

  2. In the Preferences window, click Downloads.

  3. In Components, find the legacy simulator version you want to add, and click the Install button.

Figure 1-5  iOS Simulator displays a simulated iPad running iOS 6.1

The iOS Simulator display you saw in Figure 1-2 changes from an iPhone running iOS 7.1 to an iPad running iOS 6.1, as shown in Figure 1-5.

Because the Passbook app is available only on iPhone, its icon, which was visible in Figure 1-2 for iPhone, is not visible in Figure 1-5 for iPad. Additionally, since the new user interface (UI) was first introduced in iOS 7.0, notice that the simulated iPad running iOS 6.1 is now displaying the old UI and icon designs.

If you are using a computer whose screen isn’t large enough to display an entire simulated iPad, only the screen of the iPad is displayed, not the device border around it. If this is true for you, you must access the Home screen though the Hardware menu.

If you are testing an iPad app, you can test only on a simulated iPad. If you are testing an iPhone or a universal app, you can test on either a simulated iPhone or a simulated iPad.

Alter the Settings in iOS Simulator

You can alter the settings within iOS Simulator to help test your app. To open the Settings app in iOS Simulator, go to the Home screen in iOS Simulator and click Settings. In Figure 1-6 you see the Settings app as it appears when launched in the iOS 6.1 simulation environment.

Figure 1-6  Example of the Settings app in a simulated iPad device

The iOS Simulator settings differ from the settings found on a hardware device. iOS Simulator is designed for testing your apps, whereas a hardware device is designed for use. Because iOS Simulator is designed for testing apps, its settings are naturally focused on testing too. For example, in iOS Simulator the Accessibility menu provides the ability to turn on the Accessibility Inspector, and the Accessibility menu on a device allows you to turn on and off different accessibility features.

Through the settings, you can test both accessibility and localization of your app. See “Testing and Debugging in iOS Simulator” for information on how to manipulate your settings for the various types of testing you are interested in.

Rotate the Device

You can use iOS Simulator to manipulate the simulated device much as you do a physical device.

To rotate your simulated device, choose Hardware > Rotate Left. When you rotate your simulated device, you see that Settings rotates (see Figure 1-7), just as it would on a hardware device.

Figure 1-7  A rotated simulated iPad running in the iOS 6.1 simulation environment

Test in iOS Simulator and on a Device

iOS Simulator is designed to assist you in designing, rapidly prototyping, and testing your app, but it should never serve as your sole platform for testing. One reason is that not all apps are available in the simulator. For example, the Camera app is available only on hardware devices and cannot be replicated in the simulator.

In addition, not all bugs and performance problems can be caught through testing in iOS Simulator alone. You’ll learn more about performance limitations in “Testing and Debugging in iOS Simulator.” You can also find more information on testing your app on a device in “Launching Your App on Devices” in App Distribution Guide.

Quit iOS Simulator

iOS Simulator continues running until you quit it. Even if you quit Xcode, iOS Simulator continues to run because it is a separate app. To quit iOS Simulator, choose iOS Simulator > Quit iOS Simulator. If Xcode is running, Xcode remains open.