Cocoapod详解
引言:What? 搞iOS不知道CocoaPod
首先给出一个链接,通篇文章将完全是参照,官网的内容进行整理和总结,也建议大家阅读官网的文章,网上文件大多寻章摘句。
CocoaPod官网的使用指南
CocoaPod官网
以上两个链接,可谓学习CocoaPod最好最全面的资料
一、What is CocoaPods
CocoaPods manages library dependencies for your Xcode projects.
The dependencies for your projects are specified in a single text file called a Podfile. CocoaPods will resolve dependencies between libraries, fetch the resulting source code, then link it together in an Xcode workspace to build your project.
Ultimately the goal is to improve discoverability of, and engagement in, third party open-source libraries by creating a more centralised ecosystem.
概括起来可以说明为是 CocoaPod通过Podfile文件(一种通过简单文本描述来指定依赖库的文件)可以解决类库依赖,同时通过workSpace的形式添加到你的Xcode工程中。
其中继目的就是建立一个更加中心化的生态系统来提升第三方库管理。
二、Getting Started
To update CocoaPods you simply install the gem again
$ [sudo] gem install cocoapods
Or for a pre-release version
$ [sudo] gem install cocoapods --pre
任何安装问题可以点开该文章的链接,其中CocoaPod依赖于Ruby
三、Using CocoaPods
生成一个CocoaPods
$ pod init
具体的Podfile语法
platform :ios, '9.0'
target 'MyApp' do
pod 'ObjectiveSugar'
end
之后的步骤
$ pod install
四、pod install vs. pod update
bundler, RubyGems or composer也有对对应的语言,CocoaPod设计的和他们类似
pod install
适用于获取pod,当你编辑Podfile中存在add, update, remove a pod的需求的时候,可以使用该命令。
- 根据Podfile.lock的内容,搞定不在Podfile.lock里面的pods的依赖,不会去尝试检查更新的版本。
- 对于不存在Podfile.lock里面的pod,将按照podfile里面的内容进行搜索对应的版本
pod outdated
CocoaPods将罗列出在Podfile.lock拥有更新版本的pod,也就说如果你执行pod update PODNAME,如果新的版本依然符合Podfile里面的描述,那么将会被更新
pod update
两种形式,pod update PODNAME将更新指定的pod,否则将更新所有。前提是符合Podfile里面的描述内容
注意
基于podfile.lock的作用,所以当仓库共享的时候,需要注意要提交podfile.lock。以方便统一的来锁定依赖库。
五、The Podfile
文件整体框架
简单的例子
target 'MyApp' do
use_frameworks!
pod 'Alamofire', '~> 3.0'
end
复杂的例子
source 'https://github.com/CocoaPods/Specs.git'
source 'https://github.com/Artsy/Specs.git'
platform :ios, '9.0'
inhibit_all_warnings!
target 'MyApp' do
pod 'GoogleAnalytics', '~> 3.1'
# Has its own copy of OCMock
# and has access to GoogleAnalytics via the app
# that hosts the test target
target 'MyAppTests' do
inherit! :search_paths
pod 'OCMock', '~> 2.0.1'
end
end
post_install do |installer|
installer.pods_project.targets.each do |target|
puts target.name
end
end
如果想多个targets共用一些pod,考虑使用abstract_target
# There are no targets called "Shows" in any Xcode projects
abstract_target 'Shows' do
pod 'ShowsKit'
pod 'Fabric'
# Has its own copy of ShowsKit + ShowWebAuth
target 'ShowsiOS' do
pod 'ShowWebAuth'
end
# Has its own copy of ShowsKit + ShowTVAuth
target 'ShowsTV' do
pod 'ShowTVAuth'
end
end
隐藏抽象target,如下相当于隐晦地使用抽象target
pod 'ShowsKit'
pod 'Fabric'
# Has its own copy of ShowsKit + ShowWebAuth
target 'ShowsiOS' do
pod 'ShowWebAuth'
end
# Has its own copy of ShowsKit + ShowTVAuth
target 'ShowsTV' do
pod 'ShowTVAuth'
end
具体的语法细则
指定版本规则
不指定版本
pod 'SSZipArchive'
冻结版本
pod 'Objection', '0.9'
逻辑运算符指定
- '> 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
~>来指定
- '~> 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.0 and higher
- '~> 0' Version 0 and higher, this is basically the same as not having it.
指定本地文件机制
pod 'Alamofire', :path => '~/Documents/Alamofire'
指定对应的git仓库
默认使用master分支
pod 'Alamofire', :git => 'https://github.com/Alamofire/Alamofire.git'
使用其它分支
pod 'Alamofire', :git => 'https://github.com/Alamofire/Alamofire.git', :branch => 'dev'
指定tag
pod 'Alamofire', :git => 'https://github.com/Alamofire/Alamofire.git', :tag => '3.1.1'
指定对应commit
pod 'Alamofire', :git => 'https://github.com/Alamofire/Alamofire.git', :commit => '0f506b1c45'
六、Making a CocoaPod
简单的建立CocoaPod可以参考以上连接,细化请看如下说明。
Using Pod Lib Create
pod lib create SLLibrary
默认的模板如下图,当然我们也可以采用自己的模板,通过增加如下参数为--template-url=URL
image.png
七、Getting setup with Trunk
CocoaPods Trunk
CocoaPods Trunk is an authentication and CocoaPods API service. To publish new or updated libraries to CocoaPods for public release you will need to be registered with Trunk and have a valid Trunk session on your current device. You can read about Trunk's history and development on the blog, and about private pods for yourself or your team.
使用你的email在当前设备上注册一个会话:
$ pod trunk register orta@cocoapods.org 'Orta Therox' --description='macbook air'
收到邮件之后,记得需要到邮箱里面激活下。当然也可以通过以下命令来查案你电脑中trunk的会话信息。
pod trunk me
Deploying a library
- Lints your Podspec locally
pod spec lint [NAME.podspec]
- 成功之后就可以推送到CocoaPod
如果推送到Public的使用
pod trunk push [NAME.podspec]
私有库的话就推送到
pod repo push REPO [NAME.podspec]
- 推送成功后,trunk将会发布一个权威的json代表陈述你的Podspec
添加其他人作为贡献者
// For example, to add kyle@cocoapods.org to the library ARAnalytics:
$ pod trunk add-owner ARAnalytics kyle@cocoapods.org
八、Private Pods
Private Pods
CocoaPods is a great tool not only for adding open source code to your project, but also for sharing components across projects. You can use a private Spec Repo to do this.
具体步骤
- Create a Private Spec Repo
You do not need to fork the CocoaPods/Specs Master repo. Make sure that everyone on your team has access to this repo, but it does not need to be public.
- Create a Private Spec Repo
- Add your Private Repo to your CocoaPods installation
$ pod repo add REPO_NAME SOURCE_URL
To check if your installation is successful and ready to go:
$ cd ~/.cocoapods/repos/REPO_NAME
$ pod repo lint .
- Add your Podspec to your repo
Make sure you've tagged and versioned your source appropriately, then run:
- Add your Podspec to your repo
$ pod repo push REPO_NAME SPEC_NAME.podspec
This will run pod spec lint, and take care of all the little details for setting up the spec in your private repo.
The structure of your repo should mirror this:
.
├── Specs
└── [SPEC_NAME]
└── [VERSION]
└── [SPEC_NAME].podspec
Your private Pod is ready to be used in a Podfile. You can use the spec repository with the source
directive in your Podfile as shown in the following example:
source 'URL_TO_REPOSITORY'
如何移除私有库
pod repo remove [name]