•6 months ago
As of Swift 5 and Xcode 11, Swift Package Manager supports the iOS, macOS, and tvOS build system. This support has also been greatly improved in Xcode 12 with the addition of non-source files, including asset catalogs, storyboards and nibs, core data models, and localization folders. It also supports binary frameworks. It's clear that it's now viable for most iOS projects, and many dependencies such as Stream Chat have implemented support for it.
A quick Twitter poll showed that 46.5% of iOS developers still use CocoaPods as their primary dependency manager. Swift Package Manager follows just behind with 42.1%.
It's clear that SPM, with its recent improvements and deep integration with the official iOS toolchain, will soon take over the other options. In this article, I'll present a process that you can follow to migrate your projects to Swift Package Manager fully.
To remove CocoaPods, you'll need the CocoaPods plugins
cocoapods-clean. To install them, run
sudo gem install cocoapods-deintegrate cocoapods-clean.
Before you remove CocoaPods, it's important to note each dependency in your project and their versions. In my case, I was only using Stream Chat in version 2.4.2.
Now, let's remove CocoaPods from your project. You should run the command
pod deintegrate in the same folder the Podfile is in.
That command removes the
Pods folder and removes the settings CocoaPods inserted in your
pod clean. That command will remove some leftover files like
Podfile.lock and the
.xcworkspace originally generated by CocoaPods.
It's possible that your
.xcodeproj still contains a reference to the
Pods folder. You can remove it by right-clicking the folder and selecting 'delete'. It will be gone instantly.
After you're done with these commands, you can put the last nail in CocoaPods's coffin with
rm Podfile. Thank you, CocoaPods, you've been very useful. 😢
Add Swift Package Manager
After you've mourned the loss of CocoaPods, let's replace it with SPM. Hopefully, you've taken notes of all the dependencies you had.
.xcodeproj, select the option "Add Package Dependency" in File > Swift Packages, and paste the URL of your dependency's git repository. For example: "https://github.com/getstream/stream-chat-swift".
After pressing next, Xcode will look for the repository and automatically select the latest version tagged. If this version is compatible with the one you were using, you can press next. Otherwise, type the version you were using. After that, Xcode will download the dependency.
A repository may contain multiple targets. If that's the case, you should select only the ones you use.
After you press finish, it's done! Though, you must repeat this process for each dependency you had.
It's possible that a dependency doesn't provide a
Package.swift. In that case, it can't be used with Swift Package Manager. However, you can clone the repository and add
Package.swift yourself or request the maintainers to do so. For example, see the commit adding SPM support to the Stream Chat iOS SDK.
I hope you were successful in transitioning your project to the future. It's only getting better from now on. If you need any help converting your project to Swift Package Manager, feel free to reach out to me on Twitter: @cardosodev.