-
Notifications
You must be signed in to change notification settings - Fork 473
Using CocoaPods
This guide covers 1) setting-up CocoaPods, and 2) adding and installing Pods into your XCode project. By the end of it you'll be ready for guides on actually using the Pods you need.
- Basic level in XCode
- Novice level in Terminal
- Install CocoaPods by typing the following commands into Terminal
gem update --system
sudo gem install cocoapods
pod setup
- Just close your Terminal window and re-open it to complete setup!
Note: If your Terminal gets stuck on pod setup
, see FAQ
First you need set your Terminal's Present Working Directory to the folder containing your XCode Project.
- Type the characters "
cd
" + space - Drag the folder containing your
.xcodeproject
file to your terminal, then hit your return key
CocoaPods uses a text file named Podfile
to define your project's Pods. To add your Podfile:
- Type
pod init
into your terminal - Type
open -a XCode Podfile
and edit yourPodfile
in XCode
- First, you can delete everything in this file
- Add a row for each Pod you're installing, then save
pod 'MBProgressHUD'
pod 'AFNetworking', '~> 2.0'
Note: Your Pods will be different. These are two examples.
- Next have CocoaPods install typing the folowing into terminal
pod install
Note: If pod install
is taking more than 60 seconds, see FAQ
After your first pod install
, CocoaPods will create a new .xcworkspace
file for you, which includes has your CocoaPods as well. Only use your .xcworkspace
from now on.
- Close your
.xcproject
file - Open your new
.xcworkspace
file, which you can find in your project's folder
If you later need to change your Podfile
to bring-in new Pods, simply run pod install
again.
As of 2015 most popular CocoaPods are still written in Objective-C. To use these Pods in your Swift project, your project will need a Bridging-Header.
Create one automatically by adding any Objective-C file to your project.
- Go to
File -> New -> File ... -> iOS -> Source -> Objective-C File
and add a file with any name and any settings. - When asked Would you like to configure an Objective-C bridging header?, answer Yes.
- Edit the new file called
MySpecialProject-Bridging-Header.h
:
//
// Use this file to import your target's public headers that you would like to expose to Swift.
//
#import <AFNetworking/AFNetworking.h>
#import <MBProgressHUD/MBProgressHUD.h>
Note: Your Pods will be different. These are two examples.
You can trash the other file created in this step, but keep MySpecialProject-Bridging-Header.h
We're done! Now you can use your Pods– just follow the Pod maker's tutorials.
For example, now you can use your Pod in a ViewController.
import UIKit
class ViewController: UIViewController {
var progressHUD : MBProgressHUD!
...
}
Note: Your Pods will be different. This is one example.
If you experience absurdly long/ several minute waiting on pod setup
, or pod install
, you may try the following:
- Close your Terminal window, and open a new one
- Type the following commands
pod repo remove master
pod setup
- Now you can go back to step one and try again!
Answer: The Pod's developers will usually tell you what to write on their website.
- See Codepath's in-depth on CocoaPods, which includes links to major CocoaPods directories.