How To Use Swift Package Manager With Xcode: An Introduction and Tutorial
By: Jessica Trinh
Tags: Swift, Programming, iOS, Swift Package Manager, Tutorial

How To Use Swift Package Manager With Xcode: An Introduction and Tutorial Jessica Trinh Follow Mar 27 · 8 min read

@theswiftwebdeveloper

In this article, we will be taking a look into Swift Package Manager: what it is, how it compares to its competition (CocoaPods and Carthage), how to use it, its projected popularity, and why it’s worth picking up.

First Off: What is a Package and Why does it Need Managing?

A package (also known as, namespace in other programming languages), is a file of reusable code put together by other developers to form a module; which can then be built, shipped, and shared into other projects.

A package dependency manager will monitor for changes to all of the packages that have been added to your project. Similar to an office or factory manager, package dependency managers take care of observing activities and making changes from behind the scenes. This ensures that their office, factory, or in this case — project, can continue to run smoothly without having to dedicate more time and energy from other tasks into this kind of maintenance.

Let’s paint a picture on when this might come in handy.

Let’s say you’ve added 10 different packages, like Alamofire, Firebase, etc. to your project. Without a package dependency manager, you would have to actively monitor package authors’ activities. If and when those authors update their code, you would need to manually go to the project repository on a site like Github, see what those changes are, analyze and dissect the changes, and then add those bits and pieces of changed code into your project. You’d continue to do this for all 10 packages, and then you would need to repeat this if there were subsequent rounds of updates down the road. Then again, and again, and so on, until you decide to let this current project of yours rest in peace. But, you would start the process all over again with the packages you implement into your next project.

You might be thinking, “If I don’t use packages at all, then there won’t be a problem” or “goodness gracious, packages sound awful!” Well, hang on there for a second, friend. I don’t mean to scare you away from ever using packages with that scenario just now. Packages are actually great and handy tools that save you from having to reinvent the wheel. They hold the potential to save you hundreds of hours of programming and debugging. All the work to implement one of many features you might like for your application; someone else has taken the time to already build and wrap up nicely into a pretty little package just for others like you to use.

So, instead of dealing with this endless cycle and driving yourself mad, you’d use a package dependency manager. With a typical manager, you can simply go to your project terminal and run a command (such as, ‘pod update’ in the instance of CocoaPods) to fetch all the latest changes that have occurred on all pod files that you’re using. This updates all the packages automatically — inevitably, saving you lots of time and stress. Easy enough, right? And as if you thought it couldn’t get any simpler, Swift Package Manager has taken things to another level of ease and simplicity.

About Swift Package Manager

Swift Package Manager is Apples tool for native implementation of Swift dependencies. While it is not the first dependency manager for Swift projects, it is the first to be officially provided and supported by Apple. It’s also something that only recently became more useful due to a number of updates Apple has made since Swift Package Manager first launched. The idea of Swift Package Manager is that you should simply be able to go to:

Xcode > File > Swift Packages > Add Package Dependency > Select your project > search or enter package dependency URL

The native Swift Package Manager is built into Xcode and eliminates the need to install the package dependency manager using Homebrew, like you would for Carthage, or even having to run a simple “sudo gem install” command like you would for CocoaPods.

Possible Problems

Swift Package Manager was built to be able to work alongside other third party managers whether it be CocoaPods, Carthage, etc. Unfortunately, Swift Package Manager does not always play nicely with CocoaPods.

As of right now, there is a very particular path to go about implementing Swift Package Manager into your project if you plan to do so alongside with CocoaPods. If you first install CocoaPods, then add your Swift packages in the xcworkspace to your project, you will have far fewer problems than if you were to do it the other way around (start off with a normal project, then add the Swift package, and lastly try to add a pod file). This is something Apple developers will hopefully fix within the next few rounds of updating.

Another problem you might run into at the moment is that there are very few developers able to support Swift Package Manager. Anything on Swift Package Manager is most likely on CocoaPods, but the reverse is not true. Although, popularity with Swift Package is growing, CocoaPods has been in the game longer and has a larger history and repertoire of libraries. But, with the rising popularity in Swift Package Manager, this problem will hopefully resolve itself within the next coming years.

To see if a module supports Swift Package Manager, look for the Package.swift file in the author’s repository or documentation.

We’ll take a closer look here in our demo.

Demo: Installing Alamofire with Swift Package Manager

Alamofire is one of the biggest and most popular third party modules out there. It’s also something that can be installed using CocoaPods, Carthage, or Swift Package Manager in case you’d like to explore the different ways of implementation.

Step 1: Locate the Package You’d Like to Implement

@alamofire

Taking a closer look at the main directory, determine if the project supports package managers:

Files with Package.swift means they support Swift Package Manager

Similarly, files ending in .podspec means it supports CocoaPods

If the project supports the package dependency manager you’re using, stay in the root directory of the Github repository and copy the project URL

Note: To make sure you’re in the root project directory, the URL should only include the domain name, username and the package name only (ie github.com/Alamofire/Alamofire)

Step 2: Head Over to Your Project in Xcode and Click File

Note: Make sure you’re in the Xcode window when you click File

Step 3: Avert Your Eyes Lower and Click Swift Packages and Add Package Dependency

Step 4: Add the URL to Choose Package Repository

Note: If you already have Pods installed, you will be prompted to choose which project to add the package to. If you intend to use Pods alongside with Swift Package Manager, go ahead and install Pods first before continuing with this tutorial. When you’re at this step: Choose your main project, not Pods.

If you didn’t already have Pods installed, or you just completed the steps above and clicked Next, you should now see a prompt to choose a package repository.

Go ahead and paste in the Alamofire URL you copied earlier into the search bar. Then click Next.

Step 5: Setup Package Rules

This prompt here is applied whenever the author of the package pushes an update. For the duration of this tutorial, and most likely your project; I recommend having rules stick to the default, “Up to the Next Major Update” for your project to be able to benefit from the updates and bug fixes that get brought to the package when iOS and Xcode changes. It’s very likely that updates also entail an update in the version of Swift. If there are discrepancies in the versions of Swift, then it might cause your project to break. The other options, such as choosing Exact, will be even less likely to break but the updates will most likely not be as important — hence, Next Major Update.

Step 6: Wrapping Up

After clicking Next, you’ll be prompted with a message that the package is downloading and being added to your project.Yay!

Go ahead and click Finish.

Now if you take a look at your left-side pane, you’ll see Swift Package Dependencies has been added to your project.

And there you have it. Alamofire has been installed natively using Swift Package Manager and Xcode, and you did not have to write a single line of code to get it done. Congratulations!