9.2. Integrating CocoaPods into a Project
Let's create a new project App9 in Xcode. We will not write any code here; we will just use it to see how we can integrate cocoa pods into the project.
The setup part might look a little bit tricky, but it's really easy.
After you create the project, remember the directory you store the project into.
Open the directory using FInder (file browser on Mac). Browse to the directory (folder) where you saved the project. Do not get into the directory yet. So, you should be in the parent directory of the project directory now.
Open Terminal.
Type
cdand put a space. Do not press return yet.Drag and drop the project directory onto the Terminal. You will see the path to the directory is pasted on the Terminal after
cd.Press return. Now you should be in the project directory through Terminal.
Type
lsand press return. You will see the project files in the Terminal.

Now, type in
pod initcommand on Terminal.Open Finder again. You will see that there is a file called Podfile has been created.

Now open the Podfile with your favorite text editor.
You will see a line
# Pods for App9. You can add modules after the line.

Adding a CocoaPod module, Alamofire, to our project
As I said before, Alamofire is a widely used module for beginners to manage Internet data transmission. We will integrate Alamofire to App9.
Visit https://cocoapods.org.
Search for Alamofire. On top of the search results, you will see something like 'Alamofire 5.6.4.'
Click on the button to the right, 'Site.'
It will load the main project site in Github.

If you scroll down to Installation, you will see the instructions of how to install Alamofire using CocoaPods. Copy the line that says:
pod 'Alamofire'.Open the Podfile we have seen before.
Paste the line after
# Pods for App9.

Now go back to the Terminal again. Go to the project directory if you are not there (see above if you forgot how to).
Put the following command onto the Terminal:
pod install.You will see, depending on the modules you added to the Podfile, it will install them. When it's done installing the pods, in this case, it is Alamofire, your project can use this CocoaPod module.
Now, the final step is to be able to use the module. The following part is very important. We often forget to do that and the modules do not work in code.
Now, what you have to do is, close the Xcode project.
Open the project directory again.
Do not open the .xcodeproj file. Open the .xcworkspace file for this project.
Once you install a third-party module using CocoaPods, you must always use the workspace file (.xcworkspace) to open the project. Otherwise, you can't use the third-party modules.

Now, we have completed adding the 'Alamofire' module to our project using CocoaPods.
Reference Code
Last updated
Was this helpful?