While working with cross-platform frameworks like React Native, we do not need to build our app every time to test our code on a simulator or real device. It is obvious that we must open the simulator before we can test the app on the simulator.
The common approach to open a simulator can be to start a new build from the Android Studio or the Xcode. It works very well. But it may become frustrating sometimes when you want things to happen very quickly, but the build takes a long time to open the simulator and test the app.
Today, we will be creating some bash
scripts to automate the process and start our simulators by just double-clicking on the bash script files without creating a new build every time we start working on a code.
The below scripts are tested only for MacOS.
First, we will check the device id for the simulator which we want to start. Hit the below command to list all of the installed iOS simulators:
xcrun simctl list
The output will be like this:
== Device Types ==
iPhone 11 (com.apple.CoreSimulator.SimDeviceType.iPhone-11)
iPhone 11 Pro (com.apple.CoreSimulator.SimDeviceType.iPhone-11-Pro)
iPhone 11 Pro Max (com.apple.CoreSimulator.SimDeviceType.iPhone-11-Pro-Max)
iPhone SE (2nd generation) (com.apple.CoreSimulator.SimDeviceType.iPhone-SE--2nd-generation-)
iPhone 12 (com.apple.CoreSimulator.SimDeviceType.iPhone-12)
iPhone 12 Pro (com.apple.CoreSimulator.SimDeviceType.iPhone-12-Pro)
iPhone 12 Pro Max (com.apple.CoreSimulator.SimDeviceType.iPhone-12-Pro-Max)
...
== Runtimes ==
iOS 14.5 (14.5 - 18E182) - com.apple.CoreSimulator.SimRuntime.iOS-14-5
tvOS 14.5 (14.5 - 18L191) - com.apple.CoreSimulator.SimRuntime.tvOS-14-5
watchOS 7.4 (7.4 - 18T187) - com.apple.CoreSimulator.SimRuntime.watchOS-7-4
== Devices ==
-- iOS 14.5 --
iPhone 11 (B8GFGPCF-CA22-4932-85H2-CA2980351D53) (Shutdown)
iPhone 11 Pro (C8GFGPCF-CA22-4932-85H2-CA2980351D75) (Shutdown)
iPhone 11 Pro Max (D8GFGPCF-CA22-4932-85H2-CA2980351D33) (Shutdown)
iPhone SE (2nd generation) (E8GFGPCF-CJ22-4932-85H2-CA2980351D89) (Shutdown)
iPhone 12 (G8GFGPCF-CA22-4932-85H2-CA2990351D34) (Shutdown)
iPhone 12 Pro (H8GFGPCF-CA22-4932-85H2-JA2980351D62) (Shutdown)
iPhone 12 Pro Max (I8GFGPCF-CA22-4932-85H2-LA2980351D85) (Shutdown)
...
Choose the ID for the simulator of your choice from the above list. For example, B8GFGPCF-CA22-4932-85H2-CA2980351D31
for iPhone 11.
Create a bash
file with a name of your choice, e.g. start_iphone11.sh
:
touch start_iphone11.sh
Add the following content in this file(by replacing <YOUR-DEVICE-ID> with the simulator id):
#!/bin/bash
xcrun simctl boot <YOUR-DEVICE-ID>
open /Applications/Xcode.app/Contents/Developer/Applications/Simulator.app/
In our example, for iPhone 11, the content will be like:
#!/bin/bash
xcrun simctl boot B8GFGPCF-CA22-4932-85H2-CA2980351D53
open /Applications/Xcode.app/Contents/Developer/Applications/Simulator.app/
It will boot the simulator app and open the simulator with the selected device.
Hit the below command to list all of the installed android emulators(assuming that you have already installed some emulators in Android Studio’s AVD Manager):
emulator -list-avds
It will list the name of all of the installed emulators like:
Pixel_2_API_29
Pixel_3_API_27_8_
Create a new bash
file with a name of your choice, e.g. start_pixel_2.sh
:
touch start_pixel_2.sh
Add the following content in this file(by replacing <YOUR-DEVICE-NAME> with the emulator name):
#!/bin/bash
emulator -avd <YOUR-DEVICE-NAME>
In our example, for Pixel 2
, the content of the file will be like this:
#!/bin/bash
emulator -avd Pixel_2_API_29
Our next step will be to make these bash scripts executable by double-tapping on them.
To do so, hit the following commands in the terminal inside the same file directory as that of the bash files:
chmod +x start_iphone11.sh
chmod +x start_pixel_2.sh
That's it.
When you execute these scripts(for example, by double-tapping on them), your simulators will be up and running within 2 minutes.