Say Goodbye to CocoaPods and Hello to A Swiftier Package Manager
Swift packages were introduced in XCode 8, Swift 3.0 in September 2016. The Swift Package Manager (SPM) helps users manage application dependencies and provides a means of distributing code.
Package source code is included in the distribution of packages like it is with CocoaPods. It is great for open source code because it allows developers to debug the packages they use. Also, the package does not need to include binaries for different target platforms like XCFrameworks do. But if you have proprietary source code in your library you can package it in an XCFramework and still publish it via SPM via the method described in Binary Targets section below.
Here are the steps to add an existing package to a project. You will need to find out the URL for the package which should be documented.
After completing the above steps you should be able to import and use the library. You will also be able to view the library source code. You will find the package in the project navigator tab under Package Dependencies. You will not, however, be able to edit the source code.
Editing Package Dependencies
If you want to change the package version or remove it from dependencies go to the Package Dependencies tab of project settings. Note the plus and minus icons for adding or removing package dependencies.
Double click on version rule of a package to bring up the below dialog which allows you to enter a version, branch name or commit.
If you select Version for the version rule you have some different options for selecting the package version.
Local Package Source Code Can Be Edited
In order to edit the source code in a package you will need access to the package repository. You can clone the repo or create it locally then add it.
Creating a New Package
To create a package of your own select File | New | Package or File | New | Project and then select Package. The Package.swift file includes information about the project such as the project name, products, dependencies and targets.
To test your package library you can create unit tests. And when you are ready I would also recommend you create a test app. Add the package as a local dependency. Note that you can’t have a package open in two different projects so close the package project before adding it to the test app.
When you want to make the package available for others to use push the code to a source code repository that clients have read access to. This needs to be a git repo, SVN and other version control systems are not currently supported. Then your package clients can use your package as described above where we added SwiftUIX package.
Adding a Package Dependency
If your package uses another package you need to add the package as a dependency in Package.swift. You will need the package repo URL and desired version. Add that to dependencies and add the package name in the target dependencies as well.
Binary Targets
You might have some proprietary code that you don’t want to be visible to clients of your package. If so you can create an XCFramework and add it to your package as a binary target. The below code from the Apple documentation shows how to add the framework from a remote repository as well as locally.
References