iOS.CocoaPods

1. CocoaPods

CocoaPods 是Objective-C (iOS and OS X) projects 的依赖管理器。

 

2. How to install CocoaPods

2.1 在终端中输入以下命令:

$ 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. [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>‘ "

 

3. How to use CocoaPods [2]

创建一个Podfile, 将依赖添加到Podfile中。

3.1 创建一个新Xcode工程和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是什么? 

3.5 在背后发生了什么? (What is happening behind the scenes?) 

3.6 Pods 和 Submodules (git) 

3.7 从submodules切换到CocoaPods 

 

4. Demo [3]

4.1 安装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>

5. 解析Podfile文件 

 

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://*.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

Todo:

1. CocoaPod的工作原理是什么?

$ pod install 命令创建了一个workspace,该workspace包含了原来的project,以及一个新建的project。

这和使用subproject的方法有和不同呢?

2. .xcconfig文件的作用?

3. CocoaPods能给我们带来什么?并发式开发,or 管理大项目?

iOS.CocoaPods

上一篇:[IOS]译Size Classes with Xcode 6: One Storyboard for all Sizes


下一篇:Cocos2d-JS: 2.编译到安卓和iOS