AH Technology

Albert Heijn, part of Ahold Delhaize, is the number one food retailer in the Netherlands. We’ve…

Follow publication

Replacing CocoaPods with Swift Package Manager

--

CocoaPods has long been one of the most popular package managers for developing iOS applications. Making it easy to import external libraries without having to manage dependencies and project settings yourself. For a few years already Apple has been developing and expanding the Swift Package Manager. Until recently SPM (Swift Package Manager) could only be used for server side swift or command-line applications. But with the release of Xcode 11 SPM is now fully supported and ready to use for building iOS or Mac applications.

One of the benefits of this is not having to use an external package manager but one that is fully integrated with the ecosystem and ready to use within Xcode. Another benefit opposed to CocoaPods is that there is no longer a need to use a workspace for developing your application and the Swift Package Manager doesn’t drastically change the structure of the project file like CocoaPods and isn’t dependant on Ruby and scripts tying your project together.

To add a Swift package to your project simply navigate you project in the sidebar, select your project and navigate to the “Swift Packages” tab.

The Swift Packages tab in Xcode

We can add a new package by pressing the + button.

Adding a Swift package

When pressing the plus button we are prompted to enter a package repository URL. You can find this by navigating to the Github page of the framework you want to import or you can discover new packages at: https://swiftpack.co/

You can identify if the framework you want to add supports SPM by checking if there is a “Package.swift” file present. This “Package.swift” file describes the package to the SPM and provides details about it’s dependencies but I’ll cover this in a future tutorial where we’ll be creating our own Swift package!

Once you’re sure the framework you want to add supports SPM we simply copy the repository URL into the search field and press “Next”. Once Xcode has verified the URL actually refers to a Swift package we can define the rules on what version of the package we want to import.

Defining the rules for the imported Swift package

We can either specify which version we want to use by choosing the version option which will refer to the releases of the package. Or we can choose a specific branch of the repository or even a specific commit (perhaps one that contains a bugfix we want to use).

After pressing next Xcode will resolve the package and present us with the choice of products of the package we want to add.

Choosing the product to add to our application

In the case of Alamofire there is only one product. However some packages supply multiple products. Refer to the readme of the package you’re trying to add to find out which one you need.

After pressing Finish Xcode will add the package to your project and it will appear in the project navigator.

Project navigator

Now we’re all set and ready to use the package in our application. To use the package we simply import it in our code and we can use all of its public functions.

Saying goodbye to CocoaPods

After migrating the package to SPM we can remove it from the Podfile and run pod install to update to the pods defined in your Podfile. Once you’re done migrating all packages to use SPM you can fully remove CocoaPods from your project. This is done by running the following Terminal commands in the root directory of your project.

Removing CocoaPods from your project

In the next tutorial I’ll cover how you can create your own Swift package and use it in your iOS project.

Thanks for reading! I’m also active on Twitter and Instagram where I post daily tips and tricks.

--

--

Published in AH Technology

Albert Heijn, part of Ahold Delhaize, is the number one food retailer in the Netherlands. We’ve been inspiring customers and building trust for more than 130 years. We making better food accessible to everyone — and making shopping fit the way people live today. #ahtechnology

Written by Jimmy Arts

Lead iOS engineer @ Polarsteps, Vapor enthusiast.

Responses (1)

Write a response