Swift Package Manager

swift-package-manager-guide-project-modules_-.png

If you’re starting out in iOS development you might be wondering what a framework is and how to use one in your app. This article will go into a bit of this detail, explaining what Swift Package Manager is used for and how to implement a simple framework.

During the white boarding process for Share the Load, Ben and I identified that we wanted to use a couple of third party frameworks to make the project more realistic of a ‘commercial’ app than LotR Quiz is currently.

The two frameworks we decided to use was Google Firebase and Realm.

I made a previous post about why Firebase was chosen and I intend on doing one for Realm as well in the future. In short, Firebase is our chosen realtime database to store our quiz questions and Realm will be used for on device persistence.

Basics

If you’re wondering what a framework is I would suggest heading over to Apple’s developer documentation archive right here for a detailed explanation. For the purposes of this article, a framework is a self-contained, reusable resource that you can import into your app.

As for Swift Package Manager, for a detailed article head over to Swift.org here. In short, the Swift Package Manager (sometimes abbreviated to SPM) is a tool for managing the distribution of Swift code and your project dependencies (or frameworks!).

You may have heard of CocoaPods before? If so then you’re not far off with Swift Package Manager either. It does much of the same thing, but with one additional perk over CocoaPods with it being fully integrated right into Xcode.

Swift Package Index

If you’re not sure on the best place to find your desired framework, or you want to make sure you can use SPM for a dependency then look no further than the Swift Package Index. At time of writing, the site has 3,719 packages and 57,854 versions of packages! With a simple search, you can find any framework that might be useful to you.

Let’s look at a real world example of using the ‘Realm’ package for the Share the Load project.

The first thing we want to do is to check the Swift Package Index to see if Realm exists, and we can use it using SPM:

Phew, a Swift Package we can use! 😮‍💨

Phew, a Swift Package we can use! 😮‍💨

Downloading a Package

Once you’ve found the resource required you now need to find a link that will be used in Xcode later.

To do this using Swift Package Index, select the framework you want to use and you’re taken to some more details about the framework.

Here you will find some useful information for the package, such as compatibility, features and how to use the framework.

At the very top of the page there will be a link with a handy ‘copy’ button, go ahead and click this button and then head back to your project in Xcode.

Once you’re back to your Xcode project navigate to: File > Swift Packages > Add Package Dependency. This will then show you a new window called ‘Choose Package Repository’.

Remember that link you copied? This is where you need it! Paste the link into the search bar at the top of this window and hit ‘Next’.

It will take a few seconds to validate the package, but if successful you should then get a second window which asks you to select some options for the package. These options include:

  • Version

  • Branch

  • Commit

Only one of the above options needs to be decided, and ‘Version’ defaults to the latest version of the package. Click ‘Next’.

Xcode might then spend some time thinking about life, however once completed, it will then ask you to choose which packages you want and which target from within your project you’d like to add the package to.

Screenshot 2021-05-17 at 23.36.13.png

Once you’ve selected which package to install, click ‘Finish’ and you should then see the package added to your list of dependencies.

Importing a Package

Now that the package has been downloaded and added to your project there is one last step to do for your app to access the framework, and that is to import it!

Luckily this is quite simple! 😌

import.png

From within your project navigate to a file that you want to use the framework in and at the top of that file you should already see a familiar import declaration.

All you need to do now is to import the package you’ve just installed using the ‘import’ keyword followed by the name of the package you are importing, in this case ‘Realm’ above the scope of all other code in the file.

Removing a Package

If for whatever reason you’d like to remove a package, it’s very simple as well.

Navigate to your App’s Project file and find the ‘Swift Packages’ tab at the top. From here you should see something that looks a little like this:

Screenshot 2021-05-17 at 23.58.10.png

From here all you need to do is select the package you'd like to remove and click on the minus button. Xcode will then warn you that if you remove the package then the world will end and you can proceed at your own risk.

Wrap Up

That pretty much summaries Swift Package Manager, in the above we’ve covered:

  • What a framework is

  • What a dependency manager is

  • How to use the Swift Package Index

  • What steps to take to find a Swift Package

  • Downloading a Swift Package

  • Importing a Swift Package

  • Removing a Swift Package

In terms of the Share the Load project, we have been using Swift Package Manager to import all of our third party dependencies, and unless they are not available via Swift Package Manager (yes, that’s a thing!), then we will continue to do so if we identify any more libraries we want to use.

There are further rabbit holes I could go down, such as the pros and cons over other dependency managers, however that could be an article in itself! 😆