14
Dependency Management with CocoaPods What is a Dependency? As a developer, you can think of yourself as a builder. As a builder, you will need the proper tools for your job. The tools you use may be bought, borrowed, or even hand crafted. The proper tools can make the development of an application quicker, simpler, and more efficient. In the world of iOS development, you can consider what’s known as dependencies to be some of your tools. Dependencies are also known as frameworks or sometimes libraries. Most likely, you have been using dependencies within your code and not even realizing it. If you see the words import Foundation” or “import UIKit” at the top of your Swift files, then you have been incorporating the Foundation and UIKit frameworks that were given to you by Apple. Not only does Apple want to help you succeed but also plenty of awesome developers (known as third parties) want to help. As such, these developers have curated libraries free for public use that are chock full of great tools. These third party libraries can help in interfacing with Core Data, getting user device information, caching images from the web, or just about any other process that a developer might use. What is Dependency Management? This ability to freely use other people’s tools as your own should help you to make a better application. Unfortunately, just having all of these tools at your fingertips isn’t automatically as magical as it sounds. When downloading these libraries from the previously mentioned awesome developers, their libraries can have bugs (which requires updating) and can even require other libraries to function. When these complications arise, a dependency manager becomes quite handy. A good dependency manager will download and update your requested libraries and make sure to take care of any other necessities. There are plenty of free and wonderful libraries that you can use. As of this writing there were almost 20,000 libraries compatible with CocoaPods, and luckily there is a great way to filter through all of these libraries on the CocoaPods website . Go ahead and try out the link. You can easily find libraries for interfacing with Core Data , helping with JSON , or tracking a device's location . As you can see, there are numerous libraries to choose from to help with your project. For example, AFNetworking is a popular library for networking needs; Google can be used for working with Google services, and Fakery can be used if you need to generate random bits of data like names, street numbers, or geographic coordinates.

Dependency Management with CocoaPods · CocoaPods was designed to use a text file called a Podfile to deliver dependencies to a project. CocoaPods finds the most compatible library

  • Upload
    others

  • View
    5

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Dependency Management with CocoaPods · CocoaPods was designed to use a text file called a Podfile to deliver dependencies to a project. CocoaPods finds the most compatible library

Dependency Management with CocoaPods What is a Dependency? As a developer, you can think of yourself as a builder. As a builder, you will need the proper tools for your job. The tools you use may be bought, borrowed, or even hand crafted. The proper tools can make the development of an application quicker, simpler, and more efficient. In the world of iOS development, you can consider what’s known as dependencies to be some of your tools. Dependencies are also known as frameworks or sometimes libraries. Most likely, you have been using dependencies within your code and not even realizing it. If you see the words “import Foundation” or “import UIKit” at the top of your Swift files, then you have been incorporating the Foundation and UIKit frameworks that were given to you by Apple. Not only does Apple want to help you succeed but also plenty of awesome developers (known as third parties) want to help. As such, these developers have curated libraries free for public use that are chock full of great tools. These third party libraries can help in interfacing with Core Data, getting user device information, caching images from the web, or just about any other process that a developer might use. What is Dependency Management? This ability to freely use other people’s tools as your own should help you to make a better application. Unfortunately, just having all of these tools at your fingertips isn’t automatically as magical as it sounds. When downloading these libraries from the previously mentioned awesome developers, their libraries can have bugs (which requires updating) and can even require other libraries to function. When these complications arise, a dependency manager becomes quite handy. A good dependency manager will download and update your requested libraries and make sure to take care of any other necessities. There are plenty of free and wonderful libraries that you can use. As of this writing there were almost 20,000 libraries compatible with CocoaPods, and luckily there is a great way to filter through all of these libraries on the CocoaPods website. Go ahead and try out the link. You can easily find libraries for interfacing with Core Data, helping with JSON, or tracking a device's location. As you can see, there are numerous libraries to choose from to help with your project. For example, AFNetworking is a popular library for networking needs; Google can be used for working with Google services, and Fakery can be used if you need to generate random bits of data like names, street numbers, or geographic coordinates.

Page 2: Dependency Management with CocoaPods · CocoaPods was designed to use a text file called a Podfile to deliver dependencies to a project. CocoaPods finds the most compatible library

Given so many libraries, CocoaPods isn’t the only dependency manager on the block that can assist you in your app development. For example, there is Carthage, Swift Package Manager, and Taylor just to name a few. Note: If the Carthage dependency manager piques your interest, we have some great Treehouse materials on that linked below in the “More Information” section. CocoaPods, the dependency manager you will be using in this tutorial, was launched in 2011 for supporting dependency management in Objective­C. Then in March of 2015, just five months after the release of Swift, CocoaPods published version 0.36 which supported Swift as well. CocoaPods was designed to use a text file called a Podfile to deliver dependencies to a project. CocoaPods finds the most compatible library versions for your use, downloads the source code for those libraries, and links the libraries into your Xcode workspace. The automation that CocoaPods brings to the table will help you to create and manage your dependencies easily so that you can spend more brain power on creating your application and less time developing your own tools. During this tutorial with CocoaPods, you will be

1. Installing CocoaPods on your machine. 2. Learning what a podfile is and how to use it. 3. Using the library “Hue” to create a nice gradient from a starter project. 4. Learning how to update a podfile.

Note: Throughout the tutorial, you will be asked to “paste” lines of text from the tutorial for your use. Many times these lines of text are precise and can only be used as they are written in this tutorial. Although not recommended for beginners, you are welcome to manually type the text instead of using the “paste” command. Installing CocoaPods CocoaPods is installed via the application Terminal*. To open Terminal, you will need to

1. Open a Finder window. 2. Select “Applications” under the “Favorites” folders on the left. 3. Select the “Utilities” folder. 4. Double click the “Terminal” icon to begin using Terminal.

*Terminal is a key tool that all developers should be familiar with. In fact, feel free to drag Terminal into your dock because we will be using Terminal throughout this tutorial. If you would like to know more about Terminal, we have a fantastic course detailing Terminal basics called Console Foundations, and if you don’t have time for a course, this cheat sheet of useful Terminal commands can also be great resource.

Page 3: Dependency Management with CocoaPods · CocoaPods was designed to use a text file called a Podfile to deliver dependencies to a project. CocoaPods finds the most compatible library

Once you have opened Terminal, paste the following command into Terminal followed by the “Enter” key: sudo gem install cocoapods

Note: When attempting to install CocoaPods, Terminal may produce an error saying “ ERROR: While executing gem (Errno::EPERM)Operation not permitted ­ /usr/bin/pod )” If this happens, use the following command sudo gem install ­n /usr/local/bin cocoapods/usr/bin/xcodeproj

to install CocoaPods instead. This command tells Terminal that you wish to install the dependency manager CocoaPods. Next, you may be prompted for your password, if so, go ahead and type in your user password and press “Enter”.

Once CocoaPods has finished installation, you should see a screen similar to the one below.

Page 4: Dependency Management with CocoaPods · CocoaPods was designed to use a text file called a Podfile to deliver dependencies to a project. CocoaPods finds the most compatible library

After the installation is complete, you will finish setting up CocoaPods by pasting the following command into Terminal pod setup ­­verbose

This command will copy the CocoaPods specification files into your local machine from GitHub. There are quite a few files to copy, and this can take some time depending on your internet connection. Although not required, the “­­verbose” option displays a few bits of information to let you know that Terminal is, in fact, installing CocoaPods during this (what could be) lengthy installation. Assuming that Terminal installed and setup CocoaPods properly, you will not have to go through that process again the next time that you want to harness the power of CocoaPods. CONGRATULATIONS on installing and setting up CocoaPods! Opening The Project First, go ahead and download the starter projectLink to zipped folder “VendingMachineWithCocoaPods.zip”. Once it is downloaded, open the folder “VendingMachineWithCocoaPods” and open the Xcode project “VendingMachine.xcodeproj”.* *If you would like to know more about the vending machine application, we have an excellent course entitled “Build a Vending Machine App in Swift 2.0” that teaches you error handling, multiple views, and UI Controls. Note: If you receive the warning, “ ‘Vending Machine’ is a project downloaded from the internet. Are you sure you want to open it?”, click “Open”.

Page 5: Dependency Management with CocoaPods · CocoaPods was designed to use a text file called a Podfile to deliver dependencies to a project. CocoaPods finds the most compatible library

Now, build and run the project to make sure everything is functioning properly. You should see the following vending machine app.

Next, open “ViewController.swift”, and scroll all the way down to the bottom of the file. There you will see the function “applyBackgroundGradient()”. This is where you will be making most of your updates to this project.

Page 6: Dependency Management with CocoaPods · CocoaPods was designed to use a text file called a Podfile to deliver dependencies to a project. CocoaPods finds the most compatible library

Navigating to the Project with Terminal The podfile is a text file that CocoaPods uses as a set of instructions. The podfile provides CocoaPods with key information about the libraries and their versions that you wish to use in your project. In order to create the podfile, you will need to use Terminal and navigate to the root directory of your project. The root directory is the folder that contains the Xcode project “VendingMachine.xcodeproj”.You can navigate to this folder in Terminal by entering the letters “cd“ followed by the space character (don’t press “Enter” just yet). Then, drag the folder containing the “VendingMachine” project from a Finder window into Terminal.

Upon dragging and dropping the project’s root folder into Terminal, you should see the words “VendingMachineWithCocoaPods” at the end of the Terminal line. For example, see the picture below.

Now, press “Enter”. It is critical that you are in the root directory of your project. To double check that you are, paste the following command into Terminal. ls The command “ls” tells Terminal to list the files in the current folder. You will find the “ls” command very helpful when navigating the file directories within Terminal. If you are in the root directory, you will see “VendingMachine.xcodeproj” listed.

Page 7: Dependency Management with CocoaPods · CocoaPods was designed to use a text file called a Podfile to deliver dependencies to a project. CocoaPods finds the most compatible library

If you do not see “VendingMachine.xcodeproj” listed, then go back to the start of this section and pay careful attention to the folder that you drag into Terminal. If you do see “VendingMachine.xcodeproj”, great job. Let’s go make a podfile! Making and Using a Podfile A podfile is a set of directions given to CocoaPods that lists the libraries and their version numbers that you wish to have in your project. The podfile is kept at the root directory of your project and can be created by simply pasting the following command into Terminal. pod init

Now, open the podfile for editing by pasting the following open command into Terminal open ­a Xcode Podfile

Once the podfile is opened, remove the “#” character from the line that reads # platform :ios, '9.0' Podfiles are written in the language Ruby, and in Ruby, the “#” character acts as a way to comment out a line of code. It’s analogous to “//” in Swift or Objective­C. Since we are using the 9.0 platform for our project, we should uncomment that line. Now, we need to add the following line under where the podfile reads “# Pods for VendingMachine”. pod ‘Hue’,’1.0’ If you are uncertain about the placement of the text, feel free to check the picture below with my additions highlighted in blue.

Page 8: Dependency Management with CocoaPods · CocoaPods was designed to use a text file called a Podfile to deliver dependencies to a project. CocoaPods finds the most compatible library

Now, save the podfile, go back to Terminal, and paste the following command into Terminal to install your libraries/pods. pod install

Press “Enter”, and you should see CocoaPods hard at work, analyzing, downloading, and installing your pods. Note: The install command used here “pod install” is far different from the previous install command “sudo gem install cocoapods”. When you first used the install command, you installed the program CocoaPods on your machine, but when you used the “pod install” command, you are instructing CocoaPods to use the provided podfile and incorporate the requested libraries into your project.

When CocoaPods installs a pod, it copies your project directory into a workspace and places your requested libraries/dependencies (now known as pods) into the new workspace. Also, you should no longer use “VendingMachine.xcodeproj” due to potential build errors, and as such, go ahead and close all Xcode windows and quit Xcode to ensure that you don’t accidently open the wrong project folder. Using Finder, go to the “VendingMachineWithCocoaPods” folder and open “VendingMachine.xcworkspace”.

Page 9: Dependency Management with CocoaPods · CocoaPods was designed to use a text file called a Podfile to deliver dependencies to a project. CocoaPods finds the most compatible library

Using your Pod With your workspace open (“VendingMachine.xcworkspace”), inspect the upper left corner of the workspace, and you should now see your “VendingMachine” project and the “Pods” project below it.

If you do not, go back and make sure that you opened “VendingMachine.xcworkspace” and not “VendingMachine.xcodeproj”. Assuming that all is right as rain, open up “ViewController.swift”, and at the top of the file, just below “import UIKit”, paste the words import Hue

This enables the view controller to use the Hue library of functions and methods. Now, build and run the application so that Xcode can find the Hue library and remove the Xcode error flag that may appear beside “import Hue” as can be seen above. If you do not have an error flag, that is okay as well, it just means that Xcode was smart enough to figure out where the Hue library was without you having to build and run the application. Now, scroll all the way to the bottom of “ViewController.swift” where the “applyBackgroundGradient()” function is located, and paste the following into the function // Create a color gradient from gray to darker gray let lightGray = UIColor.grayColor() let darkGray = UIColor.hex("#1E2428") let gradientLayer = [lightGray,darkGray].gradient() // Set the bounds of the gradient layer to dimensions of the backgroundview gradientLayer.frame = backgroundView.bounds; // Set the transition points for the gradient gradientLayer.locations = [0.0,0.5] // Insert the gradient layer into the view on the bottom layer (index 0) backgroundView.layer.insertSublayer(gradientLayer, atIndex: 0)

Assuming that your copy and paste skills are now perfected, the “applyBackgroundGradient()” function should now look like this:

Page 10: Dependency Management with CocoaPods · CocoaPods was designed to use a text file called a Podfile to deliver dependencies to a project. CocoaPods finds the most compatible library

Feel free to read the code comments as they do a good job of describing what each line of code does. Importing the Hue library into “ViewController.swift”, allows the view controller access to a few nice methods. First, we were able to declare a color using hexadecimal notation with the UIColor method “hex()”. Second, we were able to declare a color gradient simply by adding the “.gradient()” method to the array of two colors listed as “lightGray” and “darkGray”. With all of the updates implemented, build and run the application. You should see the gradient in the background that took a mere four lines to create… 5 if you count the import statement. Although this update was non­essential, there are plenty of pods that can be used to assist you with performing more critical and complex functionality.

Page 11: Dependency Management with CocoaPods · CocoaPods was designed to use a text file called a Podfile to deliver dependencies to a project. CocoaPods finds the most compatible library

Updating CocoaPods Given time, it is possible that your requested libraries will need to be updated so as to fix bugs or increase functionality. To apply these updates to the Hue library, the podfile must be modified. Currently, the podfile instructs CocoaPods to only use version “1.0” of the Hue library. Using the following steps, you will update the podfile so that CocoaPods will install the latest version of the Hue library ranging from version “1.1.0” up to but not including version “1.2.0”. In order to update the podfile, you will

1. Go to the file explorer within the Xcode workspace. 2. Click the enclosure triangle next to “Pods” to open up the Pods project. 3. Click “Podfile”.

VIOLA, you have opened the podfile.

Page 12: Dependency Management with CocoaPods · CocoaPods was designed to use a text file called a Podfile to deliver dependencies to a project. CocoaPods finds the most compatible library

Once inside the Podfile, replace the line pod ‘Hue’,’1.0’ with pod ‘Hue’,’~>1.1.0’

As previously stated, CocoaPods had initially only installed version “1.0” of Hue, but with the podfile update, CocoaPods will now use the latest version of Hue ranging from “1.1.0” up to but not including version “1.2.0”, and in case you are wondering, the “~>” characters are what place the bounds on the version update from “1.1.0” up to but not including version “1.2.0”. Now, navigate back to Terminal, and verify that you are still in the correct folder (the project root directory) by typing the command ls into Terminal and verifying that you are, in deed, in the same directory as “VendingMachine.xcodeproj”.

If Terminal has gone elsewhere, go back and look in the section entitled “Navigating to the Project with Terminal” for instructions on how to get back. (Or better yet, use this as an opportunity to try your hand at navigating the file structure from Terminal’s command line using the cheat sheet if necessary.) Assuming that Terminal is in the correct directory, go ahead and update CocoaPods by pasting the following command into Terminal

Page 13: Dependency Management with CocoaPods · CocoaPods was designed to use a text file called a Podfile to deliver dependencies to a project. CocoaPods finds the most compatible library

pod update

Press “Enter”. This will begin updating Hue from version “1.0” to the latest version of Hue compatible with “1.1”. As of this writing Hue was on version “1.1.0”.

Congratulations, you have now updated your pod, and you know the basics of CocoaPods! That’s awesome. Note: When adding new pods to your podfile, you must use the “pod install” command. If you wanted to add any new libraries/pods to your project you could insert them directly below the declaration for the “Hue” pod. For example, if you wanted to add the “Google” pod, your podfile would look like:

More Information Now that you have the power of CocoaPods in your hands, your life as a developer can be significantly easier. You can rely on the expertise of thousands of other generous developers who are passionate about sharing their tools. Who knows, maybe you will write your own library and share it with the world. Great things happen when people come together and share their knowledge. Feel free to practice with other tutorials, get more information on the podfile, watch a Treehouse course on CocoaPods, or look at sample of available libraries for CocoaPods on

Page 14: Dependency Management with CocoaPods · CocoaPods was designed to use a text file called a Podfile to deliver dependencies to a project. CocoaPods finds the most compatible library

GitHub. Lastly, if you enjoyed learning about CocoaPods, you might also have fun learning about the popular and ruthlessly simple dependency manager Carthage link to Treehouse Carthage Tutorial.