Quick Guide on Swift Package Manager for iOS

Learn how to use the Swift Package Manager with Xcode to add dependencies to your iOS project

Natascha Fadeeva
3 min readMay 17, 2021

It was exciting to hear Apple’s announcement of the Swift Package Manager in 2015, entering the world of dependency managers like CocoaPods and Carthage.

However, it was not until the release of Xcode 11 in 2019 that Swift Package Manager became a first class citizen for iOS projects by finally being integrated into the rest of Apple’s developer tools.

In this article, you will learn the main concepts of Swift Package Manager, how to create your own Swift packages and how to share packages across different projects.

Let’s get started.

Main concepts

Swift Packages are reusable components containing resources and code files that can also depend on other packages.

Compiling a package produces a module which can be shared as a public library using services like GitHub or be used as a locale dependency.

The manifest — a file called Package.swift — defines the name of the package, its contents and dependencies. It is placed in the root directory of each package.

Let’s see how this manifest file looks like by creating a Swift Package.

Creating a Swift Package

One way to create a new Swift Package is by selecting File > New > Swift Package in Xcode. After entering the name, Xcode generates all necessary files and folders together with the manifest file Package.swift.

  1. In the products part, we define executables and libraries a package produces.
  2. In the dependencies part, we declare other packages that our package depends on.
  3. In the targets part, we declare targets our package consists of, e.g. one for our library and one for our tests.

Adding dependencies to the Swift Package

If our package depends on other packages, we can add them in the dependencies section of the manifest file.

dependencies: [   .package(url: "https://github.com/Alamofire/Alamofire.git",      .upToNextMajor(from: "5.2.0"))]

The example adds Alamofire — a third-party library that is available on GitHub — as a dependency. Now, we can use the library by importing it into our source code files.

import Alamofire

We can also add local packages as dependencies.

dependencies: [   .package(path: "../SunshineKit")]

This possibility is very useful, for example, when splitting a project into multiple libraries. This way, we don’t have to worry about versioning and can edit the package’s files directly within the project that’s using it.

Using Swift Packages in iOS Projects

Depending on the project requirements, there are different ways to use Swift Packages in an iOS project.

To add a remote library as a Swift Package, Xcode provides the menu action File > Swift Packages > Add Package Dependency. For a step-by-step guide, check out How to add public libraries as Swift Packages to an iOS project.

As already mentioned above, Swift Package Manager also provides the possibility to work with local packages, for example if we want to organise our code in a modular way. It basically works by dragging and dropping the local package into the Xcode project and adding it in the Frameworks, Libraries, and Embedded Content section. For more details, check out How to add a local Swift Package to an iOS project.

Originally published at https://tanaschita.com.

--

--