Sharing Code With Multiple Apps Using the Swift Package Manager

Sharing Code With Multiple Apps Using the Swift Package Manager

Here at Barstool Sports we currently have 5 iOS applications, Barstool Sports, Barstool Bets, Answer The Internet, One Bite, and an internal app for Barstool employees. That is a lot of apps to maintain. How is this maintenance simplified? All 5 of these apps share a code library that houses all of the networking, model, and persistence code, along with some shared UI code and some helper classes, which we call our Barstool Swift-SDK.

The iOS team here at Barstool Sports recently moved our dependency management away from Cocoapods and to the Swift Package Manager (SPM).

The 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.

Using SPM for dependency management is the future of iOS and all swift projects and making the switch was an easy decision. There are 2 options for importing code using SPM for an iOS app, pulling in the code from a remote git repository and using code from a local Swift package.

Disclamer - The following example was done with Xcode 13.1

How to setup a remote git repository

To begin, in Xcode select your project and the Package Dependencies tab. Make sure you have the project selected and not the target selected.

1

In the above image, no packages are currently added. Select the + button to add a Swift package. Then in the modal that appears, enter the URL for the git repository in the search bar in the top right. Here you'll be able to set rules about which version to use. You'll have the option to select an exact version number, or a range based on major and minor numbers and you will also be able to specify a git branch to use. After making these selections click the Add Package button.

2

Finally you will be presented with available packages and you select which ones you want to add.

3

The Package will now download and you will see it in Xcode like this.

4

How to setup a local Swift Package

The first few steps are the same as setting up a remote git repository.

  1. in Xcode select your project and the Package Dependencies tab.
  2. Select the + button to add a Swift package.

Now instead of adding a remote git repository URL you will select the Add Local... button.

5

You will then select your Swift Package from its directory on your system. A folder named Packages will be added to your project and your local Swift Package with all of its files will be available there and can be opened and edited.

6

Next in the project file, select the target you would like to add the package to and add it in the `Frameworks, Libraries, and Embedded Content section.

7

8

And finally, to use your Swift package you import it like any library.

9

Here is where it gets interesting. I like to add local Swift packages during development so I can easily modify package code while making changes to my app code. This makes it faster to make changes, without needing to update the package in a separate Xcode project, then commit code, push to a remote repo, wait for a pull request to be approved, and then pull the code down with SPM. But, this isn't a great approach when it comes time to build for the App Store or when working with a team. How so? When a teammate pulls a branch and builds, it should work no matter what the state of their local version of the Swift package is. And for the App Store, you don't want to accidentally build with the wrong branch of your local swift package. Now I just have to figure out how to have a local Swift Package for development and a remote repo setup when it comes time to archive and send to App Store Connect. Has anyone solved this problem? If you have let me know on Twitter.