Creating an SPM Package with Xcode 15
Would you like to create a Swift package within minutes and share it on GitHub? If so, let’s get started then.
Apple provides developers in the iOS ecosystem with a broad application development and distribution platform. However, to facilitate and manage the application development process, it offers Swift packages as collections of reusable code or components that developers can integrate into their projects.
What is SPM (Swift Package Manager)?
In Apple’s own summary, the Swift Package Manager (SPM) is a tool that aims to facilitate sharing your code and reusing others’ code, managing source code distribution. SPM simplifies tasks such as compiling and linking Swift packages, managing dependencies, versioning, and distribution for you.
How can we create an SPM (Swift Package Manager) package with Xcode?
Create the Package
After opening Xcode, first select ‘File,’ then choose ‘New,’ and opt for ‘Package’.
Next, on the opened screen, proceed by selecting ‘Multiplatform’ and ‘Library’ options. Then, specify the name of your package, choose its directory, and continue.
Here, you’ll find a PackageDescription file, a Sources folder, and a Tests folder. Since I don’t need the testing part for my package, I’ll start by deleting the Tests folder. Of course, we also need to edit the PackageDescription file. Remove the .testTarget item mentioned here from the targets array.
Then, within the PackageDescription, I’m adding a new parameter after the ‘name’ parameter. With this parameter, I can specify the platforms my package will support and the specific versions for these platforms.
import PackageDescription
let package = Package(
name: "LinearBGPackageDemo",
platforms: [.iOS(.v16)],
products: [
.library(
name: "LinearBGPackageDemo",
targets: ["LinearBGPackageDemo"]),
],
targets: [
.target(
name: "LinearBGPackageDemo")
]
)
Here:
name
: The name of the package.platform
: A list of supported platforms with a specific deployment target.products
: A list of products that this package offers for customer use.targets
: A list of targets that are part of this package.
Additionally, when creating the package constant here, you can also specify other values like defaultLocalization
, pkgConfig
, providers
, dependencies
, swiftLanguageVersions
, cLanguageStandards
, and cxxLanguageStandards
.
I have a very short code for this. In this code, I first create a ViewModifier. With this modifier, I enable setting a background for the content. Then, I write a function that directly applies this modifier to the view by creating a view extension. I set the parameters at the beginning (even if the user does not enter any values, it will work).
Additionally, after preparing your page here, don’t forget to select an iOS device as the simulator if you encounter any errors.
For our example Swift package, this is sufficient. Moving forward, go to the directory of your project in the terminal and run git init
to include the .git file in your project. Then, enter the necessary commands to create a new repository and push your files to the remote.
Afterwards, go to your SwiftUI project and add our package to your project as a Package using the GitHub link.
I have uploaded the package I created to GitHub and added its URL to my Xcode project. https://github.com/hasanalisiseci/LinearBGPackageDemo
And then, by importing our package, we use the function we wrote on the view.
Successfully created our package and used it within our project.
As you can see, creating a package is this easy with Xcode and Swift. Until another article, see you! Happy coding! 🙌🏻