Logo for tanaschita.com

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:

  1. Copy the documented Swift Package url of the library. (mostly documented in the README of the library if it supports SPM)

  2. In Xcode, select File > Swift Packages > Add Package Dependency and enter the repository URL.

  3. 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.

  4. 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.

Steps for adding dependencies with Swift Package Manager
Steps for adding dependencies with Swift Package Manager

And that's it. Now, we can use the package functionality by importing it as a Swift module in our code files.

import Alamofire

To add another Swift Package, we would repeat the same steps as described above.

Sponsorship logo
Preparing for a technical iOS job interview
Check out my new book on preparing for a technical iOS job interview with over 200 questions & answers. Test your knowledge on iOS topics such as Swift & Objective-C, SwiftUI & UIKit, Combine, HTTP Networking, Authentication, Core Data, Concurrency with async/await, Security, Automated Testing and more.
LEARN MORE

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.

Deleting a Swift Package
Deleting a Swift Package

Here, we can delete the library by selecting it and tapping the - button.

Sponsorship logo
Preparing for a technical iOS job interview
Check out my new book on preparing for a technical iOS job interview with over 200 questions & answers. Test your knowledge on iOS topics such as Swift & Objective-C, SwiftUI & UIKit, Combine, HTTP Networking, Authentication, Core Data, Concurrency with async/await, Security, Automated Testing and more.
LEARN MORE

Newsletter

Image of a reading marmot
Subscribe

Like to support my work?

Say hi

Related tags

Articles with related topics

swift package manager

xcode

ios

swift

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

Latest articles and tips

© 2023 tanaschita.com

Privacy policy

Impressum