Monday, November 30, 2015

Introduction to CocoaPods Tutorial


CocoaPods Tutorial

Instead of downloading code from GitHub and copying it into your project (and thus making future updates difficult), you can let CocoaPods do it for you.
You will retrieve show information from a JSON feed provided by trakt. To simplify downloading, parsing, and showing results, you will use AFNetworking and a few other open-source libraries along the way.
To get started, you first need to install CocoaPods. CocoaPods runs on Ruby, yet that’s the only dependency it has. Fortunately, all recent versions of Mac OS X (since OS X 10.7 Lion) ship with Ruby already installed. So all you need to do is update RubyGems (just to make sure you have a recent version).
To do so, open Terminal and type the following command:
sudo gem update --system
Enter your password when requested. The Terminal output should look something like this:
Terminal Output
This update may take a little while, so be patient and give it a few minutes to complete. You can also expect some documentation in the Terminal window about the latest version; you can ignore this for now.
Next, you need to install CocoaPods. Type this command in Terminal to do so:
sudo gem install cocoapods
You may get this prompt during the install process:
rake's executable "rake" conflicts with /usr/bin/rake
Overwrite the executable? [yN]
If so, just enter y to continue. (This warning is raised because the rake gem is updated as part of the install process. You can safely ignore it.)
Lastly, enter this command in Terminal to complete the setup of CocoaPods:
pod setup
This process will likely take a while as this command clones the CocoaPods Specs repository into~/.cocoapods/ on your computer.
Great, you’re now setup to use CocoaPods!

Open Project that you created

Open Main.storyboard, and you will see just one view controller:

View Controller
Yeah, you read that right! It’s time to create your pod file.

Installing Your First Dependency

Open Terminal and navigate to the directory containing your ShowTracker project by using the cd command:
cd ~/Path/To/Folder/Containing/PiyushDemo
Next enter this command:
pod init
This will create a default Podfile for your project. The Podfile is where you define the dependencies your project relies on.
Type this command to open Podfile using Xcode for editing:
open -a Xcode Podfile
# Uncomment this line to define a global platform for your project
# platform :ios, "6.0"
 
target "ShowTracker" do
 
end
Replace # platform :ios, "6.0" with the following:
platform :ios, "7.0"
This tells CocoaPods that your project is targeting iOS 7.
Many libraries – AFNetworking included – have a minimum iOS version requirement. If you omit this line, CocoaPods assumes a default target version (currently iOS 4.3).
If you’ve only ever programmed in Objective-C, this may look a bit strange to you – that’s because the pod file is actually written in Ruby. You don’t need to know Ruby to use CocoaPods, but you should be aware that even minor text errors will typically cause CocoaPods to throw an error.
It’s finally time to add your first dependency using CocoaPods! Copy and paste the following into your pod file, right after target "ShowTracker" do:
pod 'AFNetworking', '2.2.1'
This tells CocoaPods that you want to include AFNetworking version 2.2.1 (the latest as of the writing of this tutorial) as a dependency for your project.
Save and close the pod file.
You now need to tell CocoaPods to “install” the dependencies for your project. Enter the following command in Terminal to do so (making sure that you’re still in the directory containing the ShowTracker project and Podfile):
pod install
You should see output similar to the following:
Analyzing dependencies
Downloading dependencies
Installing AFNetworking (2.2.1)
Generating Pods project
Integrating client project
It might also tell you something like this:
[!] From now on use `ShowTracker.xcworkspace`.
If you type ls now (or browse to the project folder using Finder), you’ll see that CocoaPods created a Pods folder – where it stores all dependencies – and ShowTracker.xcworkspace.

No comments:

Post a Comment