Your SlideShare is downloading. ×
0
Using CocoaPods
Library Dependency Management for Xcode

with Jeffrey Sambells!
http://JeffreySambells.com
Why CocoaPods?
Build dependencies
Error-prone

+
Difficult upgrade path

Cluttered project

Link binary with libraries

=
Header search pa...
Automatic add/remove

Automatic build configuration

Project is always shippable!
Discoverability

+

+

Large ecosystem

=...
Installation…
$ sudo gem update -- system
$ sudo gem install cocoapods
$ pod setup
Using CocoaPods
in Xcode
Cocoapods Workflow
1. Create your Podfile
2. Search for and add your dependencies
3. Run pod command
4. Open your new .xcwor...
Creating the Podfile
$ cd /path/to/project/folder
$ touch Podfile
Simple
pod 'AFNetworking'
pod 'ObjectiveSugar', '~> 0.5'
Advanced
platform :ios, '7.0'
inhibit_all_warnings!
!
workspace 'Example.xcworkspace'
!
pod 'AFNetworking', :git => 'https...
Install/Update into Project
$ cd /path/to/project/folder
$ pod

Hint: use pod --verbose if you want more info.
The Podfile and
Dependencies
General Settings
Specify a platform and minimum version

platform :ios, '6.1'
!

Optionally specify project and workspace ...
Dependencies (Pods)
Specify pod name and other options as
necessary:


•




pod ‘PodName'

Some pods use sub specs to gro...
Pod Versioning x.x.x
•

References a specific git tag using a semantic version.

•

Specify no version, a specific version, ...
Logical Versions
•

'> 0.1' Any version higher than 0.1

•

'>= 0.1' Version 0.1 and any higher version

•

'< 0.1' Any ve...
Optimistic Versions
•

'~> 0.1.2' Version 0.1.2 and the versions up to
0.2, not including 0.2 and higher

•

'~> 0.1' Vers...
Pod Sources
Pods load information from a podspec file.

The podspec files are located in…
•

the shared public spec repo at ...
Public Shared Specs
you only need to specify the name:


•

pod 'AFNetworking'

you can optionally specify a specific branc...
Local Podspecs
Load a podspec from your local machine
using :path






pod ‘MyAwesomePod’, path: => ‘../path/
MyAwesomePo...
Private Podspecs
Load a podspec from a private git repository using
:git (simiar to public specs)





pod ‘MyAwesomePod’,...
Bonus
Code doesn’t have a pod? no problem. Make your
own pod spec for it and use that:
!
pod 'ExampleKit', :podspec => !'h...
Targets
Targets allow you to specify which Xcode target
have which pod dependencies.
Pod dependencies are applied to 

ind...
Targets
Use link_with to specify multiple targets for all
pod dependencies:



pod ‘PodName’
link_with [‘MyApp’,’MyAppDemo...
Targets
Or use target :targetname to specify targets
individually:
pod ‘PodName’
!
target :test, :exclusive => true do
pod...
Hooks
pre_install

Make any changes to the Pods after they have
been downloaded but before they are installed.
!
!

pre_in...
Hooks
post_install

Make any changes to the generated Pods project,
or any other tasks you might want to perform.
!

post_...
Pod Best Practices
•

Check the source, documentation and upkeep
of a pod! Don’t blindly use them.

•

Include pods in sou...
DEMO TIME!
Thanks!
More Info"
cocoapods.org
JeffreySambells.com
@iamamused
Upcoming SlideShare
Loading in...5
×

Using Cocoapods

1,027

Published on

A quick introduction to using Cocoapods in an Xcode project.

Demo Notes (in markdown):

## Using Cocoapods Demo
1. Create a new Single View project in Xcode called **MyProject**.
1. Create **Podfile** in project root.
1. Search [Cocoapods.org](http://Cocoapods.org) for a pod ([AFNetworking](http://cocoapods.org/?q=afnet)).
1. Point out features of web site.
1. Use clipboard icon to copy/paste AFNetworking.
1. Run `pod` in terminal (installation).
1. point out errors/warnings.
1. point out and explain loc file (bbedit **Podfile.lock**).
1. point out installed dependencies (if any).
1. point out **Pods.xcconfig**.
1. change the pod file (Remove AFNetworking and add [SVProgressHud](http://cocoapods.org/?q=svP)).
1. run `pod` again to see removal and updates.
1. Open **MyProject.xcworkspace**
1. Add a SVProgressHud call on **ViewDidAppear** of primary view controller.

- (void)viewDidAppear:(BOOL)animated {
[SVProgressHUD showSuccessWithStatus:@"It Worked!"];
}

12. Build and run the app.

<!--BREAK-->

## Creating Podspec Demo

1. Open and demonstrate **AwesomeProject**.
1. Decide to create a pod for **UIViewController+Alerts** category.
1. Show pod creation help in terminal: **`pod lib`** (note options).
1. Create a library on the ~/Desktop **`pod lib create iOSKW`**.
1. Explain the folder structure.
1. Copy in UIViewController+Alerts.h & .m files into **Classes/ios**.
1. run **`pod lib lint`**.
1. note warnings and errors.
1. Edit `bbedit iOSKW.podspec` as follows:

Pod::Spec.new do |s|
s.name = "iOSKW"
s.version = "0.1.0"
s.summary = "UIViewController Categories."
s.homepage = "http://example.com"
s.license = 'MIT'
s.author = { "jsambells" => "jeffrey@speakfeel.ca" }
s.source = { :git => ".", :tag => s.version.to_s }
s.platform = :ios, '7.0'
s.requires_arc = true
s.source_files = 'Classes/ios/*.{h,m}'
s.frameworks = 'UIKit'
end

1. Run `pod lib lint`.
1. Commit to local git repo.
1. Tag local git as **0.1.0** to match Podspec.
1. Re-run `pod lib lint` to verify
1. Create demo app called **Demo** on desktop and copy contents to the lib folder.
1. Remove empty targets from default Podfile (will error if you don't!).
1. Run `pod` in Project, note output.
1. Import `<ioskw />` header into primary view controller.
1. And add test code to view did load.

- (void)viewDidLoad
{
[super viewDidLoad];
[self alert:@"HEllo!"];
}

1. Run demo app.

Published in: Self Improvement, Technology
0 Comments
3 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
1,027
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
23
Comments
0
Likes
3
Embeds 0
No embeds

No notes for slide

Transcript of "Using Cocoapods"

  1. 1. Using CocoaPods Library Dependency Management for Xcode with Jeffrey Sambells! http://JeffreySambells.com
  2. 2. Why CocoaPods?
  3. 3. Build dependencies Error-prone + Difficult upgrade path Cluttered project Link binary with libraries = Header search path Libraries in version control No transitive dependencies
  4. 4. Automatic add/remove Automatic build configuration Project is always shippable! Discoverability + + Large ecosystem = Separation of third-party code Handles ARC and no ARC Only includes the relevant source files
  5. 5. Installation… $ sudo gem update -- system $ sudo gem install cocoapods $ pod setup
  6. 6. Using CocoaPods in Xcode
  7. 7. Cocoapods Workflow 1. Create your Podfile 2. Search for and add your dependencies 3. Run pod command 4. Open your new .xcworkspace (it will be created) 5. ??? 6. Profit!
  8. 8. Creating the Podfile $ cd /path/to/project/folder $ touch Podfile
  9. 9. Simple pod 'AFNetworking' pod 'ObjectiveSugar', '~> 0.5'
  10. 10. Advanced platform :ios, '7.0' inhibit_all_warnings! ! workspace 'Example.xcworkspace' ! pod 'AFNetworking', :git => 'https://github.com/gowalla/ AFNetworking.git', :commit => '082f8319af' ! pod 'MySecretPod', :path => ‘~/Documents/MySecretPod' ! pod 'BleedingEdgePod', :head ! target :KIFTests, :exclusive => false do pod 'KIF' end ! post_install do |installer| installer.project.targets.each do |target| puts "#{target.name}" end end
  11. 11. Install/Update into Project $ cd /path/to/project/folder $ pod Hint: use pod --verbose if you want more info.
  12. 12. The Podfile and Dependencies
  13. 13. General Settings Specify a platform and minimum version
 platform :ios, '6.1' ! Optionally specify project and workspace files.
 For example MyProject.xcodeproj and MyProject.xcworkspace
 xcodeproj 'MyProject' workspace 'MyWorkspace'
  14. 14. Dependencies (Pods) Specify pod name and other options as necessary:
 • 
 pod ‘PodName' Some pods use sub specs to group optional functionality so you need to specify them as well:
 • 
 
 
 pod ‘PodName' pod ‘PodName/SubSpecA’ pod ‘PodName/SubSpecB‘
  15. 15. Pod Versioning x.x.x • References a specific git tag using a semantic version. • Specify no version, a specific version, a logical version or an optimistic version.
 
 
 
 pod pod pod pod 
 • ‘PodName’ ‘PodName’, ‘0.1.0’ ‘PodName’, ‘<= 0.1.0’ ‘PodName’, ‘~> 0.1.0’ You can also use :head to get the most bleeding edge version. pod ‘PodName’, :head
  16. 16. Logical Versions • '> 0.1' Any version higher than 0.1 • '>= 0.1' Version 0.1 and any higher version • '< 0.1' Any version lower than 0.1 • '<= 0.1' Version 0.1 and any lower version
  17. 17. Optimistic Versions • '~> 0.1.2' Version 0.1.2 and the versions up to 0.2, not including 0.2 and higher • '~> 0.1' Version 0.1 and the versions up to 1.0, not including 1.1 and higher • '~> 0' Version 0 and higher, this is basically the same as not having it.
  18. 18. Pod Sources Pods load information from a podspec file.
 The podspec files are located in… • the shared public spec repo at 
 https://github.com/CocoaPods/Specs, • on your local machine, • in a private git repo.
  19. 19. Public Shared Specs you only need to specify the name:
 • pod 'AFNetworking' you can optionally specify a specific branch or fork:
 • 
 • pod ‘AFNetworking’, :git => ‘https://github.com/ iamamused/AFNetworking.git' and a specific commit:
 pod ‘AFNetworking’, :git => ‘https://github.com/ myuser/AFNetworking.git', :commit => '082f8319af'
  20. 20. Local Podspecs Load a podspec from your local machine using :path
 
 
 pod ‘MyAwesomePod’, path: => ‘../path/ MyAwesomePod.podspec’
  21. 21. Private Podspecs Load a podspec from a private git repository using :git (simiar to public specs)
 
 
 pod ‘MyAwesomePod’, git: => ‘http://example.com/ MyAwesomePod.git’
  22. 22. Bonus Code doesn’t have a pod? no problem. Make your own pod spec for it and use that: ! pod 'ExampleKit', :podspec => !'https://raw.github.com/gist/ 123/ExampleKit.podspec'
  23. 23. Targets Targets allow you to specify which Xcode target have which pod dependencies. Pod dependencies are applied to 
 individual or multiple targets. If no specific target is specified it 
 only applies to the first target in your project!
  24. 24. Targets Use link_with to specify multiple targets for all pod dependencies:
 
 pod ‘PodName’ link_with [‘MyApp’,’MyAppDemo’]
  25. 25. Targets Or use target :targetname to specify targets individually: pod ‘PodName’ ! target :test, :exclusive => true do pod 'Kiwi' end ! • exclusive => true will only include pods declared in the target block. • exclusive => false will include pods declared in the target block along with those declared in the parent.
  26. 26. Hooks pre_install Make any changes to the Pods after they have been downloaded but before they are installed. ! ! pre_install do |installer_representation| # Do something fancy! end
  27. 27. Hooks post_install Make any changes to the generated Pods project, or any other tasks you might want to perform. ! post_install do |installer_representation| installer_representation.project.targets.each do |target| target.build_configurations.each do |config| config.build_settings['GCC_ENABLE_OBJC_GC'] = 'supported' end end end
  28. 28. Pod Best Practices • Check the source, documentation and upkeep of a pod! Don’t blindly use them. • Include pods in source control? NO! err YES! • Use inhibit_all_warnings! to hide warnings in Pods (after you check them).
  29. 29. DEMO TIME!
  30. 30. Thanks! More Info" cocoapods.org JeffreySambells.com @iamamused
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×