In this tutorial, I will guide you through the process of adding third-party libraries and frameworks to your Xcode projects using Swift Package Manager. By leveraging the power of this tool, you can streamline your development workflow and enhance the functionality of your iOS apps.
But that’s not all! If you’re new to Swift Package Manager or want to learn more about its capabilities, make sure to check out my previous blog post on How to Create Your First Package with Swift Package Manager. This tutorial will introduce you to the fundamentals of creating and integrating Swift packages locally in Xcode, empowering you to share your code efficiently on GitHub.
Additionally, if you’re interested in authenticating GitHub directly from Xcode to save time and improve collaboration, take a look at my upcoming blog post on How to Integrate Xcode with GitHub Access Token. This post will demonstrate how to seamlessly integrate GitHub using access tokens, allowing for a smoother and more secure development experience.
I’m excited to share these valuable insights with you, so let’s dive right in and discover how Swift Package Manager can signifacantly improve your iOS development process!
What is the Swift Package Manager in Swift?
Swift Packages is a dependency management solution introduced by Apple to streamline the inclusion of external libraries and frameworks into your Xcode projects. They offer a standardized way of distributing Swift code and its dependencies, making it easier to manage third-party code and share your own code across different projects.
Swift Package Manager (SPM) vs CocoaPods vs Carthage
Swift Package Manager (SPM) is the official and most commonly used package manager for managing dependencies in Swift projects. However, there are a few alternative package managers and dependency management approaches you can consider:
CocoaPods is a popular dependency manager for iOS and macOS projects. It uses Ruby as its primary programming language and provides a centralized repository for Swift and Objective-C libraries. While it’s more commonly associated with Objective-C projects, CocoaPods has support for Swift as well.
- Carthage is another dependency manager for Swift and Objective-C. Unlike CocoaPods, Carthage focuses on decentralized dependency management. It doesn’t modify your Xcode project file but instead builds each dependency into a framework that you link to your project.
When choosing a package manager or dependency management approach for your Swift project, consider factors such as project size, community support, ease of use, and integration with your development workflow.
Prerequisites
Before we begin, make sure you have the following prerequisites in place:
- Xcode Installed: Ensure you have Xcode, Apple’s integrated development environment, installed on your Mac.
- Swift Package Manager: Swift Package Manager (SPM) comes bundled with Xcode. You don’t need to install it separately.
Adding a Swift Package to Your Xcode Project
You can add a package in Xcode by opening the File tab and selecting “Add Package Dependencies”:
Next you will be presented by the package manager dialog where you have to specify what package to add.
- In the top right corner enter the url where the package can be accessed
- Select the version you want to use. You should only allow automatic updates up to the next major version (which will include major changes and probably break your project.)
- Press “Add Package”
For this example, I am going to add 2 very popular packages which are:
- Firebase https://github.com/firebase/firebase-ios-sdk
- SwiftyJSON: https://github.com/SwiftyJSON/SwiftyJSON
Since I am using the Firebase SDK, this offers a lot of individual package products. Xcode will present another dialog where I can choose the packages. In the below example, I am adding Analytics features.
For SwiftJSON this is only one dependency, so you will not see this dialog.
Xcode will need some time to download all the resources for you. You might see some loading indicators next to your package list in the project navigator. Once Xcode finished, you should see something similar to this:
How to Delete a Package
You can delete a package in Xcode. You can find this option by:
- Click on your project’s name in the Project Navigator (usually on the left side of your Xcode window).
- Select the target for which you want to add the Swift Package.
- Navigate to the “General” tab.
- Scroll down to the “Frameworks, Libraries and Embedded Content” section
Select the package. In the above example image, I selected SwiftyJSON. You can then click on the minus button below, which will present a delete package dialog window.
Handling Problems with Swift Packages
Xcode has options for “Reset Package Caches” and “Resolve Package Versions” which fixes most problems for me. This changes the “SourcePackages” to the connected folders in DerivedData. Unfortunately, Xcode will download all packages which might take some time.
To deal with Package issues, I simply 😒 close/ reopen Xcode, clean the build folder, and press “Reset Package Caches”. What Xcode is doing internally is beyond me. You can read through this stack overflow thread for your amusement.
Using Features from Packages in Your Project Files
After you added your dependencies to Xcode, you can start using them in code. Here is an example where I use SwiftyJSON:
import SwiftJSON
class DataFetcher: ObservableObject {
var json: JSON = JSON.null
...
}
and here is how I would use Firebase Analytics to track when the user presses a button:
import SwiftUI
import FirebaseAnalytics
struct AnalyticsButtonView: View {
var body: some View {
Button("Press this ") {
// do something
// log event with Firebase
Analytics.logEvent("event_name", parameters: ["param": 1])
Analytics.setUserProperty("value", forName: "name")
Analytics.setUserID("user_id")
}
}
}
Conclusion
Now that you’ve successfully added a Swift Package to your Xcode project, you’re ready to leverage external libraries and frameworks to enhance your app’s functionality. Enjoy the benefits of modularity and maintainability that Swift Packages offer, and streamline your development process.
Further Reading