TribalScale
Published in

TribalScale

Delivering Our Library With Swift Package Manager

Written by: Daniel Carmo, Agile Software Engineer, TribalScale

In our last blog post, we created a Small but flexible Dependency Injection library for Swift. This library can be used in your application by directly copying the code to your project and then using it. Apple has its own method behind distributing packages that we are going to explore today.

Photo by Arnold Francisca on Unsplash

What is Swift Package Manager (SPM)?

Swift Package Manager is a tool for managing the distribution of Swift code. It’s integrated with the Swift build system to automate the process of downloading, compiling, and linking dependencies. Apple created this manager in order to help developers with the distribution of their code through services like github, gitlab, etc. As of Xcode 11, it has been fully integrated with Xcode to include packages in your project in iOS, macOS, watchOS, and tvOS applications.

Generating our Swift Package

In order to generate our package, we’re going to use Xcode to produce the package. In Xcode, click on File -> New -> Package.

From here you’ll be asked where to save and the name of the project. Select and name the project as you please. For our implementation we’ll call it FlexInject and save it to our SwiftPackages folder.

After clicking create, you’ll be presented with the basic outline to make our library into a Swift Package. The main file to note after the initial generation is the Package.swift. This file contains the definition for our Package including the name, products, dependencies, and targets. There are more definitions that can be added to this file that can be seen here https://docs.swift.org/package-manager/PackageDescription/index.html.

For our purposes, we are only going to add Quick and Nimble to our dependencies and add them to our testing target. This will allow us to create our Unit Tests for the library. Below is our final Package.swift.

Now that we have our Package.swift set up. We can copy over our code for the library that we created. It’s always a good idea as well to fill out the README of your project before proceeding with distribution.

Above is the final project setup. Now all the code is properly organized into our package, and is ready for distribution.

Distributing our Swift Package

In order to distribute our new library with SPM you can either distribute it privately to only your internal organization, or you can make the library public and distribute it to anyone. All that is needed to distribute the library is a remote repository with the package.swift in it. You can make the repository private to only distribute to members that have exclusive access to the git repository. Otherwise if you make the repository public then anyone will be able to download your package.

It is always a good practice to make tags for your library. This will allow users to download specific versions of the library and for you to mark important points in your code. SPM will also be able to pick up those tagged versions to be able to distribute to your users without explicitly setting the version anywhere in the package.swift.

Adding the package to your project

To add the package to your project, while in Xcode, go to your projects settings in Xcode and select Package Dependencies. This will list the packages that your project is currently using. Clicking on the “+” button will begin the process of adding a new package to your project.

A pop up will now be shown to you with any collections that you have added in the past and recently used packages as well. To add a new package, search for the URL to the repository in the top right corner.

Once you have found your package select it and adjust any version information that you would like to change in the right hand side of the menu. Once all the information is how you would like it, click the “Add Package…” button to add it to your project.

Conclusion

Swift Package Manager is a great and simple way to distribute your packages to a wide audience. It also adds a simple way for you to add dependencies that are used within your package. Distribution requires only a repository to hold your code and a `package.swift` to describe your package and its dependencies. To add the dependency to your project you use Xcode directly and add the package to your code base.

Have any questions about our development capabilities? Click here to chat with one of our experts.

Resources

Package Manager

Publishing a Swift Package with Xcode

Creating a Standalone Swift Package with Xcode

Daniel is an Agile Software Engineer with experience on a variety of platforms in both development and design. He also enjoys collaborative projects with clients, leading developer projects, and mentoring junior and intermediate developers on everything from code quality to programming knowledge. When he’s not coding, his hobbies include baking, running agility with his dog, raising chickens, and gaming.

TribalScale is a global innovation firm that helps enterprises adapt and thrive in the digital era. We transform teams and processes, build best-in-class digital products, and create disruptive startups. Learn more about us on our website. Connect with us on Twitter, LinkedIn & Facebook!

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
TribalScale Inc.

TribalScale Inc.

2K Followers

A digital innovation firm with a mission to right the future.