This workshop will be retired on May 31, 2020.
Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Let's spend a few minutes learning about Dependency Management in general and then we'll dive into installing, using and updating CocoaPods in a real iOS app.
Swift 3 Update
If you are following along using Xcode 8 and Swift 3 then please make sure to use a new version of the pod
Hue. This is what you will add to your
pod 'Hue','~> 2.0.1'
[MUSIC] 0:00 As a developer you can think of yourself as a builder. 0:04 Like any builder you'll need the proper tools for your job. 0:07 Those tools might be bought, borrowed, or even hand crafted. 0:10 The proper tools can make the development of an application quicker, simpler, and 0:13 more efficient. 0:17 By the way, there are lots of steps in this video. 0:18 So we've also provided a written version of the tutorial, 0:21 along with lots of helpful screenshots, and that, of course, is linked below. 0:24 In the world of IOS development, 0:29 you can consider what's known as dependencies to be some of your tools. 0:30 Dependencies are also known as frameworks or sometimes libraries. 0:35 Most likely, you've been using dependencies within your code and 0:38 not even realizing it. 0:41 If you see the words import foundation or import UI kit at the top of your swift 0:43 files, then you've been incorporating the foundation and 0:47 UI kit frameworks that were given to you by Apple. 0:49 Not only does Apple want to help you succeed, but there are plenty of other 0:52 awesome developers, known as third parties, who wanna help as well. 0:56 These developers have curated libraries free for 1:00 public use that are chock full of great tools. 1:03 These third party libraries can help in interfacing with core data, 1:06 getting user device information, caching images from the web, or 1:09 just about any other process that a developer might require. 1:12 This ability to freely use other people's tools as your own 1:16 should help you make a better application. 1:20 Unfortunately, just having all of these tools at your fingertips isn't 1:22 automatically as magical as it sounds. 1:26 When downloading these libraries from the previously mentioned awesome developers, 1:28 their libraries can have bugs, which require updating and 1:32 even require other libraries to function. 1:35 When these complications arise, a dependency manager becomes quite handy. 1:38 A good dependency manager will download and update your requested libraries and 1:42 make sure to take care of any other necessities. 1:46 There are plenty of free and wonderful libraries that you can use as of this 1:49 writing there were almost 20,000 libraries compatible with CocoaPods and 1:53 luckily there's a great way to filter through. 1:58 All of these libraries on the CocoaPod website. 2:00 Go ahead and try out the link below you'll find libraries for interfacing with core 2:02 data helping with JSON tracking of devices location and plenty more. 2:06 Given so many third party libraries, CocoaPods isn't the only dependency 2:11 manager on the block that can assist you in your app development. 2:15 For example, there's Carthage, Swift Package Manager and 2:18 Taylor, just to name a few. 2:21 By the way, if Carthage Dependency Manager peaks your interest, 2:22 we have some great Treehouse materials on that below. 2:26 CocoaPods, the dependency manager we'll be using in this tutorial, 2:30 was launched in 2011 for supporting dependency management in Objective-C. 2:33 Then in March of 2015, just five months after the release of Swift, 2:38 CocoaPods published version 0.36, which supported Swift as well. 2:42 CocoaPods was designed to use a text file called a POD file to deliver dependencies 2:46 to a project CocoaPods finds the most compatible library versions for 2:51 your use, downloads the source code for those libraries, and 2:56 links the libraries into your Xcode workspace. 2:59 The automation that CocoaPods brings to the table will help you create and 3:02 manage your dependencies easily, so that you can spend more brainpower 3:05 on creating your application and less time developing your own tools. 3:09 During this tutorial, you'll be installing CocoaPods on your machine, 3:13 learning what a podfile is and how to use it, using the library Hue to create 3:18 a nice gradient from a starter project and learning how to update a podfile. 3:23 All right, I think that's enough overview. 3:28 Let's get this party started. 3:30 So CocoaPods is installed via Terminal. 3:33 Now I keep Terminal down here in my dock. 3:35 But if you don't have it there, first of all you should put it there. 3:38 But the way you get to it is you're going to go to applications, utilities, 3:41 and then Terminal. 3:47 By the way if you haven't used terminal or haven't used it much, 3:50 working from the command line is something developers should be familiar with and 3:53 I've linked some courses on that below. 3:57 So once here, we can type the following command, sudo gem install cocoapods. 3:59 We're gonna hit enter and then we're going to hit our password here. 4:09 Now if for some reason that command of pseudo gem install CocoaPods didn't work, 4:17 there are some known errors for 4:22 this and I've linked to some solutions on that down below. 4:23 Okay now that we see twenty three gems installed here, 4:27 we know that CocoaPods has been installed. 4:31 Now we're gonna wanna set up 4:34 by typing Pod setup --verbose and we'll hit enter. 4:41 Now, this command will copy the CocoaPod specification files into your local 4:48 machine from get-up. 4:52 And there are quite a few files to copy and this can take some time 4:53 depending on your internet connection and that verbose that I typed up there. 4:56 That isn't required though it will give us some bits of information along the way so 5:01 that you know terminal is in fact installing CocoaPods during what might be 5:05 a lengthy installation depending on how long this takes to download. 5:10 Okay, now our setup is complete. 5:15 You won't have to repeat the steps we just performed every time you wanna update or 5:18 add pods provided CocoaPods is setup on that machine. 5:22 Okay, now let's put CocoaPods to use in an application. 5:26 We'll close Terminal for now and 5:30 let's open up the sample project we'll be using during this video. 5:32 By the way, the link for 5:36 that is down below, as is the link to the course where we build this app. 5:37 It's called Build a Vending Machine app in Swift 2.0 and that'll teach you error 5:42 handling, multiple views, some UI controls, lots of good stuff. 5:46 So let's find our file And let's fire up the app. 5:49 Make sure everything's working correctly. 5:59 Okay, in our sim, we see we've got a vending machine. 6:04 We can add some stuff to it. 6:09 We can purchase it. 6:11 Things seem to be working just fine here. 6:13 We'll close out of the sim and now let's head to the view controller. 6:16 And all the way down at the bottom, we're going to see 6:22 a function, apply background, gradient. 6:26 And this is where we're going to use the code from our pod. 6:31 Okay now before we do that we need to create our pod file and as you 6:37 might guess a pod file is a text file that CocoaPods uses as a set of instructions. 6:42 The pod file provides cocoa pods with the key information about the libraries and 6:48 their versions that we wanna use in our project. 6:51 Now in order to create our pod file. 6:54 We're going to use Terminal again. 6:57 I'm just gonna minimize this, open Terminal back up. 6:58 And the first thing that we want to do is we want to navigate in Terminal 7:03 to the folder that's containing our project. 7:07 So I'm going to head up one level and 7:10 you can actually just drag this over into Terminal. 7:13 I'm sorry. 7:16 First you need to type CD, Space. 7:19 And then we can drag it in, hit Return. 7:23 And now we're going to use the list command. 7:27 That's LS, just to verify that we're in the right place. 7:28 So it looks like we're in a place where there's a vending machine and 7:32 vending machine proj file. 7:35 Which is suspiciously familiar. 7:37 It looks just like that. 7:39 So, it looks like we're in the right place. 7:41 Now, if for some reason you're not seeing these things listed here, 7:43 perhaps you navigated to the wrong directory. 7:46 I'd back up a few steps and make sure that you're in the right place. 7:49 Since we are in the right place, 7:53 we're just gonna type pod init and 7:57 then open hyphen A, Xcode, pod file. 8:02 I missed an e on pod file, there we go and that opens our pod file for us. 8:08 Now, once this is open we're going to remove this pound sign here and 8:14 the pound sign is just commenting out this line. 8:19 This is written in Ruby and that's the symbol for a comment. 8:22 So we'll get rid of this, because we want this line. 8:26 And now, let's add the pod that we want to add to our project. 8:30 We're going to type that under here, under pods for vending machine. 8:34 And we're going to say, pod 'Hue', '1.0' 8:37 and of course that's going to be the version of the pod that we want. 8:43 All right, now that we've made our changes to the pod file we're going to save it, 8:50 head back to terminal and then type pod install. 8:54 OK great it looks like our one total Pod has been installed. 9:01 Now it's worth pointing out that the command we just typed in and 9:06 the process we just went through was installing the pod 9:10 that's different from what we did at first where we were installing CocoaPods. 9:13 Installing CocoaPods, you only have to do once 9:17 you're gonna install the individual Pods every time you wanna add a Pod. 9:20 Now when CocoaPods installs upon if it copies your project directory into a work 9:24 space and places you requested libraries or 9:29 dependencies which are known as Pods in CocoaPods into the new work space. 9:31 Also you should no longer use the vending machine Xcode project 9:36 because you might get build errors. 9:40 So instead we're gonna want to close all our X code windows. 9:42 Okay, they were already closed, that's fine. 9:49 We'll go back through finder and 9:53 instead you're gonna wanna open up the work space and you'll see why, 9:58 because now you see we've got pods listed in here, and we really want that. 10:03 That's what this whole thing is about. 10:09 If for some reason this isn't listed here, pods. 10:11 Head back up, follow the instructions really carefully and 10:13 make sure you are doing everything just right. 10:17 Okay, let's head back to our view controller 10:19 class that we checked out briefly before. 10:22 And the first thing we're gonna wanna do is import Hue so we can use it. 10:24 There we go. 10:31 We'll scroll all the way down. 10:36 And let's add our code in here. 10:40 So what we did right here is create a color gradient from a dark gray to 11:21 light gray. 11:25 Next what we're going to do is set the bounds of the gradient layer 11:27 to the dimensions of the background view. 11:30 And we can do that by saying gradientLayer.frame 11:32 = backgroundView.bounds. 11:39 Next we're going to set the transition points for the gradient. 11:45 GradientLayer.locations. 11:51 Let me make that 0.0 and 0.5. 11:58 And lastly we're going to insert the gradient layer into the view on the bottom 12:04 of the layer. 12:08 So that'd be index, zero. 12:09 So go backgroundVeiw.layer.insertSublayer. 12:12 And, we'll make that gradient layer. 12:22 And then make it above, oops, let's see, it's not index. 12:25 Okay, we've got everything we need. 12:34 Build it. 12:36 You'll notice this error should go away. 12:38 It would have actually gone away before we type this code. 12:40 It was just we didn't know that Hue was there yet 12:43 it doesn't kick in the import statement. 12:47 No there we go. 12:49 New and exciting. 12:50 Okay, now before we go, we should also talk about updating CocoaPods. 12:58 So given enough time it's possible that the libraries you use 13:03 will need to be updated to fix bugs, to increase functionality, who knows. 13:07 So to apply these updates to the Hue library you have to update the pod file. 13:12 So currently, the pod file which we'll open in one second, 13:17 instructs CocoaPods to only use version 1.0 of the Hue library. 13:20 We can head to the pod file like this or 13:26 through the work space if we want, 13:30 actually we can just go through here and 13:34 let's update this from a 1.0 to, 13:39 I don't know, let's say 1.1.0. 13:44 There we go. 13:48 And what that means is we want to update this to the latest version of Hue 13:53 ranging anywhere from one point one up to but not including one point two. 13:59 OK so that's what this notation means here that we're going to take anything 14:07 ranging from one point one up to one point two but not including one point two. 14:11 All right, now using the command line, 14:16 we're gonna make sure that we can update this again. 14:19 So let's open up Terminal and let's see 14:22 where we are to make sure in the right place, we use the list command again. 14:25 And we are in the right place, we have more stuff in this folder. 14:29 Because that was added during this process but we're actually in the same directory 14:32 where we were the first time so that's good and now that we're there 14:36 all we have to do is type you can probably guess what I'm gonna type Pod 14:41 update and there we go with updating our Pods this will take just a minute. 14:46 There we go so we installed Hue 1.1 it was 1.0 14:52 before that because that's what we Initially installed. 14:57 All right, so if you've been following along and you've gotten where I've gotten. 15:01 Congratulations you've updated your pod, you know the basics of CocoaPods. 15:04 It's worth noting that when adding new pods to your pod file 15:09 you must use the pod install command. 15:13 So if you wanted to add any new libraries to your project you could insert them 15:16 directly below into your pod file. 15:19 But then of course, you're gonna need to do the Pod install. 15:22 Not the whole CocoaPods install, just the pod install. 15:25 Now that you have the power of CocoaPods in your hands, 15:29 your life as a developer can be significantly easier. 15:31 You can rely on the expertise and work of thousands of other generous developers, 15:34 who are passionate about sharing their tools. 15:38 Who knows maybe you'll write your own library and share it with the world, great 15:40 things happen when people come together and share their knowledge, in fact it's 15:44 one of the things that makes programming such a unique and rapidly changing field. 15:47
You need to sign up for Treehouse in order to download course files.Sign up