Unified Swift/SwiftUI Library Deployment

Shubham Vyas
Simform Engineering
6 min readAug 11, 2023

Publish your library to the open-source iOS community

As a developer, you may have used a few third-party libraries in your project and be familiar with its leading dependency managers, such as Cocoapods, SPM, and Carthage.

But have you ever considered creating and sharing your library across these platforms to benefit the iOS open-source community?

GIF by Tenor

As you read this blog, you will learn how to use the same repository and project to distribute a simple library to all major platforms, including Cocoapods, SPM, and Carthage. This allows you to manage all your libraries in one place and update the library on all platforms simultaneously.

Table of Contents

We will create a pod with Cocoapods and then add support for the other two platforms. I’ll create a demo using the SwiftUI library that returns a view with text capitalizing it.

Here’s a quick rundown of all the steps involved in creating a library:

  1. Create GitHub Repository
  2. Create a Pod
  3. Add source files to the pod and update the Podspec file
  4. Release Library to Cocoapods
  5. Integrate Swift Package Manager (SPM)
  6. Integrate for Carthage
GIF by Gifer

Step 1: Create GitHub Repository

To begin, establish a public repository for our cross-platform library. Ensure the repository is set as public

Prior to library creation, select a unique name. Check CocoaPods for name availability. The library name for this project is MultiPlatformLib, which is available.

Step 2: Create a Pod

A pod can be created in various ways, commonly by following official CocoaPods documentation. Quickly bootstrap an Xcode workspace for pod development with the following command.

pod lib create [pod name]

On execution, you’ll be asked questions about its language, platform, example, and testing.

We will skip the Testing part for the purpose of this non-testing blog. However, we’ll affirm creating an example project to demonstrate library usage.

I recommend you follow suit and include an example highlighting your library.

Next, access the Xcode project template for Cocoapods. Return to the terminal and execute these commands to configure your repository with both local and pod integration.

cd [pod name]
git remote add origin [your repository URL]
  • These commands will set up the GitHub repository with your initial project, as per the first command. With your repository added to Cocoapods, we can proceed to the initial commit.
  • Employ these commands to upload your empty project to GitHub, completing the initial commit.
git add -a 
git commit -m "initial commit"
git push -u origin master

Step 3: Add source files to pod and update Podspec file

Upon executing the command, open your Xcode template. The resulting project structure should appear.

Default Cocoapods Project Structure
Default new Cocoapods library project structure.
  • To add your source files to the library, locate ‘ReplaceMe.swift’ within Pods > Development Pods > [Pod Name] > ReplaceMe.Swift.
  • Create a group called ‘Sources’ (or your preferred name) and transfer your source files there.

Ensure that ‘Copy items if needed’ is checked, ‘Select ‘Create Groups’, opt for Target as Pod Name, and deselect Example and Tests target, if selected.

  • Click finish and then move the ReplaceMe.swift to Trash. Here I have added ‘CapitalizedText.swift’. You can add multiple files as per your library requirements.
Options and targets to select while adding source files
  • You might have to change the deployment target for all the Targets in Pods Section and the Example section as needed. Here, I have set deployment targets for iOS 13.0 as we create the SwiftUI library available on iOS 13.0 and above.
  • After adding files, rebuild the project. It will enable the usage of your library in your project’s example, Target.
  • Now, craft detailed examples illustrating optimal library usage for added value.
  • Move on to the crucial .podspec file in the PodSpec MetaData group. Fill in version, platform, remotes, and dependencies.
  • Provide concise short and comprehensive long descriptions for your library. These descriptions enhance discoverability.

Note: As we are creating a demo library, we won’t add descriptions.

  • Change the deployment target if needed, as I changed to 13.0.
  • Check the GitHub URLs for the homepage and source.
  • Update the source files’ location value to match your files’ location.
My MultiPlatformLib project structure and podspec
My MultiPlatformLib project structure and podspec

Step 4: Release the library in Cocoapods

To release the library, you have to create one release in GitHub with a tag named the same as the current version of the pod in Podspec. Our current version is 0.1.0.

Before publishing a library with Cocoapods, push your code to GitHub.

After creating the release, run the following command:

pod trunk register [Your Email]
pod lib lint --allow-warnings
pod trunk push [Pod Name].podspec

You’ll receive an email to verify a session from Cocoapods after the first command. Verify it to proceed with the next step.

The first command will register you to release the library. The second follow-up command will verify your .podspec file and its details, alarming you about any issues, if any. The third command will publish the library to the Cocoapods.

Your library is now successfully published and available via Cocoapods.

Errors may result from Podspec mistakes or missing releases

Step 5: Integrate Swift Package Manager (SPM)

To implement SPM compatibility, create a ‘Package.swift’ file with provided code, replacing pod details.

Move this file to your project’s root directory via Finder. Ensure your project’s structure mirrors the Finder view.

Package.swift
Package.swift file location

With the file in place, proceed to add support for SPM.

Just run the following git commands to add these changes to the GitHub repository and publish our library to SPM.

git add -a 
git commit -m "Added support for SPM"
git push -u origin master

Once the code is pushed, you can seamlessly add the package through SPM, releasing your library within the SPM ecosystem.

Step 6: Add support for Carthage

To proceed, construct the framework using Carthage. If the Terminal window is closed, reopen and navigate back to the project folder.

To release the library via Carthage, execute this command.

carthage build - no-skip-current
  • It will build the framework and create a Carthage folder in the root project folder.

With Carthage support added, apply these modifications to the GitHub repository. Your library is now accessible via Carthage.

GIF by GIPHY Engineering

Using our published Library

CocoaPods

  • Generate a podfile by running pod init command to your project root directory.
  • Add your library name to the generated podfile and run the command pod install to install the library into your project.
  • You will be able to import the library into your project after this.

SPM

  • Go to File > Add Package > And add the GitHub URL of your Package into the search bar, and you will be able to see your Library there.
  • Add this dependency to your project.
  • You will be able to import the library into your project after this.

Carthage

  • Generate a CartFile using the vim Cartfile command to your project root directory.
  • Add your library name to CartFile that was generated and run the command carthage update — platform iOS to install the library into your project.

Congratulations on publishing your library to the open-source iOS community with leading dependency managers!

Wrapping up

I hope you found this blog helpful and that it inspires you to share your favorite codes with other iOS developers!

Have fun coding!

Let us know if you would like to learn how to release an update for these dependency managers next (in the comment section).

Till then, follow Simform Engineering for more such insights.

--

--

Shubham Vyas
Simform Engineering

📱 Mobile App Developer | Crafting Digital Magic | UX Wizard | Sharing Insights on Code & Creativity 🚀