How to migrate your swift project from CocoaPods to SwiftPM
- We’re using
Cocoapods
for several years, but Swift Package Manager (SPM) is new standard dependency manager which included in Swift 3.0 and above. If you are the one who wants to adoptSwiftPM
in your swift project, this is how to do it right. - Before removing
CocoaPods
from your project, make sure all your dependencies are compatible withSwiftPM
. - I’ve found one small tool
spmready
by Humi to check If all your dependencies are ready to migrate toSwiftPM
or not.
- If all your libraries are not compatible with
SPM
, then either you go ahead withCocoaPods
+SPM
together or just leave as it is. - If you are still want to integrate
SPM
for all compatible dependencies along withCocoaPods
then skip the removal process of CocoaPods.
Steps to Remove CocoaPods from your Project:
To remove CocoaPods from your project, you need to install cocoapods-deintegrate
and cocoapods-clean
gems.
Open your terminal application (Application
› Utilities
› Terminal.app)
and run the following commands:
$ sudo gem install cocoapods-deintegrate
$ sudo gem install cocoapods-clean
or you can combine both the commands as follow:
$ sudo gem install cocoapods-deintegrate cocoapods-clean
NOTE: Before removing CocoaPods from your project, its advisable to take backup of your project and most importantly, version of the each library you are currently using. You can find all the libraries and their version in the Podfile.lock file of your project’s root directory.
After successfully installing the gem, navigate to your project’s root directory in terminal and run following commands:
$ pod deintegrate // 1
$ pod clean // 2
$ rm Podfile // 3
- First command will remove the
Pods
folder from your project and all theCocoaPods
linking from.xcodeproj
file added during CocoaPods integration. - Second command will remove the
Podfile.lock
and the.xcworkspace
file, which was created during CocoaPods integration. - Final command will remove the
Podfile
from your project.
Now, open your project in Xcode using .xcodeproj
file and remove Pods
folder if it contains any reference.
That’s it. You have successfully deintegrated CocoaPods from your Project.
Integrate libraries using SwiftPM in your Project:
We have successfully removed CocoaPods from our project. Now, It’s time to install all our dependencies/libraries using Swift Package Manager.
As earlier said, before deintegrating CocoaPods from your Project make sure all the libraries that you are using is your project is available using with SwiftPM
.
- Open your project in
Xcode
using.xcodeproj
file. - Navigate to
File
›Swift Packages
›Add Package Dependency…
- Enter the URL of library’s git repository. (For example: https://github.com/onevcat/Kingfisher.git) and click Next button
- Now, Xcode will start looking for your repository and automatically select the latest version tagged. You can also choose the different version, branch or specific commit.
- Now click on the Next button and Xcode will download the selected dependency package.
- The repository may contain multiple package products. If that’s the case, you should select the ones you need and click on Finish button.
- That’s it. You have successfully integrated your dependency/library via
SPM
- Now, repeat this process for each and every dependency that you have.
Conclusion
- Swift Package Manager will soon replace CocoaPods for integrating third-party libraries into your project.
- Checkout WWDC19 session 408 — Adopting Swift Packages in Xcode, to learn more about Swift Package Manager.
I have created Poll on Twitter to see which dependency manager iOS developers are using now a days. Please select the appropriate option.
Questions?
Please feel free to comment below, if you have any questions.
If you like this article, feel free to share it with your friends and leave me a comment. Also, click on the 👏 clap button below to show how much you like the article.
Thanks for reading! 👨🏼💻
You can find me on:
Twitter | LinkedIn | GitHub | Medium | HackerRank | LeetCode | Stack Overflow