iOS.CocoaPods.0
1. CocoaPods
CocoaPods 是Objective-C (iOS and OS X) projects 的依赖管理器。
A CocoaPod (singular) is a specification for a library, usually open source.
CocoaPods (plural) is the tool for managing these specs. [1]
2. How to install CocoaPods
2.1 安装CocoaPods步骤
在终端中输入以下命令:
$ sudo gem install cocoapods
注:在终端中安装CocoaPods时可能会遇到如下问题 (感谢伟大的GFW):
"ERROR: Could not find a valid gem 'cocoapods' (>= 0), here is why:
Unable to download data from https://rubygems.org/ - Errno::ETIMEDOUT: Operation timed out - connect(2) (https://rubygems.org/quick/Marshal.4.8/cocoapods-0.33.1.gemspec.rz)"
这时候我们需要改变 gem source, 参考[4], [5]。需要在终端中执行如下命令:
$ gem sources -l
$ gem sources --remove https://rubygems.org/
$ gem sources -a https://ruby.taobao.org/
$ gem sources -l
接下来输入以下命令:
$ pod setup
This process will likely take a while as this command clones the CocoaPods Specs repository
into ~/.cocoapods/ on your computer. Ref[6]
至此CocoaPods安装完毕。
注: 如果漏掉 "$ pod setup" 命令,而直接执行"$ pod init"
会有类似以下的error信息:
"$ pod init
Setting up CocoaPods master repo
[!] /usr/bin/git clone 'https://github.com/CocoaPods/Specs.git' master --depth=1
Cloning into 'master'...
error: RPC failed; result=52, HTTP code = 0
fatal: The remote end hung up unexpectedly
/Library/Ruby/Gems/2.0.0/gems/claide-0.6.1/lib/claide/command.rb:304:in `handle_exception': undefined method `verbose?' for nil:NilClass (NoMethodError)
from /Library/Ruby/Gems/2.0.0/gems/claide-0.6.1/lib/claide/command.rb:284:in `rescue in run'
from /Library/Ruby/Gems/2.0.0/gems/claide-0.6.1/lib/claide/command.rb:274:in `run'
from /Library/Ruby/Gems/2.0.0/gems/cocoapods-0.33.1/lib/cocoapods/command.rb:48:in `run'
from /Library/Ruby/Gems/2.0.0/gems/cocoapods-0.33.1/bin/pod:33:in `<top (required)>'
from /usr/bin/pod:23:in `load'
from /usr/bin/pod:23:in `<main>' "
2.2 更新CocoaPods
$ [sudo] gem install cocoapods
http://guides.cocoapods.org/using/getting-started.html
更新cocoapods到指定的版本:
$ [sudo] gem install cocoapods -v VERSION
3. How to use CocoaPods
创建一个Podfile, 将依赖添加到Podfile中。
3.1 创建一个新Xcode Project, 并在该工程中使用CocoaPods
A: 如正常创建一个新工程
B: 在终端窗口中, 执行 cd 命令到 工程目录
C: 创建一个Podfile文件. ($ vim Podfile 或者 $ touch Podfile )
D: 在Podfile文件第一行指明平台和版本, 例如:
platform :ios, '6.0'
E: Add a CocoaPod by specifying pod '$PODNAME' on a single line (????)
pod 'ObjectiveSugar'
F: 保存 Podfile.
G: 运行 $ pod install
H: 打开MyApp.xcworkspace
3.2 集成到一个已存在的workspace中
集成CocoaPods到一个存在的workspace中,需要在Podfile中额外添加一行。
将".xcworkspace"去掉后的根文件名添加到Podfile中:
workspace 'MyWorkspace'
3.3 是否应该在源代码控制中忽略Pods目录?
3.4 Podfile.lock是什么以及作用?
该文件是由Pod工具生成的:
"When this is run, CocoaPods will recursively analyze the dependencies of each project,
resolving them into a dependency graph, and serializing into a Podfile.lock file." Ref[10]
3.5 在背后发生了什么? (What is happening behind the scenes?)
3.6 Pods 和 Submodules(git)
3.7 从submodules切换到CocoaPods
4. Demo
Ref[3]
4.1 将Pod添加到Xcode Project中
安装CocoaPods完毕后,用Xcode创建一个Project(例如:CocoaPodsDemo)后,
cd 到 CocoaPodsDemo 的目录下:
$ pwd
/Users/XiaoKL/Projects/CocoaPodsDemo
执行以下命令:
$ pod init
"$ pod init" 创建一个Podfile。
$ ls -l
total 8
drwxr-xr-x 12 XiaoKL staff 408 8 23 22:09 CocoaPodsDemo
drwxr-xr-x 5 XiaoKL staff 170 8 23 22:09 CocoaPodsDemo.xcodeproj
drwxr-xr-x 5 XiaoKL staff 170 8 23 22:09 CocoaPodsDemoTests
-rw-r--r-- 1 XiaoKL staff 160 8 23 22:52 Podfile
创建的Podfile内容如下:
"# Uncomment this line to define a global platform for your project
# platform :ios, "6.0"
target "CocoaPodsDemo" do
end
target "CocoaPodsDemoTests" do
end "
4.2 修改生成的Podfile
修改后的Podfile内容如下:
"# Uncomment this line to define a global platform for your project
platform :ios, "6.0"
target "CocoaPodsDemo" do
pod "SVProgressHUD", "0.9"
end
target "CocoaPodsDemoTests" do
end"
然后执行命令:
"$ pod install"
该命令有以下输出:
"Analyzing dependencies
Downloading dependencies
Installing SVProgressHUD (0.9)
Generating Pods project
Integrating client project
[!] From now on use `CocoaPodsDemo.xcworkspace`."
"$ pod install" 命令创建了:CocoaPodsDemo.xcworkspace, Podfile.lock和Pods, 如下可以从ls命令的时间戳上看出来。
$ ls -alG
total 16
drwxr-xr-x 9 XiaoKL staff 306 8 23 23:30 .
drwxr-xr-x 4 XiaoKL staff 136 8 23 22:09 ..
drwxr-xr-x 12 XiaoKL staff 408 8 23 22:09 CocoaPodsDemo
drwxr-xr-x 5 XiaoKL staff 170 8 23 22:09 CocoaPodsDemo.xcodeproj
drwxr-xr-x 3 XiaoKL staff 102 8 23 23:30 CocoaPodsDemo.xcworkspace
drwxr-xr-x 5 XiaoKL staff 170 8 23 22:09 CocoaPodsDemoTests
-rw-r--r-- 1 XiaoKL staff 186 8 23 23:26 Podfile
-rw-r--r-- 1 XiaoKL staff 165 8 23 23:30 Podfile.lock
drwxr-xr-x 17 XiaoKL staff 578 8 23 23:30 Pods
4.3 使用SVProgressHUD
在代码中使用SVProgressHUD,需要#import <SVProgressHUD.h>
4.4 为一个已经用CocoaPods管理的workspace添加一个新的依赖库
A: 将依赖项添加到Podfile文件中
B: 运行命令: $ pod install
4.5 pod install 卡在 "Analyzing dependencies"
A: pod install --verbose --no-repo-update
或者使用VPN。 这是因为访问github.com慢导致的。
5. 解析Podfile文件
http://guides.cocoapods.org/using/the-podfile.html
5.1 Podfile是什么?
Podfile一个规格书, 该规格书来描述Xcode工程的target的依赖关系。
The Podfile is a specification that describes the dependencies of the targets of one
or more Xcode projects. The Podfile always creates an implicit target, named default,
which links to the first target of the user project.
podfile可以如下简单:
pod 'AFNetworking', '~> 1.0'
关于version的说明:
'> 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.
version 也可以是 :head, 例如:
pod 'Objection', :head
注:在实践中,Podfile中如下写,
pod 'FMDB', '~> 2.0'
安装的是 2.5(即 当时最新的版本), 下面是console的输出:
Installing FMDB (2.5)
5.2 version冲突的解决
[Todo]
5.3 使用本地文件夹的文件
[Todo]
5.4 From a podspec in the root of a library repo.
本节示例了如何使用某个分支,某个tag,某个commit。
5.5 Podfile的语法规范
[Todo]
6. Pods在使用过程中的FQA
6.1 .pbxproj格式变为xml格式的问题
Cocoapods 0.34 在pod install 或 pod update 时会将Pods/Pods.xcodeproj/project.pbxproj
以xml的格式重写,这给我们进行merge带来了很大的挑战。
解决方法: 安装xcproj Ref[11]
6.2 pod install Vs. pod update
pod install 和 pod update的区别以及pod其它的各个命令 Ref[12]
Ref[12] Ref[14]
Reference:
1. http://ashfurrow.com/blog/getting-started-with-cocoapods-demo
2.
3. Getting Started with CocoaPods
http://ashfurrow.com/blog/getting-started-with-cocoapods-demo
该blog中讲的安装过程漏掉了"pod setup" 这一步, 参加[6]中的步骤。
介绍了一个Xcode插件:https://github.com/kattrali/cocoapods-xcode-plugin
PPT:Effective Use of Open Source Software (ToRead)
https://speakerdeck.com/ashfurrow/effective-use-of-open-source-software
4. RubyGems 镜像
http://ruby.taobao.org
5.
http://stackoverflow.com/questions/19612185/unable-to-install-cocoapods-gem-from-rubygems-org-bad-response-backend-read-e
6. Introduction to CocoaPods Tutorial (ToRead)
http://www.raywenderlich.com/64546/introduction-to-cocoapods-2
7. Streamlining Cocoa Development With CocoaPods (ToRead)
http://code.tutsplus.com/tutorials/streamlining-cocoa-development-with-cocoapods--mobile-15938
8. Using CocoaPods to Modularize a Big iOS App (ToRead)
http://dev.hubspot.com/blog/architecting-a-large-ios-app-with-cocoapods
9. Using CocoaPods to Manage Private
http://chariotsolutions.com/blog/post/using-cocoapods-to-manage-private-libraries/
CocoaPods 支持git, svn等scm工具。 如何使用CocoaPods管理私有的库,以及示例。
pod spec create projectname
10. CocoaPods
http://nshipster.com/cocoapods/
11. Generate ASCII format xcodeproj
https://github.com/CocoaPods/CocoaPods/wiki/Generate-ASCII-format-xcodeproj
12. pod 各个命令的解释
https://github.com/CocoaPods/CocoaPods/issues/760#issuecomment-46300500
13. 在cocoapods中搜索开源库/Project
https://cocoapods.org/
14. Demystifying Pod Install and Pod Update [To Read]
https://hackernoon.com/demystifying-pod-install-and-pod-update-1751dc35de43
Todo:
1. CocoaPod的工作原理是什么?
$ pod install 命令创建了一个workspace,该workspace包含了原来的project,以及一个新建的project。
这和使用subproject的方法有和不同呢?
2. .xcconfig文件的作用?
3. CocoaPods能给我们带来什么?并发式开发,or 管理大项目?
浙公网安备 33010602011771号