Migration to Swift Package Manager

Igor Anany
7glyphs
Published in
3 min readApr 28, 2020

--

We’ve been using CocoaPods as a dependency management solution for iOS development since 2013. At that time, it was like a breath of the fresh air as nothing was around to manage dependencies in the iOS development world. Sometime later Carthage was released, but we didn’t use it much as we couldn’t convince ourselves to move from CocoaPods to Carthage for a variety of reasons which are outside of the scope of this article.

In 2016 Apple released their own, native version of dependency management system called Swift Package Manager (aka SPM or SwiftPM) alongside with the release of Swift 3. We were delighted to get a native dependency management system as we have never been 100% happy with CocoaPods due to the following reasons:

  • It is very slow.
  • It modifies the project files and creates a Workspace file where all dependencies are added to the workspace.
  • Every time you build the project, all dependencies will also be built, which is overkill, in my opinion.
  • It’s been developed in Ruby, which requires additional setup to get it running.

Don’t get me wrong, it is working, and we kind of got used to it and all its issues. But having something natively supported by Xcode seems like an excellent option.

Of course, we couldn’t jump straight into SPM. It was too risky in the beginning as not many libraries were there. We’ve been watching how the community reacts to SPM and how fast libraries gets it’s support. We’ve been waiting until last week. Then we decided to give it a go and fully migrate one of our projects from CocoaPods to Swift Package Manager.

The project which was selected to test the migration is written in Swift 5 and has the following dependencies:

  • SwiftHEXColors
  • SnapKit
  • DateToolsSwift
  • FacebookCore
  • FacebookLogin
  • FacebookShare
  • KeychainSwift
  • Alamofire
  • Kingfisher
  • UIImageColorSwift
  • SwiftyTimer
  • UITextView+Placeholder
  • MarqueeLabel/Swift
  • GrowingTextView
  • Firebase/Core
  • Firebase/Messaging
  • Firebase/Firestore
  • Firebase/Auth
  • Firebase/Analytics
  • Firebase/Storage
  • SwiftEntryKit
  • SwiftyJSON
  • SwiftyShadow
  • NVActivityIndicatorView

We started the migration by removing CocoaPods completely from the project by running `pod deintegrate` command.

Then, we added libraries that are supported without any issues. Simply add them as packages to the project using SPM.

Those libraries are:

  • SwiftHEXColors
  • SnapKit
  • Facebook
  • KeychainSwift
  • Alamofire
  • Kingfisher
  • UITextView+Placeholder
  • MarqueeLabel
  • GrowingTextView
  • SwiftEntryKit
  • SwiftyJSON
  • NVActivityIndicatorView

Not bad.

Three libraries had SPM support, but we couldn’t add them due to the Swift tools version was below the supported one. We’ve checked those repos to see if we can submit a pull request to update Swift tools version, but those repositories seem to be dead, so we forked them, updated Swift tools version and used our forks instead.

The list of those libraries is below with the links to our forks:

  • SwiftyTimer (https://github.com/7glyphs/SwiftyTimer)
  • SwiftyShadow (https://github.com/7glyphs/SwiftyShadow)
  • DateTools (https://github.com/7glyphs/DateTools)

The process of updating those libraries was straightforward. Just update the Package.swift file and wrap all UIKit code used in the library with `#if os(iOS)` `#endif` block.

Two libraries listed below didn’t have SPM support at all:

  • UIImageColorSwift
  • Firebase

With UIImageColorSwift we simply added that support by doing the following:

  • Forked the repo.
  • Run `swift package init` inside the library folder.
  • Update Package.swift file to match the current library folder structure.
  • Wrap UIKit code of the library with `#if os(iOS)` `#endif` block.
  • Run `swift build` command to check if everything is building without any issues.
  • Push the code and add the library as a package in Xcode.

The fork of this library with SPM support is here:

https://github.com/7glyphs/image-color-swift

The only package that we couldn’t add to SPM was Firebase. Firebase does not support SPM yet. There are so many discussions on the internet, including Firebase repo, but Google still hasn’t added Firebase to SPM. Which I think is a big shame to Google.

The only option was to add Firebase manually to the project until it gets SPM support.

Everything is up and running, and we managed to do everything in one day. I think it is a great result, even having to manage Firebase manually.

--

--

Igor Anany
7glyphs

茶 ॐ 👨🏻‍💻 🎮🎧☕️🍵🧉🇳🇿 | CEO at 7 glyphs — the number one mobile and web development studio in New Zealand 🇳🇿 | iOS Developer | Father of 👦🏼 & 👧🏻