Building an SDK for iOS in Swift

Nick Holbrook
The Startup
Published in
4 min readJun 3, 2020

Recently, I was working on a project that would entail clients configuring and integrating our service with their existing mobile applications. At first, I thought it would be a simple enough process that they would be able to figure it out themselves with just some simple documentation. As we added more functionality, however, the work on their side became increasingly more complex to a point where we realized this was no longer a workable solution.

We realized that by continuing with our initial hands-off approach, we would be opening up the door to countless unforeseen issues down the road and potentially a sub-par end user experience if out parters incorrectly configured the application.

This led us to the eventual decision that the best approach would to build our own SDK that we would then be able to distribute to client — drastically reducing the time and effort spent by their development team, as well as reducing the scope of errors that might pop up during the initial setup process.

This post will go through the process of setting up a simple iOS package, talk about some common pitfalls you might run into during the process, explore how to test locally, and finally publish to SPM (Swift Package Manager).

Creating a New Package

Xcode

Creating a new swift package with Xcode

Creating a new package from inside Xcode takes about 5 seconds. Navigate to File > New > Swift Package, then save the package wherever you want.

Command Line

Alternatively, you can create a package from the command line by running:

Configuring your Swift Package

Swift packages don’t use .xcproject or .xcworkspace but rely on their folder structure and use the Package.swift file for additional configuration. The following code listing shows a simple package manifest

The Code

At this point you’ll have a file called MediumSDK.swift (or whatever your package is called) under the Sources directory. This is where we’ll store the code that needs to be included in our SDK.

At this point, you would add whatever functionality you needed for your SDK here. For my project, I needed a custom preconfigured WKWebView that I could send to our partners. Here’s a stripped down version of something similar to what I ended up using.

You would then add this file under Sources/MediumSDK, or Sources/YourPackageNameHere. A couple things to note when adding a custom class like this:

  • Make sure your class (or protocol or function) is public or else your project that is importing it won’t be able to access it
  • Inside of Xcode, make sure to build the package before publishing to git. This will reveal errors that you otherwise wouldn't have noticed and will save you tons of debugging time
  • Test locally before pushing to git (I’ll go into how to do this next)

Deployment Options

Local Testing

Before publishing your package anywhere, you definitely want to make sure to integrate and test it with an Xcode project locally.

Doing this is pretty simple. First you’ll want to make sure your files are being tracked by git and you have the most recent changes committed. Once that’s taken care of, you’re ready to import the package into an Xcode project for testing.

Find and copy the absolute path to your package, then go to File > Swift Packages > Add Package Dependency…, and enter file://{path} as the url. At this point you can choose what branch, commit id, or version you want to use. Continue and it will automatically install and link your project as well as any dependencies.

If you make any changes to your code, make sure to commit them then update the package within Xcode. You can do this by navigating to File > Swift > Packages > Update To Latest Package Versions.

Publishing to SPM (Swift Package Manager)

Once you’re ready to publish to SPM, the process is almost as easy as it was to test locally. Simply push your local commits to GitHub (or any other code repo) and you can follow the process above just replacing the local filepath with the repo URL.

Publishing to CocoaPods

Another very popular package manager is CocoaPods. Publishing to CocoaPods is a bit more complicated than SPM, but if you’re looking to make it as easy as possible for other develops to integrate your package I would definitely recommend looking into it.

Heres a link to the documentation on the CocoaPods website if you’re interested:

For additional info, check out:

--

--

Nick Holbrook
The Startup

CEO of www.holbrookconsultingservices.com. Co-founder of www.speaqapp.com. Tech author, cloud-native architect and certified AWS specialist.