Using Cocoapods
-
Upload
jeffreysambells -
Category
Self Improvement
-
view
799 -
download
0
description
Transcript of Using Cocoapods
Using CocoaPodsLibrary Dependency Management for Xcode
with Jeffrey Sambells!http://JeffreySambells.com
Why CocoaPods?
+ =
Build dependencies
Header search path
Link binary with libraries
Libraries in version control
Cluttered project
Error-prone
Difficult upgrade path
No transitive dependencies
+ =+
Automatic add/remove Automatic build configuration
Handles ARC and no ARC
Separation of third-party code
Only includes the relevant source files
Project is always shippable!
Large ecosystem
Discoverability
Installation…
$ sudo gem update -- system
$ sudo gem install cocoapods
$ pod setup
Using CocoaPods in Xcode
Cocoapods Workflow1. 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!
Creating the Podfile
$ cd /path/to/project/folder
$ touch Podfile
Simple
pod 'AFNetworking' pod 'ObjectiveSugar', '~> 0.5'
Advancedplatform :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
Install/Update into Project
$ cd /path/to/project/folder
$ pod
Hint: use pod --verbose if you want more info.
The Podfile and Dependencies
General SettingsSpecify a platform and minimum version
!
Optionally specify project and workspace files. For example MyProject.xcodeproj and
MyProject.xcworkspace
platform :ios, '6.1'
xcodeproj 'MyProject' workspace 'MyWorkspace'
Dependencies (Pods)• Specify pod name and other options as
necessary:
• Some pods use sub specs to group optional functionality so you need to specify them as well:
pod ‘PodName'
pod ‘PodName' pod ‘PodName/SubSpecA’ pod ‘PodName/SubSpecB‘
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.
• You can also use :head to get the most bleeding edge version.
pod ‘PodName’, :head
pod ‘PodName’ pod ‘PodName’, ‘0.1.0’ pod ‘PodName’, ‘<= 0.1.0’ pod ‘PodName’, ‘~> 0.1.0’
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
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.
Pod SourcesPods 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.
Public Shared Specs• you only need to specify the name:
• you can optionally specify a specific branch or fork:
• and a specific commit:
pod 'AFNetworking'
pod ‘AFNetworking’, :git => ‘https://github.com/iamamused/AFNetworking.git'
pod ‘AFNetworking’, :git => ‘https://github.com/myuser/AFNetworking.git', :commit => '082f8319af'
Local Podspecs
Load a podspec from your local machine using :path
pod ‘MyAwesomePod’, path: => ‘../path/MyAwesomePod.podspec’
Private Podspecs
Load a podspec from a private git repository using :git (simiar to public specs)
pod ‘MyAwesomePod’, git: => ‘http://example.com/MyAwesomePod.git’
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'
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!
TargetsUse link_with to specify multiple targets for all
pod dependencies:
pod ‘PodName’ link_with [‘MyApp’,’MyAppDemo’]
TargetsOr use target :targetname to specify targets
individually:
!
!
• 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.
pod ‘PodName’ target :test, :exclusive => true do pod 'Kiwi' end
Hookspre_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
Hookspost_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
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).
DEMO TIME!
Thanks!
More Info"
cocoapods.org
JeffreySambells.com
@iamamused