方式一:
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/ShowTracker
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
Note: You shouldn’t use TextEdit to edit the pod file because TextEdit likes to replace standard quotes with more graphically appealing quotes. This can cause CocoaPods to get confused and display errors, so it’s best to just use Xcode or another programming text editor.
The default Podfile should look like this:
# 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.
This link has more information about the Podfile format and syntax. If you want to do more complicated stuff (like specifying “any version higher than” a specific version of a library), you should definitely check it out.
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 dependenciesDownloading dependenciesInstalling AFNetworking (2.2.1)Generating Pods projectIntegrating 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 – andShowTracker.xcworkspace.
VERY IMPORTANT!
From now on, as the command-line warning mentioned, you must always open the workspace (ShowTracker.xcworkspace) and not the project!
Close the Xcode project (if you had it open) and open ShowTracker.xcworkspace.
摘抄自:http://www.raywenderlich.com/64546/introduction-to-cocoapods-2
方式二:
Podfiles are used to tell Cocoapods which Pods, or open-source projects, to import.
To create your first Cocoapod, first use the cd command in Terminal to navigate to the the folder where you saved your Xcode project. Launch the Pico editor by entering the pico command in Terminal.
Once pico has opened, paste in the following lines:
platform :ios, '7.0'
pod 'Mantle'
pod 'LBBlurredImage'
pod 'TSMessages'
pod 'ReactiveCocoa'
This file does two things:
It tells Cocoapods which platform and which version you’re targeting. Here you’re targeting iOS 7.0.
It gives Cocoapods a list of all the projects you want to import and set up.
To save your file, press Control-O, name the file Podfile and hit Enter. Now press Control-X to exit Pico.
To install the four Pods noted in your Podfile, type the following into Terminal and hit Enter.