Sign in
Log inSign up

Migrate Your iOS Project From CocoaPods To Swift Package Manager

Matheus Cardoso's photo
Matheus Cardoso
·Nov 18, 2020·

5 min read

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.

Image shows CocoaPods logo with an arrow pointing to a Swift Package Manager logo

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

Twitter poll shows 46.5% of CocoaPods usage, 42.1% of Swift Package Manager, and 11.4% of Carthage

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.

Remove CocoaPods

To remove CocoaPods, you'll need the CocoaPods plugins cocoapods-deintegrate and 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.

Screen shows terminal with the pod deintegrate command executed and some lines of output

That command removes the Pods folder and removes the settings CocoaPods inserted in your .xcodeproj.

Now, run 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.

Screenshot shows Xcode with the Pods folder selected

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.

Open your .xcodeproj, select the option "Add Package Dependency" in File > Swift Packages, and paste the URL of your dependency's git repository. For example: "github.com/getstream/stream-chat-swift".

Screenshot shows Xcode with the Add Package Dependency dialog opened and Stream Chat iOS SDK GitHub URL in the input field

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.

Screenshot shows an Xcode screen selecting a dependency version and an Xcode screen downloading that dependency

A repository may contain multiple targets. If that's the case, you should select only the ones you use.

Screenshot shows an Xcode screen with dependency targets to be selected

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.

Conclusion

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.