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.

具体步骤

    1. 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.
    1. 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 .
    1. Add your Podspec to your repo
      Make sure you've tagged and versioned your source appropriately, then run:

$ 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]

九、所有的命令汇总

十、上传之后如何提高自己的库搜索排名

附录:

参考链接:

CocoaPods Guides

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,088评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,715评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,361评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,099评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 60,987评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,063评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,486评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,175评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,440评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,518评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,305评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,190评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,550评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,880评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,152评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,451评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,637评论 2 335

推荐阅读更多精彩内容