ROBBIE: Hey, dad.
DAD: Why hi, son.
Something bothering you?
Your get up and go seems to have got up and left.
ROBBIE: Eddie Clark was making fun of me
today because he's got CocoaPods in his Xcode project
and I don't have any.
What am I going to do?
DAD: Son, I think it's time we had a talk.
You're getting older now, and I'm
guessing you've started noticing the allure
of certain third-party libraries.
ROBBIE: AFNetworking has been kind of nice to me lately.
And oh gosh, I guess I do sort of like Google Analytics.
But I think I'd be too scared to talk to them.
DAD: Son, I won't lie.
Integrating with a third-party library, It isn't always easy.
You have to download frameworks and bundles
and import them into your Xcode project.
You probably need to link your binary
and add a new iOS linker flag or two.
And, well, heck, if this library depends
on any other frameworks, well, you're
going to have to do it all over again with those libraries.
It wasn't that funny.
Anyway, keeping these things up to date isn't easy either.
You have to go on onto the internet
and see if anything has changed, and repeat the whole process
all over again.
ROBBIE: Gosh, dad, that just sounds like a bunch of work.
DAD: Well, there are a lot of steps.
And if you're not careful, you're
going to end up making some pretty confusing
little mistakes, like this one.
Oh, instance 7fe5b1, I'm sorry I let you down.
ROBBIE: Gee, dad, this just sounds too scary.
Forget I even asked.
DAD: Well, not so fast, son.
You see, this is where CocoaPods comes in.
CocoaPods is a dependency manager written specifically
for adding third-party libraries to your iOS application.
ROBBIE: Really?
Well, how does that work?
DAD: Well, come with me.
I'll show you.
So now you see, Robbie.
Oh, good thing I have this on me.
The author of a library is going to write a Podspec file that
specifies everything you as a developer would need
to properly install it-- what files are required
and where they live, what iOS libraries
you might need to run it, if there
any pre or post-processing stops, and much more.
Now, you as a developer don't actually
need to care about most of this, although I do
think it's interesting.
You just need to know the name of this Podspec file,
and optionally, if there's a specific version
you want to be using.
See, you're going to create your own file,
called a Podfile, that lists the names of the Podspecs
that represent the libraries you want
to use with your application.
Then CocoaPods takes care of the rest.
We here at Google like it so much,
heck, you might even say we're cuckoo for CocoaPods.
ROBBIE: I don't get that reference.
DAD: You will in a few years.
We like it so much that Google is now making it
the official way we distribute a growing number of our libraries
to the world, including AdMob, Google Sign-in, and Google
Analytics.
ROBBIE: Wow.
So how do we get started?
DAD: Well, Robbie, perhaps the best way
to understand the beauty of CocoaPods
is to see it for yourself.
Here, we'll use my laptop.
So Robbie, if you've never used CocoaPods before,
the first step is to install it.
We're going to be using the RubyGems
tool to install CocoaPods.
Yeah, that's right.
We're going to be using a dependency manager to install
a dependency manager.
ROBBIE: It must go deeper, huh, dad?
DAD: Yeah, that movie hasn't been written yet, Robbie.
Anyway, go ahead and open up your favorite terminal program.
iTerm2, good choice.
Now, the first thing we want to do here
is make sure your Gems tool is up to date.
So go ahead and type sudo gem update - - system.
And you can enter your password there.
ROBBIE: OK.
Hunter two.
DAD: That's your password?
All right.
Well, depending on how old your version of Gems is,
it might need to update some things.
But once that's done, you can use Gem to install CocoaPods.
So you'll type sudo gem install cocoapods.
And that will install the CocoaPods tool.
This might take a while, so don't worry if it
looks like your system is hung.
ROBBIE: Gosh, this is taking a long time.
DAD: Yeah, it is.
Let's skip ahead here.
That's better.
Now, you're going to type one more command here.
You're going to type pod setup.
And this is going to download every single Podspec file,
over 8,000 of them, from the official CocoaPods repository
to your machine.
Now, don't worry.
These automatically get updated in the future,
so you only need to do this once.
All right.
Once that's done, we're ready to go.
Next, let's create a simple application in Xcode.
Just go ahead and create a simple single view application
here.
And now you can quit Xcode.
ROBBIE: Well, that was exciting.
DAD: Don't be a smart aleck.
Now we're going to navigate to the project you just created.
You want to be in the same directory as our Xcode project.
And here, we get to create our very own pod file.
Now, we could have CocoaPods create a slightly filled out
version for us by typing pod init,
or we can start with an empty file.
Let's do that because it's easier.
So now go ahead and edit this empty file.
And there are really only a couple lines of text
you're going to need to add here.
First, you need to tell CocaPods pods what platform you're
running on by typing platform: iOS, and then in quotes, "7.0."
And this says we're building on iOS
and 7.0 is our deployment target.
ROBBIE: Deployment target?
DAD: Right.
See, that's the minimum iOS target
that your app is going to support.
It's something every developer sets in their Xcode project,
and we need to make sure we list it here.
ROBBIE: Why?
DAD: Well, libraries might only work
on certain versions of iOS.
You need to tell CocoaPods the oldest version of iOS
your project is going to support so that it can point out
problems to you ahead of time.
Now we can list the podspecs we want to add.
Now what was that delightful little library
you're keeping your eye on?
ROBBIE: Aw shucks, it was AFNetworking.
DAD: Ah yes.
All right.
Well, let's add that one first.
Now, the thing is, we don't know for sure what AFNetworking's
podspec is named.
There's no real industry standard there.
Luckily, there are ways to figure this out.
One way is to use the pod search command from the command line.
So go ahead and type pod search AFNetworking.
ROBBIE: Whoa.
That's a lot of results.
DAD: Well yeah, but it looks like this first one is simply
called AFNetworking is the one that we need.
ROBBIE: Well, what are these others?
DAD: You mean things like AFNetworking-Synchronous?
These are mostly categories written
by other developers that extend the capabilities
of AFNetworking.
But we just want the main AFNetworking CocoaPod.
And as we can see here, you can get that
by adding the line pod afnetworking to our pod file.
ROBBIE: Oh, oh, can we add Google Analytics next?
Can we?
DAD: Yes, yes.
Calm down, son.
Now, for Google Analytics, I'll show you
how to search for it another way.
We can open up our browser to cocoapods.org
and in this big search bar at the top, just type in Google
Analytics.
Well, this looks like the one we want.
Now, it has this copy to clipboard icon here.
So you can just select that.
And now you can paste this line into your pod file as well.
ROBBIE: Say, wait a second.
What's that squiggly arrow mean?
DAD: Well, it gets complicated.
But you can think of it as saying let's start with version
3.10, but feel free to update it for me until you get to version
4.
ROBBIE: Sounds like a good topic for a follow-up video.
DAD: You always were a bright kid.
So let's go back to your command line and type pod install.
Here we go.
You're going to see CocoaPods think for a while.
Then it's going to go out onto the internet,
grab the libraries that we need, and install them.
We can wait a few more seconds.
And we're done.
ROBBIE: Say, what's this line about using xcworkspace file?
What's that?
DAD: Well, an xcworkspace file is basically
a collection of projects.
In this case, we have our original project,
which is our empty application.
And then we also have the Pods project,
which builds all of our CocoaPods as static libraries.
ROBBIE: But I don't get it.
Why can't we just use our original project?
DAD: Well, CocoaPods has altered our original project
to rely on the static libraries built by this other project.
So go ahead and open your original project by itself
and try and run it.
See?
Xcode is complaining that it doesn't
know what these libraries are, and we get these fun linker
errors.
ROBBIE: Yikes.
DAD: Well, it's not really too bad.
You just need to open the workspace in Xcode
and build from there.
Once you do that, it builds the static libraries and everything
works just fine.
ROBBIE: Oh, I see.
But if I tried to reopen my old project
from the welcome screen--
DAD: That's right.
That, along with any recent projects list,
it's probably a link to your old project
that no longer works on its own.
ROBBIE: So is that it?
Can I use my libraries now?
DAD: You betcha.
Let's go back to your view controller here.
You can import AFNetworking and GIA, which stands
for Google Analytics Igloos?
ROBBIE: I don't think that's it.
DAD: No All right.
And in our viewDidLoad method, you
can make calls on both of these libraries.
And look at that.
Everything builds and runs just fine.
We saved a whole lot of manual steps
we normally would have needed to include
both of these libraries.
And if you ever want to make sure you're running the latest
and greatest of any these pods, you
can simply type pod update from the command line.
CocoaPods will check and see if there are newer versions
of any these libraries.
If there are, it will update them for you.
Since we just installed everything a few minutes ago,
I would be surprised if this command actually did anything.
But I'm sure you can imagine how much work this would save you
on an actual project.
So there you go, son.
Everything you've always wanted to know about CocoaPods.
ROBBIE: Gee, thanks, dad.
DAD: Now, when that Eddie Clark starts teasing you again
at the school yard, what are you going to tell him?
ROBBIE: I'm going to tell him, hey Eddie, it was
me who set your car on fire.
DAD: You set his-- oh, Robert.