With the advent of Swift 3.0, Apple has released an official package manager called Swift Package Manager (AKA SPM or SwiftPM), that helps us manage dependencies. Show
But there are two other package managers out there: CocoaPods and Carthage. In this tutorial, you will learn about the pros and cons of each package manager, and how to use them to integrate frameworks into your codebase. You will be using a simple to-do app called 3, to learn about each integration. This app has a backend that allows users to synchronize all their to-do lists.You’ll use Xcode 10 and Swift 4.2 in this tutorial, but CocoaPods and Carthage also support Objective-C. Why should I use it?You might be asking yourself, “Can’t I just copy the source code inside my app project and use that?” Yes, you can, but what will you do if a critical update comes up? You will need to manually follow the repository release updates and keep checking for new versions. That doesn’t sound like fun, does it? Well, you’re in luck! By using a package manager, you can easily make sure that your code is always up to date. What is a package manager?Great question! A package manager is a tool that automates the process of installing, upgrading, configuring, and removing a software, or in this case, inside our app. But what does that really mean? Let's suppose you need to handle network requests inside your app, but you don't want to reinvent the wheel. You just want to use a robust, reliable, well crafted, and well-tested framework. With a package manager, you can easily manage dependencies inside your software, like Alamofire to help you better handle the network requests inside your to do app. CocoaPodsCocoaPods is a centralized dependency manager for Swift and Objective-C Cocoa projects. It is open-source and was built with Ruby by many volunteers and the open-source community. What does centralized mean? Well, CocoaPods is based on a main repository called Specs that hosts all the framework specifications. In order to make it available to others, package developers have to push new versions to this repository by using the 4 command line.CocoaPods has a wonderful public search feature available on its official website so you don't have to scan the web to find the right dependencies.
Supported PlatformsAll Apple platforms, iOS, tvOS, watchOS, and macOS. CocoaPods command line tool only works on a Mac. How To Use ItFirst, you need to install it on your Mac, open Terminal and type
Then, to integrate a dependency using CocoaPods, you have to create a 5 in your project root folder, like the following one:
Now open the Terminal App on your Mac and type
You have successfully integrated your dependency! Let's see what you’ve done:
After you run 6 on the Terminal, a 7 file will be created to let other developers, or even you, add the same version of the same dependencies to your app. If your project is under version control, usually this file is pushed to the repository.A folder called 8 was also created. Inside this folder, you will find everything is related to the CocoaPods integration. If your project is under version control, this file is usually pushed to the repository too, but this is optional.From now on, you must use the 9 file, instead of the 0 file to open your app's source code. This is needed in order to integrate your original app project and the CocoaPods project inside a single workspace.Go to your Swift file and type
You can now use Alamofire inside your file! Advantages
Disadvantages
List Of Useful Commands
CarthageCarthage is a decentralized dependency manager for Swift and Objective-C Cocoa projects. It is open-source and built with Swift by the open-source community. What does decentralized mean? Unlike CocoaPods, you don't have a main Specs repository. This reduces maintenance work and avoids any central points of failure, but project discovery is more difficult. For example, this means that checking for outdated dependencies means checking every dependency repository instead of a single centralized one. Supported PlatformsAll Apple platforms, iOS, tvOS, watchOS, and macOS. Carthage command line tool works only on a Mac. How To Use ItFirst, you need to install it on your Mac. There are two ways to do it:
Then, to integrate a dependency using Carthage, you have to create a 6 in your project root folder, like the following one:
Now open the Terminal App on your Mac and type
You have successfully integrated your dependency! Let's see what you have done:
That’s all! But a lot of things happened under the hood. Carthage created a 8 file to let other developers, or you, add the same version of the same dependencies to your app. If your project is under version control, you would normally push this file to the repository.It created a 9 folder in your project root folder. Inside this folder, it checked out every dependency declared in your 6. If your project is under version control, you would normally push this file to the repository as well.It also created a 1 folder inside it, where you will find a folder for every platform that your dependencies support. For example, for Alamofire, a framework that supports all Apple platforms, you will find iOS, Mac, tvOS, and watchOS folders.If you want to only build dependencies for a specified platform, use 2 command.Inside every platform folder, you will find all the frameworks that support that platform (you will also find other files and a 3 file, which is used for desymbolising crash logs).There are a few other steps we need to do (from ):
Go to your Swift file and type
You can now use Alamofire inside your file! Advantages
Disadvantages
List Of Useful Commands
Swift Package ManagerAlso known as SwiftPM or SPM, it's been included in Swift since version 3.0. From the official repository:
Swift Package Manager is also used to create backend apps by using frameworks like Vapor, Kitura or Perfect. Your app can only use corelibs or other dependencies based on top of them. Currently, there are:
Thanks to that, we will create a backend App instead of an iOS App. Supported PlatformsCurrently supports only macOS and Linux 14.04, 16.04, and 18.04. How To Use It
To be able to build, test, and use your project, you have to use a specific folder structure, like 5 for the source files and 6 for tests files. Inside the 5 folder, you have to create a 8 file. This will be the main App source code.Now let's create our app package description. Create a 9 file inside your project root folder.Inside it, we have to define all the characteristics of our app: 0Now open Terminal and type 1If everything went ok, you have successfully created your App! See what you have done:
After running 2 on the Terminal, a 3 file will be created to let other developers, or even you, add the same version of the same dependencies to your app. If your project is under version control, you would usually push this file to the repository.A 4 folder will be created for you, where you will find everything related to Swift Package Manager. You will find all the dependency repositories and your executable app.To execute your app, you can type 5.If you are on a Mac, you can even automatically generate the Xcode project with 6. From now on, you can use Xcode to build and maintain your app.Now we can use our dependency inside our app. Open 8 source file and type 2You can now use SwiftNIO inside your file! Advantages
Disadvantages
List Of Useful Commands
What Package Manager Should I Use?The below flowchart has only some simple questions to help you choose the best package manager that better fits your needs. SummaryWe learned how the package managers available for Apple platforms (even Linux!) differ, how they work, and how to use them. Now it’s up to you to choose the best package manager for your project. As a next step, why don't you try to create your own framework that supports all the package managers? How do I add a dependency to pod?From the CocoaPods repository
Specify the name of a Pod library in the pod() function. In the configuration block, you can specify the version of the library using the version parameter. To use the latest version of the library, you can just omit this parameter altogether. You can add dependencies on subspecs.
Is it possible to add a local dependency to .podspec file?It's not possible to set a local pod as a dependency, but it's possible to set a pod's source for a specific Podfile, which will work the same way.
How do I add a Podfile?Simple steps to install a pod file:. Open the terminal.. Command on the terminal: sudo gem install cocoapods.. Set your project path in the terminal.. Command: pod init.. Go to the pod file of your project and add the pod which you want to install.. Added in the pod file: pod 'AFNetworking', '~> 3.0.. Command: Pod install.. How install dependencies with CocoaPods?Open Terminal or iTerm and execute the gem install cocoapods command to install CocoaPods. Depending on the configuration of your machine, you need to prefix this command with the sudo command. I usually advise people to use a version manager for Ruby, such as RVM or rbenv. I have used both and have settled with rbenv.
|