First step to CocoaPods with Xcode project - Install, Podfile, Add and install 3rd party libraries
Introduction
Everyone who start to develop a functional application of IOS in Xcode must need CocoaPods to help them out, otherwise it's really hard and time-wasting to write all things by self.
"CocoaPods is a dependency manager for Swift and Objective-C Cocoa projects. It has over eighteen thousand libraries and can help you scale your projects elegantly." Quote from CocoaPods
There are many useful libraries those are totally open-sourced on GitHub and already apply on a lot of very large projects. Some famous modules like: AFNetworking, MFSideMenu, SDWebImage, KLCPopup, SVProgressHUD... There are still many and many useful libraries that can help you enhance your projects. But now on this stage, let us get start on it with a very basic installation.
Install CocoaPods
CocoaPods is distributed as a ruby gem, so we only need to use one very simple command to install it.
Step1: Press "Command+Space" on your keyboard, to open a Spotlight Search
Step2 : Type in "Terminal" and press Enter
Step3 : A window of terminal will open. Copy&Paste / Type the command below, next of the "$" symbol, and press Enter.
sudo gem install cocoapods
Step4 : Wait for a while for the process. Enter the command below, and press Enter.
pod setup
Step5 : Finished!!!!
Until now, after few easy steps, we already installed the CocoaPods on computer. And we're going to create podfile to start our project.
Podfile
Podfile is for CocoaPods to manage project dependencies, the dependencies are specified in the file. To create podfile you need to start your Xcode project first, and create it in the same directory of the project (your_project_name.xcodeproj).
Step1 : Open App Store, search Xcode and install it, it will consume around 30 minutes.
Step2 : Trun on Spotlight and type "Xcode" to open Xcode
Step3 : Go to the left-top corner of screen, click "File -> New -> Project", or use short-cut "Shift + Command + N" to create a new project.
Step4 : Select "iOS -> Application -> Single View Aplication", press "Next"
Step5 : Fill in all fields and press "Next"
In "Language" you can select objective-c / swift as your main programming language of the project, it depends on your skills. For newbies I suggest to use Swift. (Obj-C vs Swift)
In "Device" choose iPhone if you're new to it.
Other leaves as default.
For reference (copied from Apple.com):
- The "product name" is the name of your app as it will appear to customers in the store and should be similar to the app name you enter later in iTunes Connect.
- The "organization name" is an attribute of the Xcode project and is used in boilerplate text throughout your project folder
- The "product name and organization identifier" you enter are concatenated to create the default bundle ID using reverse domain name service (reverse DNS) notation.
Step6 : Choose location to create your project, it can be at anywhere. Click "Create"
Now you created the project!!!! Let's move on to the main topic - create podfile!
Step1 : Go back to "Terminal", use command "cd" to move to the directory of your project
Format:
cd /PATH/your_project_file_name
For example:
cd /Desktop/myNewProject
or
cd /Document/Xcode/myFirstProj
Step2 : Enter the command below each by each
touch Podfile
open -a Xcode Podfile
We just created a podfile and open it with Xcode, do you see it? If YES, good!!! If NO, try agin the second command...
Add and Install 3rd Party Libraries
Podfile is readied! It's time to include the libraries we need into the project! Your "Terminal" should be linked to your project directory, and podfile already opened for edit by Xcode.
Step1 : Copy the following codes and Replace the podfile with the codes. Press "Command + S" to save it
target '<your_proj_name>' do
# Uncomment this line if you're using Swift or would like to use dynamic frameworks
# use_frameworks!
target '<your_proj_name>UITests' do
inherit! :search_paths
# Pods for testing
end
source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '8.1' #This is used for declare which is the minimum version of the platform you want to target in.
pod 'AFNetworking', '~> 2.5' # The second argument is for specify which version of the library you want to install into the project.
pod 'MFSideMenu'
pod 'SDWebImage'
pod 'KLCPopup'
pod 'SVProgressHUD'
end
Now, you already setup the dependencies of all five libraries I mentioned before.
Step4 : Go back to "Terminal", be aware it should be still in the directory of your project. If not, try to use "cd" command to move into it.
Step5 : Enter the following command, and press Enter
pod install
It will start to install the libraries those included in the podfile. It takes few minutes to install them. This command will also update the installed libraries, so don't worry how to update it, it's all easy.
Step6 : Close Xcode and type the following command in the "Terminal", change <YourProjectName> to your project's name, press Enter (You can also open your project in the Finder)
open -a Xcode Podfile
Step7 : Now it's ready to start your amazing project! You can install libraries as much as you want by modify the podfile.
Remember, you should open your project with .xcworkspace at the end of filename from now on. Don't open the project with .xcodeproj. Otherwise, your pod libraries will not be use in the project.
One more step, open your Xcode project, go to the header file of the ViewController(which filename end with .h), type the following command at the top to import the libraries you need to use in the ViewController.
Format:
#import "<target_library_name>.h"
Example:
#import "KLCPopup.h"
Conclusion
At this point, everything is setup to use the CocoaPods libraries in your project. Try and discover all the possibility it can deal with! Also, go check out my another article to follow the step to create project with Interface Builder!
I'm open and welcome to discuss and improve anything not enough and bad in this article. Please don't hesitate to leave your comments!