If you’re tired of building and running your iOS apps via Xcode’s ▶️ button, let’s explore an exciting way to build and run your apps without touching the ▶️ button (ever again).
This is a four-step process.
- Collect required information
- Build the project
- Install the app in the iOS Simulator
- Launch the app in the iOS Simulator
Overview
Xcode comes with a number of command line tools. These tools are capable of performing pretty much every thing you can do via Xcode’s UI. While you need a human to point and click certain buttons in Xcode to make it work, these command line tools can help automate the whole process of building and running your project. Powerful, right?
Collecting the Info
The pieces of information required to build and run the app via the command line are as follows:
- Project/Workspace name
- iPhone Simulator name and OS version
- Scheme
- Bundle id
We’ll let Xcode help us extract the required information.
So we have the following information:
- project = Example.xcodeproj
- simulator = iPhone 11 Pro Max, 13.1
- scheme = Example
- bundle_id = com.ihak.Example
Now that we have all the pieces of the puzzle, let’s start the build process.
Step 1: Build the project
xcodebuild
: the command line tool that allows us to build our project. Run any one of the following commands, depending on if you have a project or a workspace.
If everything goes well, and it should, you’ll see **BUILD SUCCEEDED**
at the end of the command.
You might have also spotted that our command provides a build
folder to be used as a derivedDataPath
. This will help us find the finished product to be installed and run on the iOS Simulator.
Step 2: Install the app in the iOS Simulator
xcrun
: helps locate and run developer tools
simctl
: command line utility to control the Simulator
(The simctl
command isn’t available as a standalone command like xcodebuild
. It’s used as a subcommand to xcrun
.)
Run the following command to install the app in the Simulator:
⚠️ Warning: Make sure the
Simulator
app is running andiPhone 11 Pro Max
is already booted. Otherwise, the above command will fail.
Note: You can also pass a booted
argument instead of the Simulator name. In this case, the first booted device (when Simulator
was launched) will be used.
Looking for new ways to elevate your mobile app’s user experience? With Fritz AI, you can teach your apps to see, hear, sense, and think. Learn how and start building with a free account.
Step 3: Launch the app in the Simulator
In the final step, we’ll run the command to launch our app in the Simulator
.
Run Script
The process of building and running the app via the CLI (Command Line Interface) is exciting but a bit lengthy. Especially when you compare it to pressing the Xcode’s ▶️ button.
But the real power of those tools come when we combine them in an executable bash script.
Create a text file with the name runscript.sh
and paste the above three commands in that file.
Make the runscript.sh
executable by issuing the following command:
chmod +x runscript.sh
At this point, your script is ready to be executed. Execute the script by issuing the following in the CLI:
./runscript.sh
That’s it! You only need to run above command in the CLI to build, install, and launch your app in the iOS Simulator.
What’s Next?
This very simple script gives you the power to build and launch your app with a single-line command. It’s particularly useful when you’re thinking about automating the app-building process. This script can be extended to execute unit tests and archive your app to distribute it later.
This can be a starting point for you to setup CI (continuous integration) in you project as well.
Editor’s Note: Heartbeat is a contributor-driven online publication and community dedicated to exploring the emerging intersection of mobile app development and machine learning. We’re committed to supporting and inspiring developers and engineers from all walks of life.
Editorially independent, Heartbeat is sponsored and published by Fritz AI, the machine learning platform that helps developers teach devices to see, hear, sense, and think. We pay our contributors, and we don’t sell ads.
If you’d like to contribute, head on over to our call for contributors. You can also sign up to receive our weekly newsletters (Deep Learning Weekly and the Fritz AI Newsletter), join us on Slack, and follow Fritz AI on Twitter for all the latest in mobile machine learning.