Swift Package Manager (SwiftPM) vs Cocoapods
Table of Contents
Abstract
Today we will talk about dependency managers in iOS
and compare their advantages and disadvantages. Finally, we learn how to use those to add dependencies to your project.
Dependencies
When we build applications, we often use code from other developers writing to build more powerful applications more quickly. In your projects, we can make an easy animation to hide or show the button. But if you want to add a complex animation to your project that’s not built-in. Such as import lottie animation into your projects to load and renders animations and vectors exported in the JSON format. So,
Dependency managers in iOS development
There are three main dependency manager tools when developing iOS applications:
Swift Package Manager
The Swift Package Manager is a tool for managing the distribution of Swift code. It’s integrated with the Swift build system to automate the process of downloading, compiling, and linking dependencies. The Package Manager is included in Swift 3.0 and above. SwiftPM is a cross platform build system that is available for MacOS and Linux. Apple published an introduction video Getting to Know Swift Package Manager – Apple WWDC 2018 and SwiftPM has been documented.
Package structure
SwiftPM
created the following package structure. Source files are put in Sources
and test files are put in Tests
. Here is an example:
├── Package.swift
├── README.md
├── Sources
│ └── helloworld
│ └── main.swift
└── Tests
├── LinuxMain.swift
└── helloworldTests
├── XCTestManifests.swift
└── helloworldTests.swift
Swift Package
The most inportant file is call Package.swift
. This file use the PackageDescription library in the package manifest to list dependencies, configure localized resources, and set other configuration options.
/ swift-tools-version:4.2
// The swift-tools-version declares the minimum
// version of Swift required to build this package.
import Packagecontent
let package = Package(
name: "helloworld",
dependencies: [
// Dependencies declare other packages that this package depends on.
// .package(url: /* package url */, from: "1.0.0"),
],
targets: [
// Targets are the basic building blocks of a package.
// A target can define a module or a test suite.
// Targets can depend on other targets in this package,
// and on products in packages which this package depends on.
.target(
name: "helloworld",
dependencies: []),
.testTarget(
name: "helloworldTests",
dependencies: ["helloworld"]),
]
)
How to use
Package Resolution
With the package dependencies configured in the SwiftPM, Xcode
will download the packages and resolve dependencies at the build time. When you setting dependencies, you have some options:
- By version: Next version, up to next minor, …
- By branch
- By commit
Adding a Package to your Project
To add a package dependency to your Xcode project, select File > Swift Packages > Add Package Dependency and enter its repository URL. You can also navigate to your target’s General pane, and in the “Frameworks, Libraries, and Embedded Content” section, click the + button, select Add Other, and choose Add Package Dependency. For more information, you can view all at adding-package-dependencies-to-your-app -appledoc
Create a Module and Add a Local Package with SwiftPM
As you develop your app, organize its code in a modular way to keep it maintainable by creating Swift packages and using them as local packages.
-
Open your Xcode project and create a local Swift package as part of your project by selecting File > New > Swift Package.
-
Choose a project and a group, but don’t create a new Git repository if your app is already under version control; then create the new Swift package.
-
Move your code to the new package in the Project navigator and make any needed updates to the package manifest.
-
Select your project in the Project navigator, then select your app target and navigate to its General pane.
-
Click the + button in the “Frameworks, Libraries, and Embedded Content” section, select the local package’s library product, and add it as a dependency.
When you organize your app’s codebase using local packages, your Swift package’s code is part of the same repository as your app’s code. As you create more apps, consider moving local packages to their own Git repositories, and add them to your apps as a package dependency to reuse code across apps. You may even consider sharing them with other developers. For more information, see Publishing a Swift Package with Xcode.
You can check library support SwiftPM
at: https://swiftpackageregistry.com/
CocoaPods
CocoaPods is a dependency manager for Swift and Objective-C Cocoa projects. It has over 91 thousand libraries and is used in over 3 million apps. CocoaPods can help you scale your projects elegantly.
Installing Cocoapods
Cocoapods is built on top of Ruby, so you would need to install that in your system before using Cocoapods.
Then, you can install using this command: sudo gem install cocoapods
Using Cocoapods
Open Terminal
and go to your Xcode project directory. Afterthat, run command bellow:
pod init
The pod file is generated, so you can config the like like:
platform :ios, '8.0'
use_frameworks!
target '12Bay' do
pod 'TTBaseUIKit'
end
Carthage
It is the another dependency manager. Carthage has been written in swift language. It will not change anything in Xcode project. It builds framework binaries using xcodebuild
but user needs to integrate those libraries into project. To know more about Carthage click here
Supported Platforms
All Apple platforms, iOS, tvOS, watchOS, and macOS. Carthage command line tool works only on a Mac.
Advantages/Disadvantages
Advantages
SwiftPM | CocoaPods | Carthage |
---|---|---|
It’s Official Package Manager for Swift. It’s Future | Supports both Dynamic Frameworks and Static Libraries (since version 1.5.0) | Supports both Dynamic Frameworks and Static Libraries (since version 0.30.0). |
Automatically manage a dependency’s dependencies. | Automatically manage a dependency’s dependencies. | Automatically manage a dependency’s dependencies. |
Anyone inside the project will easily know what dependencies your app is using | Anyone can easily tell what dependencies your app is using | Anyone inside the project will easily know what dependencies your app is using. |
Automates the entire process of building, linking the dependency to targets | It’s easy to check if a new version of a dependency is available by using the command pod outdated . |
It’s easy to check if a new version of a dependency is available by using the carthage outdated command. |
It supports XCFrameworks in binary format. | Almost every framework supports CocoaPods. | |
It’s the new standard build by Apple to create Swift apps. Works on Linux. | Has an official Mac app to easily manage app dependencies. | |
Because it doesn’t need installations so, it saves time on CI unlike the other dependencies managers for example Carthage or Cocoa-pods. | Your project builds faster in comparison to CocoaPods as Carthage only builds the frameworks once |
Disadvantages
SwiftPM | CocoaPods | Carthage |
---|---|---|
At this point of time, SPM doesn’t support all third-party libraries | You will have to wait a long time the first time you install your dependencies. This will also happen on every pod update command when you want to update your dependencies. |
Not every framework supports Carthage. |
You have to follow a specific folder structure. | Every time you build your project, all your dependencies will also be built, which leads to slower build times. | Too many steps to add |
Conclusion
Oki, that was it. Through this post, I found it at this point we should use CocoaPods instead of Swift Package Manage. Because SwiftPM is still very young and not all libraries support it. But,
Swift Package Manage
is the future of dependency management for any Swift project
So, In the next post, we will talk about how to update iOS cocoapod library to support Swift Package Manager
. Thank for reading this post. If you enjoyed it, please share it with others and see you next post.
Posts in this Series
- How to Update Current Cocoapod Lib to Support SwiftPM
- Swift Package Manager (SwiftPM) vs Cocoapods
- TTBaseUIKit - Framework to Build UI Programmatically
- Natural Language Processing on Ios Application Implement Viper Clean Architecture in an Ios App
- Basic Ci Cd Gitlab Fastlane
- The VIPER Architecture
If you enjoy reading my articles and find them helpful, please support me. Your support will encourage me to create and share more content with you ^^
By me a coffee