Ditching Xcode’s ▶️ button

Building and running iOS apps from the command line

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.

  1. Collect required information
  2. Build the project
  3. Install the app in the iOS Simulator
  4. 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:

  1. Project/Workspace name
  2. iPhone Simulator name and OS version
  3. Scheme
  4. Bundle id

We’ll let Xcode help us extract the required information.

So we have the following information:

  1. project = Example.xcodeproj
  2. simulator = iPhone 11 Pro Max, 13.1
  3. scheme = Example
  4. 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 you have a project
xcodebuild -project Example.xcodeproj 
-scheme Example 
-configuration Debug 
-destination 'platform=iOS Simulator,name=iPhone 11 Pro Max,OS=13.1' 
-derivedDataPath build

# if you have a workspace
xcodebuild -workspace Example.xcworkspace 
-scheme Example 
-configuration Debug 
-destination 'platform=iOS Simulator,name=iPhone 11 Pro Max,OS=13.1' 
-derivedDataPath build

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:

# instal app in the simulator
xcrun simctl install 
"iPhone 11 Pro Max" 
./build/Build/Products/Debug-iphonesimulator/Example.app

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.

Step 3: Launch the app in the Simulator

In the final step, we’ll run the command to launch our app in the Simulator.

# launch app in the simulator
xcrun simctl launch "iPhone 11 Pro Max" com.ihak.Example

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.

#!/bin/bash

# Clean the build
rm -rf ./build

# Build the project
xcodebuild -project 
Example.xcodeproj 
-scheme Example 
-configuration Debug 
-destination 'platform=iOS Simulator,name=iPhone X' 
-derivedDataPath build

# Install the app in the Simulator
xcrun simctl install 
"iPhone X" 
./build/Build/Products/Debug-iphonesimulator/Example.app

# Launch the app after installation
xcrun simctl launch "iPhone X" com.ihak.Example

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.

Avatar photo

Fritz

Our team has been at the forefront of Artificial Intelligence and Machine Learning research for more than 15 years and we're using our collective intelligence to help others learn, understand and grow using these new technologies in ethical and sustainable ways.

Comments 0 Responses

Leave a Reply

Your email address will not be published. Required fields are marked *