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.
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.
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.
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.
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.
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.
Let’s run the app and see the output in the console.
Yay! It works!
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! ??