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
Since Xcode 11, managing dependencies with Swift Package Manager is directly integrated into Xcode.
This tip provides a quick start for adding third-party libraries to your iOS project as a Swift Package. For more in-depth concepts, check out this Guide on the Swift Package Manager for iOS.
Adding a remote Swift Package
To add a public library, e.g. Alamofire that is available on GitHub as a Swift Package requires the following steps:
Copy the documented Swift Package url of the library. (mostly documented in the README of the library if it supports SPM)
In Xcode, select File > Swift Packages > Add Package Dependency and enter the repository URL.
Decide how the package should be updated, for example up to the next major version, up to the next minor version, in a specified range or restricting it to the current version.
After confirming, Xcode fetches the library automatically. The fetched dependencies are stored in the project’s derived data directory. Xcode also adds it to the Package.resolved file to ensure every developer has the same dependency versions. The Package.resolved file is is stored at AppName.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved.
And that's it. Now, we can use the package functionality by importing it as a Swift module in our code files.
To add another Swift Package, we would repeat the same steps as described above.
Updating a Swift Package
To update a Swift Package, Xcode provides the menu action File > Swift Packages > Update to Latest Package Versions. At the time of writing, there is no way to update a single package.
Deleting a Swift Package
To delete a Swift Package, navigate the the project's Swift Packages tab.
Here, we can delete the library by selecting it and tapping the - button.
Like to support my work?
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
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
iOS developer guide on the main aspects of Core Data
Understand the main concepts of the Core Data framework.
19 Sep 2022 · 5 min read
Working with Xcode configuration files
Learn how to manage different iOS build environments with .xcconfig files.
21 Feb 2022 · 3 min read
Managing different iOS build configurations in Xcode
Learn the possibilities to set up environments like debug, staging and release for your iOS app.
14 Feb 2022 · 4 min read
Latest articles and tips
How to use the @available attribute in Swift
Learn to make Swift code only available for certain language versions or platforms.
06 Feb 2023 · 2 min read
How to import Objective-C types as optionals or non-optionals into Swift
Learn how to use nullability declarations and macros.
30 Jan 2023 · 3 min read
Developer guide on keychain for iOS
Learn how to securely store passwords with Apple's Keychain API.
23 Jan 2023 · 4 min read
Understanding basic animations in SwiftUI
Learn different options to animate SwiftUI views.
16 Jan 2023 · 4 min read
How to store a Swift struct in UserDefaults
Learn about the pitfalls when using Codable types in UserDefaults.
09 Jan 2023 · 4 min read