Adding dependencies to your Xcode project easily using Swift Package Manager

There are various ways to add dependencies to your Xcode project. There is CocoaPods, a dependency manager for iOS & Mac projects where you specify the dependencies for your project in a simple file, Podfile and install them using some commands. There is also another dependency manager called Cathage that allows you to declare the dependencies for your project in a file, Cartfile and then install them using some commands as well. Then there is the Swift Package Manager (SPM). Before I discuss SPM further, let’s get to know what Swift Packages are.

Swift packages are reusable components of Swift, Objective-C, Objective-C++, C, or C++ code that developers can use in their projects. They bundle source files, binaries, and resources in a way that’s easy to use in your app’s project.

The Swift Package Manager (SPM) is a tool for managing the distribution of Swift code. It is integrated with the Swift build system to automate the process of downloading, compiling, and linking dependencies.

Adding dependencies to your Xcode project using Swift Package Manager (SPM) comes with ease. Latest versions of Xcode (11.0 and above) as at the time of this writing come with full support for SPM, which makes it easy to use.

Let’s see how this is done.

I have created a new Xcode project, LoggrDemoApp where I would be integrating the library, Loggr, a simple logging solution I have created for swift projects. You can check out the article where I described how you can easily create and distribute your own Swift Package.

LoggrDemoApp.xcodeproj 2020 09 03 22 51 38

With the project opened, simply go to New > Swift Packages > Add Package Dependency… A dialog shows up with a search box where you can either search for the desired package or enter the package repository url. Simply type in “Loggr” and hit the enter key, select Loggr from the list of repository displayed. You can also get the package repository URL from Github and enter it in the box instead. Select the package and click on the Next button.

Monosnap 2020 09 03 23 18 02 1

Next is a dialog where you can specify what version of the package you will like to add. You can specify an exact version like 1.0.0 or choose the Up to Next Major, which will always install for you the latest major release. We will go with Up to Next Major and enter “1.0.0” as the minimum package version and click the Next button.

Monosnap 2020 09 03 23 22 54

Xcode will then fetch the package for you and display a dialog where you specify in which Target you want to add the dependency. We go with the LoggrDemoApp Target and click on Finish button.

Monosnap 2020 09 03 23 28 01 1

Automatically, the package would be added to your project and you can see the package both in the Project section and in the Swift Package Dependencies section on the left hand side of your Xcode.

LoggrDemoApp.xcodeproj 2020 09 03 23 31 06

That’s all! Now we can go ahead and make use of our imported library. Now I can proceed to ViewController.swift to test it out. I will import the Loggr module and add some logs.

screencast 2020 09 03 23 40 50

Let’s run the app and see the output in the console.

ViewController.swift 2020 09 03 23 43 59

Yay! It works!

Happy Stephen Colbert GIF

Now, you can see how easy it is to add dependencies to your Xcode project using the Swift Package Manager. Why don’t you go ahead and try it out! 😉😁