Member-only story
Modularize an iOS App With SPM
How to define modules for your app using Swift Package Manager
Back in 2018, during the WWDC, Apple announced the Swift Package Manager (SPM or SwiftPM). The goal of SPM is to allow developers to define modules that can be reused across different applications and platforms. A package could be defined to work with both iOS and macOS, for example, and therefore its features can be shared across tools and apps on those platforms.
In the beginning, the package manager was quite limited: It did not support module-specific resources, for example. During the following years, the SPM has improved a lot. It now allows developers to define executables other than packages and libraries. It supports resources so that we can, for example, add images directly in a UI module and reuse them. Very recently, it replaced also the Xcodeproj files: Xcode is now able to create a project on the fly, starting from the Package.swift
definition!
It still has some limitations: For example, it does not handle iOS apps automatically. However, there is a way to make it work, and today I want to explore with you how to create a modularized iOS application with SPM.
Get Started
The first step to create a modularized application with SPM is to create a folder for the project. Open the terminal and run the following commands:
mkdir SPMApp
cd SPMApp
mkdir SPMAppKit
cd SPMAppKit
swift package init --name SPMAppKit
These commands create a folder called SPMApp
and another folder called SPMAppKit
, and moves us into it. We need this nesting to keep our project tidy. Later in the article, we are going to create a folder for the iOS app because SPM does not allow the creation of an iOS app directly. The whole application code will stay in the SPMAppKit
package; the SPMApp
will work as a container for the package. Finally, with the last command, it creates a package named SPMAppKit
.
The output should look like this:

SPM is creating the Package.swift
file that is our project definition file. Then…