Quick Guide on Swift Package Manager for iOS
Learn how to use the Swift Package Manager with Xcode to add dependencies to your iOS project
17 Apr 2021 · 6 min read
It was exciting to hear Apple's announcement of the Swift Package Manager in 2015, entering the world of dependency managers like CocoaPods and Carthage.
However, it was not until the release of Xcode 11 in 2019 that Swift Package Manager became a first class citizen for iOS projects by finally being integrated into the rest of Apple’s developer tools.
In this article, you will learn the main concepts of Swift Package Manager, how to create your own Swift packages and how to share packages across different projects.
Let's get started.
Swift Packages are reusable components containing resources and code files that can also depend on other packages.
Compiling a package produces a module which can be shared as a public library using services like GitHub or be used as a locale dependency.
The manifest - a file called Package.swift - defines the name of the package, its contents and dependencies. It is placed in the root directory of each package.
Let's see how this manifest file looks like by creating a Swift Package.
Creating a Swift Package
One way to create a new Swift Package is by selecting File > New > Swift Package in Xcode. After entering the name, Xcode generates all necessary files and folders together with the manifest file Package.swift.
// swift-tools-version:5.3import PackageDescriptionlet package = Package(name: "SnowKit",// 1.products: [.library(name: "SnowKit",targets: ["SnowKit"]),],// 2.dependencies: ,// 3.targets: [.target(name: "SnowKit",dependencies: ),.testTarget(name: "SnowKitTests",dependencies: ["SnowKit"]),])
- In the products part, we define executables and libraries a package produces.
- In the dependencies part, we declare other packages that our package depends on.
- In the targets part, we declare targets our package consists of, e.g. one for our library and one for our tests.
Adding dependencies to the Swift Package
If our package depends on other packages, we can add them in the dependencies section of the manifest file.
dependencies: [.package(url: "https://github.com/Alamofire/Alamofire.git", .upToNextMajor(from: "5.2.0"))]
The example adds Alamofire - a third-party library that is available on GitHub - as a dependency. Now, we can use the library by importing it into our source code files.
We can also add local packages as dependencies.
dependencies: [.package(path: "../SunshineKit")]
This possibility is very useful, for example, when splitting a project into multiple libraries. This way, we don't have to worry about versioning and can edit the package's files directly within the project that’s using it.
Using Swift Packages in iOS Projects
Depending on the project requirements, there are different ways to use Swift Packages in an iOS project.
To add a remote library as a Swift Package, Xcode provides the menu action File > Swift Packages > Add Package Dependency. For a step-by-step guide, check out How to add public libraries as Swift Packages to an iOS project.
As already mentioned above, Swift Package Manager also provides the possibility to work with local packages, for example if we want to organise our code in a modular way. It basically works by dragging and dropping the local package into the Xcode project and adding it in the Frameworks, Libraries, and Embedded Content section. For more details, check out How to add a local Swift Package to an iOS project.
Articles with related topics
swift package manager
How to add local Swift Packages to an iOS project
Learn how to add local dependencies to your iOS project by using Swift Package Manager and Xcode
17 Apr 2021 · 2 min read
swift package manager
How to add public libraries as Swift Packages to an iOS project
Learn how to add, update and delete remote dependencies using the Swift Package Manager and Xcode
16 Apr 2021 · 3 min read
Step-by-step guide for localizing plurals in iOS
Learn how to localize plurals for multiple languages in iOS by using the Localizable.stringsdict file
01 Aug 2020 · 6 min read
Explore Apple's new Swift logging API for iOS 14
Learn how to log events and errors in your iOS app with the new API introduced at WWDC 2020
05 Jul 2020 · 4 min read
Quick guide on supporting Dark Mode on iOS
Improve your app's user experience by following this Dark Mode support guide
15 Oct 2019 · 4 min read