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.
Articles with related topics
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
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
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