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.
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Contents
Xcode Simulator App Advanced
30 mins
- Getting Started
- What Is a Simulator?
- Running App on a Device vs. a Simulator
- Computing Performance
- Display
- Hardware Limitations
- Framework Limitations
- Organizing Simulators Using Xcode
- Running Older Runtimes
- Creating Simulators From the Simulator Menu
- Comparing Simulator Size Options
- Physical Size
- Point Accurate
- Pixel Accurate
- Fit Screen
- Slow Animations
- Dark Mode
- Simulating Push Notifications
- Zooming In and Out
- Simulating a Location
- Sharing Locations From the Maps App
- Simulating Shake Gesture
- Simulating a Memory Warning
- Organizing Simulators Using the Command Line
- Creating Simulator From the Command Line
- Taking Screenshots
- Recording Video
- Customizing the Status Bar
- Debugging and Diagnosing
- Automating Using a Bash Script
- Implementing Launch
- Launching RayWonders in a Different Locale
- Where to Go From Here?
Pixel Accurate
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
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.
Tap the picture again. The view now animates slowly.
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.
Select Features ▸ Toggle Appearance. This toggles the appearance to Dark Mode. Now, tap the Map tab in RayWonders.
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.
Follow these steps:
- Open RayWonders in the Demo simulator.
- Tap the bell icon on the top right in the Photos tab.
- Select Allow in the notification permission prompt.
- Select Device ▸ Home from the Simulator menu to put the app in the background.
Next, drag and drop RayWondersPushNotification.apns file on the Demo simulator.
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.
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.
Follow these steps:
- Open RayWonders in the simulator and switch to the Map tab.
- Tap the Start Location Services button in the Map tab.
- Select the Allow While Using App permission.
Now, to simulate location:
- Select Features ▸ Location ▸ Custom Location from the Simulator menu.
- Enter 41.8902142 as the latitude and 12.4900422 as the longitude.
- Click OK.
This takes you directly to the Colosseum on the map — your fastest journey ever!
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.
Follow these steps:
- Open the Maps app.
- Enter Machu Picchu in the search text.
- Click the Share button next to the search field.
- Choose Simulator from the drop-down.
- In the location prompt, select Demo as the simulator.
- Click Send.
You’re now in Machu Picchu in the simulator’s map view.
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.
This simulates a shake gesture.
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.
This simulates a memory warning.
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.