Swift Package Manager is available since 2016 but wasn’t that powerful yet. In September, a new version of Swift Package Manager will be released together with Xcode 11. This new version will finally allow you to replace CocoaPods.
Using Swift Package Manager, it is possible to manage third-party libraries as easily as CocoaPods. Maybe even easier, because there is no need to use the terminal anymore.
If some of the libraries that you currently use, are not supported, you can use both CocoaPods and Swift Package Manager together.
Migrate to Swift Package Manager
Migrating from CocoaPods is easy, but before you are able to migrate, all libraries you currently use should add support for Swift Package Manager. For libraries it’s easy to support the package manager by adding a Package.swift file with some metadata about the library.
It’s easy to figure out which libraries are supported. As soon as the Package.swift file exist, the support should be there. Also check the readme of each library to be sure the library is supported.
Remove CocoaPods from your project
It’s easy to remove CocoaPods from your project. Go to the Terminal and navigate to the root folder of your project, where you normally run ‘pod install’. Then, run the following command:
Using this command, CocoaPods will be removed from your project. No traces of CocoaPods will be left in project.
From now on, there are three files left in your project:
Go to the folder and remove those files manually.
After removing CocoaPods, you can use the .xcodeproj file again. Open your project, then go the menu bar and select File -> Swift Packages -> Add Package Dependency.
From here you are able to add new packages. To validate if a library is supported, simply check if the Package.swift file is in the library source.
You also may find in the readme if the library is supported. If the Package.swift file exist in the project, just copy the URL of the library and past the URL repository search bar. Then press next and Xcode will find the library.
Xcode will ask which version of the library must be used. If you want the most stable version, all settings are done. If you want another version, a branch or even a specific commit, you can set these rules on the package option screen.
After pressing next, the library will be fetched and added to your project. You’re done for now!
Update libraries using Swift Package Manager
Swift Package Manager supports semantic versioning. libraries will update automatically for minor (1.x update) and patch versions (1.1.x update). If a major version update is available, this can contain breaking changes, so you need to do this update manual.
Minor versions will only add additional functionalities and patch versions will only include bug fixes, so those updates must be without any breaking changes.
Manage packages in Xcode
Packages can be easily managed in Xcode in the organizer. Select the project that contains the libraries and go to the tab ‘Swift Packages’.
From here, it is possible to change the version of the library by doing a double click on the library. Also, it’s possible to remove a library or adding a new library to the project.
Swift Package Manager will soon replace CocoaPods for loading third-party libraries into your project.
If you want to learn more, a must watch is WWDC19 session 408 – Adopting Swift Packages in Xcode, which includes a demo and more details about the Package.swift file.