Swift Package Manager

Codecademy Team
Learn how to use Swift Package Manager to add dependencies to your project.

Dependencies

When building applications, it is often helpful to use code that other developers have written to build more powerful applications more quickly. In iOS development, you have access to SwiftUI, which is code that Apple developers have built. But what if you want to add a complex animation to your project that’s not built-in? Just like you import SwiftUI into your projects, you can import frameworks that other developers have built. Whenever you use someone else’s code in your project, that is known as a dependency, because your code now depends on theirs to work.

By using code that other developers have written instead of writing everything from scratch, you can build more complex applications more quickly. The downside is that if their code breaks due to an update, your application might break as well.

Let’s say you want to add a pie chart to your application, but there’s no default PieChartView in SwiftUI. You could use someone else’s PieChartView by simply copying and pasting it into your codebase. However, you’d need to make sure that they gave permission and it would be challenging to update the copied code when the original developers make changes. Additionally, it might be challenging to find all of the necessary files to make the dependency work, and the dependency might even contain private code that you can’t view. Fortunately, there are many tools in iOS development that make it easy to manage dependencies.

Before getting started with Swift’s dependency management tools, it’s important to note that the best solution is often to write all of your code from scratch when possible. Adding dependencies to your project can make it much harder to maintain and can introduce problems between dependencies. Try to only add a dependency if it uses technology you can’t implement on your own, like Firebase, or if it is very well established, like Alamofire.

Dependency managers in iOS development

There are three main dependency management tools when developing iOS applications:

Swift Package Manager is the newest tool for managing dependencies, and the one that we’ll be using in this article. It is fully integrated with Xcode and has an easy-to-use interface for adding and removing dependencies from your application. However, because it’s the newest tool, some packages might not have support for Swift Package Manager. As it becomes more mature, expect more packages to be available.

Before Swift Package Manager, CocoaPods was the primary tool for managing dependencies. Using CocoaPods requires creating a separate file known as a Podfile that outlines the different packages your application will import. Then, you need to run some instructions in Terminal that download the dependencies and add them to your project.

Another dependency management tool that you might see is Carthage. Just like CocoaPods, Carthage requires doing some setup in the terminal and creating a separate file to list your dependencies. However, while CocoaPods will add the dependencies directly to your project, you need to manually move the dependencies that Carthage generated into Xcode yourself.

While all these tools work for managing dependencies, because Swift Package Manager is directly integrated with Xcode, it’s the best option if the packages you are trying to add have support for it.

Adding packages with Swift Package Manager

Swift Package Manager is integrated directly into Xcode, making it easy to add packages. To get started, first find a package that you want to add to your project. You can find all the options at sites like this one here.

Let’s say you want to build an application that uses toasts to display a popup that disappears by itself. A toast looks like this:

toastDemo

By default, iOS doesn’t have a way to present toasts. While you could write your own code, you can also find a dependency or package that another developer has written. Searching for “toast” in the Swift Package Registry gives the following results:

toastSearchResults

To find which will serve your purposes, it is helpful to look at each of the repositories and see which are built for SwiftUI as well as how many stars the repository has. From the options above it looks like ToastUI will work. To add it into a project, click on File -> Add Packages….

addPackageMenu

In the search field at the top right, enter the URL of the package (https://github.com/quanshousio/ToastUI), then click on Add Package

findToastPackage

Once the package has been downloaded, click on Add Package to add the package to Xcode.

addToastPackage

You can now see the package that you’ve added in the Navigator View. You can even expand the “Sources” directory to see exactly how the package was implemented.

navigatorViewPackage

Now that the package has been added to Xcode, you can import it just like you import SwiftUI into files that depend on SwiftUI. The view below contains a button that will present a toast when tapped. The .toast(isPresented:dismissAfter) modifier and ToastView are both available because of the ToastUI library that we are importing into the file.

import SwiftUI
import ToastUI
struct ContentView: View {
@State private var showToast = false
var body: some View {
VStack {
Spacer()
Button {
showToast = true
} label: {
Text("Show Toast")
.bold()
.foregroundColor(.white)
.padding()
.background(Color.blue)
.cornerRadius(5.0)
}
.padding()
}
.toast(isPresented: $showToast, dismissAfter: 1.0) {
ToastView {
Text("This is a toast 🍞!")
}
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}

toastDemo

To find more information on a package, consulting the README file is always a good idea.

Conclusion

Adding packages to your project is a great way to use code from other developers. By using Swift Package Manager, you can find and add packages easily to your project. However, while adding third-party packages makes it possible to have more features in your application, it also makes you dependent on the maintainers of the package to fix bugs and update it as SwiftUI updates and changes. In general, the best practice is to only add a package when it’s either very well established or something that would be too difficult to write on your own.

It’s even possible to write your own packages and upload them to Github! For more information on that and on Swift Package Manager, check out the Apple documentation.