Hi, Alex here from rebeloper.com. Welcome back to the channel and welcome back to
this tutorial where you will learn how to use a cocoa pod in your project.
So hit that subscribe button if you want to build out this project with the help of
our awesome cocoapods. So let's dive into it and create your first cocoapod.
But before we do that let's just see on cocoapods.org what is really a cocoapod.
So I'm just going to read this out like cocoapod is a dependency manager
for swift and objective-c cocoa projects. It has over 68,000 libraries and is used
in over 3 million apps. Now what this means for you is that somebody wrote a
chunk of code library that does some awesome stuff and you can use it in your
own app for basically for free in many many cases. So today we are going to use
cocoapods, we are going to install as the web image. So we can download some
images from the web into our app, okay. And the first of all we want to install
cocoapods if you don't already have it in your computers so what you need to do
is just copy this sudo gem install cocoapod. So let me just copy that, open up
terminal and I have already worked here just disregard this let me just close
this and open up terminal again so we get a nice clean look, there we go.
So what you want to do is just type in sudo gem install cocoapods and hit return.
I already have cocoa pods installed so I won't bother with that. Now this may take
a few seconds or a few minutes depending on your internet connection, but after
you have installed cocoapods it's time to create our SwiftUI project. So let's
just open up Xcode here, there we go. Xcode and I am just going to create a
new Xcode project. Let me just resize this so you can see
it better, there we go. We are going to use a
single view application and I will just call it Awesome, make
sure that we are the user interface is set to SwiftUI. Let's click on next
here and I will just drop it here. I want create a git repository this is
just a simple example and you just we just have to follow along with the dummy
project, okay. Now that we do have everything set up
here it's kind of counterintuitive, but next up we want to close our Xcode
project because with cocoapods you need to create an XCWorkspace, an Xcode
workspace which has two projects. Your project that you have created and a pod
project that we are going to create through terminal. So let's just close this
right away and what you want to do is just navigate to your root folder of
your project and as you can see this is the root folder and now we need to
create a pot file where we are going to add our pots. I know if this is the first
time for you this kind of comes interesting to say the least but just
bear with me, let's just create our pod file and then we move on to adding the
pod for our as the web image and then we just download that library into our
Xcode workspace. So what we need to do is open up terminal and yeah it's it is
kind of scary to work in terminal but don't worry there are just two lines of
code that you need to learn and that the first one is pod init and the second
one will be pod install. And that's just it, okay. So with pod init we are going
to create a new pod file and you will see a pod file appear right here.
But first of all we need to navigate to that root folders. So typing in CD and the
space and just dragging and dropping the root folder into terminal and hitting
return, will take us there. You can see. Now let's just type in
pod init and let's see our pod file created. How awesome is that.
Now we need to open this up in text editor but I usually
avoid text editors because some comments may be overwritten with some strange
symbols so what I'd like to do is just drag and drop our pot file into Xcode.
Let just open that up inside Xcode. There we go. Now first thing that we should do
is set the platform to iOS 13 because yeah we are using SwiftUI. So that is
pretty safe and now we want to add our pods here. We are going to add one pod, so we
want to download images from the web and we are going to use SDWebImage.
We're going to use SDWebImage because there is a objective-c
basically UIKit SDWebImage and there is a SwiftUI version and that is
what we are going to use today. So let's just tap on there and just scroll a
little bit down and you will see the installation. We can use Swift package
manager cartridge but today we are going to have our cocoapods installed.
Okay! So let's copy and as out let's move back to Xcode here and just paste that
in there and we can safely close this because it's going to be safe. Let's move
on back to terminal and now type in pod install and I'm just going to present
our folder here because you will see quite some files created when I hit
return on pod install. So let's do that and as you can see the pods have been
created, analysing the dependencies, downloading them and installing them.
Now this might take a few seconds to a few minutes according to your internet
connection. So there we go. Two dependencies have been added and you
might see that SDWebImage has also been added. That is because the SwiftUI
version also needs the SDWebImage. So you kind of added one pod one line of
that but you have been downloading a lot more. If you basically take a look at the
other pods you will see that it does kind of the same. So SDWebImage
SwiftUI has a dependency of SDWebImage, okay and
now it's saying that I need to open up our Xcode workspace. Now what is this
Xcode workspace and where is it. Now if you look closely a new file has been
created this awesome cocoapods xcode workspace or XCWorkspace. Now from now
on we need to open our project with this Xcode workspace and not the other one.
If you open the other one you will not get access to the pods.
Okay! And finally we have our pod file dot lock here. What this does is locks
the version of our pod that we have created. If you want to update your
current lock file then you need to type in pod update or pod repo update.
It's out of the scope of the tutorial but go ahead and comment down below if you want
to find out more about this, okay. So we will obey the terminal and we are going
to open up our project with the in our Xcode workspace. So now let me just
resize this real quick and from now on we are going to work in and this project.
So workspace not the project because as you can see there are two projects.
One the project that we have created and the other one is basically the pods and if
you extend that you will see your pod file and you can edit your pod file
from Xcode from now on in here and you will see your pots those two
dependencies has been added there. Now that's good because you now know where
your pods are your extra third-party libraries but let's just take a look at
our awesome cocoapods file and let's just open up content view. Let just get rid
of our canvas and let's just hit command B so we can build our project. Maybe you
have to let Xcode know that okay we have added some new cocoapods. It's always a
good thing to build your project after you
have installed or updated your cocoapods. Okay! So now let's just go back to our
browser here and let's see what we have to do. So yeah there is a usage and I
will just copy and paste it in here and yeah basically we have URL string here
and it says onsuccess what we want to do when the success we have successfully
downloaded image and this web image is also resizable. We can have a placeholder
on it we can have an indicator so a transition it's so so nice to have when
we want to download images from our word web. I basically use it in almost all of
my projects. Well SwiftUI projects too. So I'm just copying this out and just
pasting this in here just so you know that is actually it is working. It you can
just get rid of this and let's hit command B, maybe we need to import SDWebImage.
So let's do that. Import SDWebImage SwiftUI and let's hit
command B now. Yes, it should be all okay. Build has succeeded and let's just
build and run this on our simulator and I will choose iPhone 11 Pro Max.
That's really nice. Let's just build that out. And basically this is just the end of
our tutorial. You all these know how to install a cocoapods I'm just showing
you that it really works and it is basically downloading an image.
So let's just wait for that to appear. It should be any minute now, there we go.
As you can see and it had that indicator and now we have fetched our image and it
is really, really awesome to have these third-party libraries. There are a lot more.
So go ahead and if you want to just check out all of
the libraries on cocoapods.org, you can just search there. But you know you can
just search github too and one of my favorites is firebase. So, yeah that is
with cocoapods installed on my projects also. Now you know how you can use any
type of cocoapod in your project so go ahead and make your best project ever
with the help of these awesome third-party libraries. Now I want you to
hit that notification bell because in my next project I will be talking about
optionals. Yes! That's right we are going to dive deep into optionals and how you
can unwrap them. In the meantime make sure to check out these two videos as well.
I talked a lot about SwiftUI, iOS development in general. So as usual I
will see you in the next one.