Integrate package dependencies to share code between projects, or leverage code written by other developers.
- Swift Package Manager
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.
Instead of adding a repository URL, you can search for a package on GitHub or GitHub Enterprise. Add your GitHub or GitHub Enterprise account in Xcode’s preferences, and a list of package repositories appears as you type. The following screenshot shows the list of repositories for the search term
When you added a source control account in Xcode’s preferences and the search field is empty, the list contains:
Decide on Package Requirements
Once you enter the package 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.
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.
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.
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 add it as a local package to your project. See Editing a Package Dependency as a Local Package to learn how you can override a package dependency with a local package to make edits.
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 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.