Use Swift Package Manager to manage dependencies and create reusable modules

Use Swift Package Manager to manage dependencies and create reusable modules

Managing dependencies is one of the most common tasks for developers while working on an iOS project. We are bound to use external libraries for common tasks — whether it’s networking, loading images, using some custom UI components, parsing etc.

CocoaPods and Carthage were two popular options for dependency management for quite a long time among the iOS developers community. Since the release of Swift Package Manager (SwiftPM) in 2015, it has started attracting developers. Its popularity started increasing rapidly after the integration of the tool with Xcode 11 in 2019.

With Swift Package Manager, we can easily create, distribute, and consume Swift packages. A Swift package is a collection of Swift source files and resources that can be used as a dependency by other packages or projects. The Swift Package Manager handles the downloading, building, and linking of your dependencies. It also handles the resolution of any conflicts or compatibility issues.

Adding dependencies to your iOS project

It’s really easy to add dependencies to our iOS project using Swift Package Manager. At Evangelist Apps, we use The Composable Architecture (TCA) the library for most of our projects. Let’s see how we can add the TCA dependency using SwiftPM.

The easiest way to add any dependency using SwiftPM is to select Add Package Dependencies… option from Xcode File Menu -

Next, we enter the package URL to get the desired package and hit the Add Package button.

This will start downloading the package from the remote URL -

We finish the process by adding the package to our target -

From the Xcode Project Navigator, you can see the Package Dependencies section, which will list the package you added, with all of the packages that it depends on.

Here we have added the “swift-composable-architecture” package only. But this package has other dependencies. SwiftPM resolves its dependencies and added those packages as well.

Now we can start using the TCA library by importing it -

As you can see, it’s really easy to use SwiftPM to manage dependencies. Next, we will have a quick look into how we can create our reusable module using SwiftPM.

Create your reusable module

From Xcode File menu, select “New” -> “Package” -

Select the “Library” template under “Multiplatform” Section -

Provide a name for the library, and tap on “Create” button -

Xcode will generate all the necessary files and folders for our Swift package

The Package.swift file, also known as the “package manifest”, describes the configuration of our Swift package. It defines the package name, products, targets, dependencies on other packages etc.

We can see the package manifest begins with the string // swift-tools-version:, followed by a version number such as // swift-tools-version:5.9.

By specifying the Swift tools version, we declare the version of the PackageDescription framework, as well as the Swift language compatibility version and minimum version of the Swift tools to use the package.

The products section of the manifest defines the output of our package, such as a library or an executable.

The dependencies section defines the external packages that our package depends on.

The targets section defines the source files and resources that make up our package, as well as any test cases that we have.

You can check out the Apple documentation to learn more about this.

Adding your Code

Now let’s add our code to the package so that it can be used for our iOS projects. We add our code to the Sources directory.

After we have created our Swift package, we need to push it to a Git repository that is publicly accessible, such as GitHub, or Bitbucket.

To publish our Swift package, we need to create a tag that matches the version number of our package and push it to the remote repository. We can use the git tag and git push commands to do this. For example, if we want to publish the version 1.0.0 of our package, you can write something like this:

git tag 1.0.0
git push origin 1.0.0        

By creating and pushing a tag, we are telling the Swift Package Manager that this is a stable release of our package that other developers can use.

Once we have published our Swift package, we (and other developers) can add it as a dependency on other Swift projects. As we can see, SwiftPM is a powerful tool to simplify our development workflow.

要查看或添加评论,请登录

社区洞察

其他会员也浏览了