Article

Adding Package Dependencies to Your App

Integrate package dependencies to share code between projects, or leverage code written by other developers.

Overview

Xcode comes with built-in support for source control accounts and makes it easy to leverage available Swift packages. Use Xcode to manage the versions of package dependencies and make sure your project has the most up-to-date code changes.

Add a Package Dependency

To add a package dependency to your Xcode project, select File > Swift Packages > Add Package Dependency and enter its repository URL. You can also navigate to your target’s General pane, and in the “Frameworks, Libraries, and Embedded Content” section, click the + button. In the “Choose frameworks and libraries to add” dialog, select Add Other, and choose Add Package Dependency.

Screenshot showing the dialogue for adding content to the "Frameworks, Libraries, and Embedded Content" with Add Package Dependency… selected.

To search for packages instead of adding their repository URL, configure a source control account in Xcode’s Preferences. As soon as you’ve configured a source control account, a list of repositories appears as you type.

Screenshot showing the dialogue to add a package dependency. The user has entered ExamplePackage in the search field.

Once you enter the dependency’s URL or pick a Swift package from the list of packages, choose one of three package requirements. Package requirements determine the allowed versions of the package dependency in your project and Xcode updates your package dependency based on the requirement that you choose.

Version

Decide whether your project accepts updates to a package dependency up to the next major version or up to the next minor version. To be more restrictive, select a specific version range or an exact version. Major versions tend to have more significant changes than minor versions, and may require you to modify your code when they update. The version rule requires Swift packages to conform to semantic versioning. To learn more about the semantic versioning standard, visit semver.org.

Selecting the version requirement is the recommended way to add a package dependency. It allows you to create a balance between restricting changes and obtaining improvements and features.

Branch

Select the name of the branch for your package dependency to follow. Use branch-based dependencies when you’re developing multiple packages in tandem and don’t want to publish versions of your package dependencies.

Commit

Select the commit hash for your package dependency to follow. Choosing this option isn’t recommended, and you should only use this option in exceptional cases. While pinning your package dependency to a specific commit ensures that the package dependency doesn’t change and your code remains stable, you don’t receive any updates. If you worry about the stability of a remote package, consider one of the more restrictive options of the version-based requirement.

Once you choose a package requirement, Xcode resolves and fetches the package dependency. Select the package’s products that you need, and add them to specific targets in your project.

In the Xcode sidebar, the Swift Package Dependencies section shows the newly added package. Click the disclosure triangle to view the contents of the package as it exists on your Mac.

Edit a Package Dependency

You can’t edit the content of your package dependencies directly. If you want to make changes to a package dependency’s content, you need to check out the Swift package from its Git remote separately. Once you’ve checked out the Swift package locally, drag it into your app’s Xcode project from Finder to add it as a local package. You can edit it as part of your project; you can fix bugs, or develop new features for the package dependency in tandem with your app.

You don’t have to remove your package dependency; the local package overrides the package dependency with the same name. When you’re done editing the local package, push your changes to its Git remote. Once the change has made it into the next release of the Swift package, remove your local package and update the package dependency to the latest version.

Coordinate Package Versions Across Your Team

When collaborating on a project, make sure everyone uses the same version of a package dependency. When you add a package dependency to a project, Xcode creates the Package.resolved file. It lists the specific Git commits to which each package dependency resolves. Commit this file in Git to ensure that everyone is using the same version of a package dependency.

Delete a Package Dependency

If you need to remove a package dependency from your Xcode project, navigate to the Swift Packages pane in the settings, then select the Swift package from the list of packages. Click the - button to remove the selected package dependency.

Screenshot showing the confirmation dialogue that appears when removing a package dependency.

See Also

Code Sharing

Creating a Swift Package with Xcode

Create a Swift package to promote modularity and code reuse across your apps.

class Package

The configuration of a Swift package.

Publishing a Swift Package with Xcode

Publish your Swift package privately, or share it globally with other developers.

Building Apps that Use Swift Packages in Continuous Integration Workflows

Prepare apps to consume package dependencies within an existing continuous integration setup.