Migrate Your iOS Project From CocoaPods To Swift Package Manager

Jeroen L.
Jeroen L.
Published August 15, 2024 Updated November 18, 2020

On August 13, 2024, Orta posted some big news on the CocoaPods blog. CocoaPods is entering maintenance mode. This is big. Thirteen years ago, CocoaPods was developed and it was huge. All of a sudden iOS and mac developers aroudn the world could integrate third party dependencies with relative ease. CocoaPods singlehandedly changed the way software was developed in the Apple ecosystem.

AFNetworking quickly became the most used 3rd party dependency amongst iOS developers. This was back in the day of alloc, init and NSURLConnection. Especially NSURLConection code required mastery to get right, unless you used AFNetworking throug CocoaPods. (Any remember what draining the auto release pool was about?)

On December 3, 2015, Apple release Swift Package Manager onto the world. It was barebones and required a lot of work, but with the release of Swift 5 and Xcode 11 in 2019, Swift Package Manager recieved upport for the iOS, macOS, and tvOS build system. This support was improved significantly a year later with the release of Xcode 12 supporting non-source files, including asset catalogs, storyboards and nibs, core data models, and localization folders. It also supports binary frameworks. Since then the writing was on the wall. CocoaPods has been Sherlocked by Apple. Swift Package Manager is now viable for most iOS projects, and many dependencies such as Stream Chat have implemented support for it and moved to supporting Swift Package Manager first and CocoaPods as an afterthought.

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

It's clear that SPM, in its current state is the only option you should consider for managing your packages and dependencies in your Xcode projects. If you are still using CocoaPods, you should consider moving away from CocoaPods as soon as you can.

If you are using CocoaPods, the question is, how can you move away from it?

Removing 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 4.61.0.

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: "https://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

A repository may contain multiple targets. If that's the case, you should select only the ones you use and add them to your targets that need them.

Screenshot showing the package and target selection screen.

After you press Add Package again, 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 with one of Stream's SDKs to Swift Package Manager, please get in touch..

Ready to Increase App Engagement?
Integrate Stream’s real-time communication components today and watch your engagement rate grow overnight!
Contact Us Today!