Xcode Simulator App Advanced

In this tutorial, you’ll learn about Xcode Simulator’s advanced features to improve your daily development experience. By Vidhur Voora.

Leave a rating/review
Download materials
Save for later
Share
You are currently viewing page 2 of 4 of this article. Click here to view the first page.

Pixel Accurate

Simulator in pixel accurate mode

In this mode, the window size changes to the same number of pixels as the physical device. Each pixel on the simulated device maps to one pixel on your Mac’s display. This causes simulators to appear larger on screen if your Mac’s display has a lower pixel density than the simulated device. You can use this mode to check the alignment of images and controls in your app.

Fit Screen

Simulator in fit mode

This resizes the simulator to the size of your Mac display.

Next, you’ll learn the various options provided in a simulator.

Slow Animations

Animations are an integral part of an app experience. Build and run RayWonders in the Demo simulator.

Tap the Photos tab. Next, tap a picture of one of the wonders of the world to present a view of details about the place. Dismiss the view by sliding it down.

To simulate slow animations, select the Debug ▸ Slow Animations option in the Simulator menu.

Slow animation menu option

Tap the picture again. The view now animates slowly.

RayWonders on simulator with slow animation

Viewing the animations slowly can help bring clarity. In addition, it can help in examining problems in rendering and performance.

Next, you’ll learn to simulate Dark Mode. Before proceeding, disable the slow animations by deselecting the Debug ▸ Slow Animations menu option.

Dark Mode

Dark Mode provides a great viewing experience in low-light environments. The simulator provides an option to view your app in Dark Mode.

Toggle appearance menu option

Select Features ▸ Toggle Appearance. This toggles the appearance to Dark Mode. Now, tap the Map tab in RayWonders.

RayWonders map Dark Mode

You’ll notice that the map has changed to a dark appearance. Easy, isn’t it? This is a handy way to test your app in Dark Mode.

To change the appearance back to default, deselect Features ▸ Toggle appearance. To learn how to support Dark Mode, check out Supporting Dark Mode: Adapting You App to Support Dark Mode tutorial.

Next, you’ll learn how to simulate push notifications!

Simulating Push Notifications

Push notifications are a great way to let your users know of new content updates in your app. In the past, testing push notifications was a challenge. You needed a physical device to test the push notifications. Starting with Xcode 11.4, you can simulate push notifications on a simulator.

Create a file named RayWondersPushNotification.apns using the following:

{
  "Simulator Target Bundle": "com.raywenderlich.RayWonders",
  "aps": {
    "alert": {
      "title": "Hindi language support added!",
      "body": "Checkout RayWonders in Hindi!"
    }
  }
}

This is a simple, visible push notification payload with a title and a body. The Simulator Target Bundle is a special key. It specifies the bundle ID of the app that should receive the notification.

Before trying the payload, you need to first grant the notification permission in the app.

Grant notification permission

Follow these steps:

Home menu option in simulator

  1. Open RayWonders in the Demo simulator.
  2. Tap the bell icon on the top right in the Photos tab.
  3. Select Allow in the notification permission prompt.
  4. Select Device ▸ Home from the Simulator menu to put the app in the background.
  5. Home menu option in simulator

Next, drag and drop RayWondersPushNotification.apns file on the Demo simulator.

Simulate push notification using drag and drop

Ta-da! A visible push notification appears. That was easy! Bear in mind that a simulator can only simulate a push notification. To test an actual push notification received from the Apple push notification server requires a physical device.

To learn about push notifications in more depth, read this Push Notifications Tutorial: Getting Started.

Next, you’ll learn how to zoom.

Zooming In and Out

Open RayWonders in the simulator. Switch to the Map tab. The map is centered and zoomed in on a wonder of the world. Where are the other wonders of the world?

You’ll need to zoom out to see the world view. Click and drag while holding down the Option key.

Zoom and pan in simulator

Clicking and dragging is great, but it can soon get tiresome. Next, you’ll learn a quicker way to go to a location.

Simulating a Location

The simulator provides an easy way to simulate a location. First, you need to grant location permissions in RayWonders.

Grant location permissions

Follow these steps:

  1. Open RayWonders in the simulator and switch to the Map tab.
  2. Tap the Start Location Services button in the Map tab.
  3. Select the Allow While Using App permission.

Now, to simulate location:

Simulate location menu

Simulate location coordinates

  1. Select Features ▸ Location ▸ Custom Location from the Simulator menu.
  2. Simulate location menu

  3. Enter 41.8902142 as the latitude and 12.4900422 as the longitude.
  4. Click OK.
  5. Simulate location coordinates

This takes you directly to the Colosseum on the map — your fastest journey ever!

Colosseum on the map

However, entering the coordinates for each location to simulate is quite a memory-intensive — your memory, that is — task. Next, you’ll learn an easier solution to this problem.

Sharing Locations From the Maps App

The Maps app on macOS provides an easy way to share locations with your simulator.

Simulate location from Maps app

Follow these steps:

Simulator menu option in Maps

  1. Open the Maps app.
  2. Enter Machu Picchu in the search text.
  3. Click the Share button next to the search field.
  4. Choose Simulator from the drop-down.
  5. In the location prompt, select Demo as the simulator.
  6. Simulator menu option in Maps

  7. Click Send.

You’re now in Machu Picchu in the simulator’s map view.

Location simulated in RayWonders

Congratulations! You’ve just created a world record as the fastest traveler from the Colosseum to Machu Picchu! :]

To learn about handling location in your app in more depth, check out MapKit and Core Location.

Now, it’s time to shake things up.

Simulating Shake Gesture

Shake gestures are great cues to prompt the user to give feedback in an app. But how can you shake a simulator? Thankfully, shaking the Mac rigorously is not the solution. The simulator provides an easy way.

Open RayWonders on the Demo simulator. Select Device ▸ Shake from the Simulator menu.

Shake menu option

This simulates a shake gesture.

Shake detected in RayWonders

RayWonders detects the shake and prompts an alert to the user. You can detect and handle shake gestures in your app by implementing the method motionEnded(_:with:). To learn more, check out the Apple documentation.

Next, you’ll learn how to simulate a memory warning.

Simulating a Memory Warning

iOS sends a warning to the app when its memory usage approaches the upper limit on the device. The app needs to respond by purging the cache data, which can be re-created later.

Open RayWonders on the Demo simulator. Select Debug ▸ Simulate Memory Warning from the menu.

Memory warning menu option

This simulates a memory warning.

Memory warning in RayWonders

RayWonders shows an alert. You can learn more about handling memory warnings in this Responding to Memory Warnings Apple documentation.

So far, you’ve seen some useful simulator options. There are several others that aren’t covered in this tutorial. You should check them out, depending on your app needs. Some of these options include:

  • Simulate iCloud sync using Features ▸ Trigger iCloud Sync.
  • Simulate Apple Pay authorization using Features ▸ Authorize Apple Pay.
  • Trigger Siri using Device ▸ Siri.
  • Check System Log using Debug ▸ Open System Log.

Next, you’ll learn to interact with simulators using the command line.