Swift Package Manager(SPM) Part 1
In this blog we will see how we can integrate public swift package in our fresh app, or app that uses cocoapod and how we can replace cocoapod dependency with SPM
Swift Package Manager(SPM)
- The Swift Package Manager is a tool for managing the distribution of Swift code
- It’s a dependency manager like cocoa-pod or Carthage
- It requires Xcode 11(Swift 5.1) or higher /
- It helps you to manage third-party libraries and split up your project into the different module or even distribute your own package to others
- SPM is part of the Swift toolchain
- Here I talked about the benefits of using the framework
Getting Started
Use SPM with Fresh Project
As shown in Figure 1 we create a fresh swift project, since it’s a fresh project and no legacy code everything will be easy, let’s say for this project I need alamofire
As shown in Figure 2, first we check whether alamofire
have SPM support or not, we just need to search Package.swift
file, if it exists then we are good to go
As shown in Figure 3, Go to File → Swift Packages → Add package Dependency
Copy URL alamofire repo URL. and paste here and Tap next
It will ask you version
details, you can specify the branch
or commit
. SPM package should follow semantic versioning.
Semantic Versioning
This is a wide-view standard that assigns specific semantic meaning to each component of a version number. Major.Minor.Patch like in this case 5.4.1
Major →
Let’s say if you are a alamofire
library developer and your next library changes will break thing in the client (the application using your SDK), Like you remove methods, rename the parameter in the methods, change method signature, or major behavior changes then you will release 6.0.0
Minor →
If your next library update has some additional method and new functionality is added in a backward-compatible manner then you will update the minor version 5.5.0
Patch →
backward-compatible bug fixes
As shown in Figure 6, we can select multiple options and by default, SPM will choose the latest version which is in Alamofire case 5.4.1. There are 4 rules you can specify
Up to next Major
→ It will automatically update from 5.4.1 < 6.0.0 means if alamofire
releases 5.4.2, 5.9.0 and 6.0.0
. Xcode will automatically update to 5.4.2, 5.9.0
and after that, it will not update on calling update SPM. It is recommended one
Up to next Minor
→ if alamofire
releases 5.4.2, 5.9.0 and 6.0.0
. Xcode will automatically update to 5.4.2
and after that, it will not update on calling update SPM since the minor version has changed
Range
→ You can specify custom ranges
Exact
→ You can lock the version as well by specifying the exact version
Note : You can opt-in for pre-release version identifier to your version as well. for example :
5.0.0-beta.1
→ Up To next version, If5.0.0-beta.6
release Xcode will automatically update it to , But when5.0.0
will release you need to update version
Hit Next now it , you can see Xcode fetching repo and cache locally and automatically reference that package to our target
As shown in Figure 9, we have added one package to our fresh iOS project
You can also update SPM version
Finally, we integrated Alamofire using SPM
Replace Cocoapods with SPM
As shown in Figure 12 we are using cocoapods for alamofire and we need to replace cocoa pod with SPM
As shown in Figure 13 first we need to remove pod . These are the step
- execute
pod deintegrate
in terminal (It will deintegrate the pods from your project first.) - Delete pod folder as shown in Figure 14
Install Alamofire from SPM and you are good to go
Delete SPM Dependencies
Tap on the negative sign, it will remove SPM from the project as a dependency
Use Cocoapods and SPM In Same Project
As shown in Figure 17 we added Alamofire dependency using SPM and Kingfisher from cocoa-pod. If you don’t know how to use cocoa pod please read this blog
Next
In the next blog we will see how we can create our own swift package and published it as private and how can we use it on some other project