1、使用Cocoapods 指令集:
安装Cocoapods
$ sudo gem install cocoapods
初始化环境
$ pod setup
创建一个Podfile
$ pod init
安装所需要的库
$ pod install
反复使用pod install命令,只会让CocoaPods重复以上步骤,重新安装这些库。所以,当你需要升级他时,使用
$ pod update
安装一个库之前,先试用一下。你只需要在try后面加上任意一个CocoaPods公共库的名称
$ pod try Ono
gem是管理Ruby库和程序的标准包,如果它的版本过低也可能导致安装失败,升级gem指令
$ sudo gem update --system
Ruby的默认源使用的是cocoapods.org,国内访问这个网址有时候会有问题,网上的一种解决方案是将远替换成淘宝的,替换方式如下:
//移除
$ gem sources --remove https://rubygems.org/
//添加
$ gem sources -a http://ruby.taobao.org/
//获取列表
$ gem sources -l
卸载cocoapods和xcodeproj,如果机器上面有多个版本,可以选择All versions卸载
$ sudo gem uninstall cocoapods
$ sudo gem uninstall xcodeproj
删除全局的缓存:
$ sudo rm -fr ~/Library/Caches/CocoaPods/
$ sudo rm -fr ~/.cocoapods/repos/master/
Cocoapods(0.35.0)未安装前gem组件列表:
*** LOCAL GEMS *** bigdecimal (1.2.0) bundler (1.7.8) bundler-unload (1.0.2) executable-hooks (1.3.2) gem-wrappers (1.2.7) io-console (0.4.2) json (1.7.7) minitest (4.3.2) psych (2.0.0) rake (0.9.6) rdoc (4.0.0) rubygems-bundler (1.4.4) rvm (1.11.3.9) test-unit (2.0.0.0)
Cocoapods(0.35.0)安装后的所有gem组件:
activesupport (4.1.8) bigdecimal (1.2.0) bundler (1.7.8) bundler-unload (1.0.2) claide (0.7.0) cocoapods (0.35.0) cocoapods-core (0.35.0) cocoapods-downloader (0.8.0) cocoapods-plugins (0.3.2) cocoapods-trunk (0.4.1) cocoapods-try (0.4.2) colored (1.2) escape (0.0.4) executable-hooks (1.3.2) fuzzy_match (2.0.4) gem-wrappers (1.2.7) i18n (0.6.11) io-console (0.4.2) json (1.7.7) minitest (5.4.3, 4.3.2) molinillo (0.1.2) nap (0.8.0) netrc (0.7.8) open4 (1.3.4) psych (2.0.0) rake (0.9.6) rdoc (4.0.0) rubygems-bundler (1.4.4) rvm (1.11.3.9) test-unit (2.0.0.0) thread_safe (0.3.4) tzinfo (1.2.2) xcodeproj (0.20.2)
Cocoapods会安装的gem 组件(以0.35版本为例):
如果实在出现一些莫名其妙的问题,在git上及stackoverflow等都无法找到答案。 可以尝试卸载Cocopods全部相关组件,然后重装。Fetching: nap-0.8.0.gem (100%)
Successfully installed nap-0.8.0
Fetching: thread_safe-0.3.4.gem (100%)
Successfully installed thread_safe-0.3.4
Fetching: minitest-5.4.3.gem (100%)Fetching: minitest-5.4.3.gem
Successfully installed minitest-5.4.3
Fetching: tzinfo-1.2.2.gem (100%)Fetching: tzinfo-1.2.2.gem
Successfully installed tzinfo-1.2.2
Fetching: i18n-0.6.11.gem (100%)
Successfully installed i18n-0.6.11
Fetching: activesupport-4.1.8.gem (100%)
Successfully installed activesupport-4.1.8
Fetching: open4-1.3.4.gem (100%)
Successfully installed open4-1.3.4
Fetching: escape-0.0.4.gem (100%)
Successfully installed escape-0.0.4
Fetching: colored-1.2.gem (100%)
Successfully installed colored-1.2
Fetching: molinillo-0.1.2.gem (100%)Fetching: molinillo-0.1.2.gem
Successfully installed molinillo-0.1.2
Fetching: netrc-0.7.8.gem (100%)
Successfully installed netrc-0.7.8
Fetching: cocoapods-trunk-0.4.1.gem (100%)
Successfully installed cocoapods-trunk-0.4.1
Fetching: cocoapods-try-0.4.2.gem (100%)
Successfully installed cocoapods-try-0.4.2
Fetching: cocoapods-plugins-0.3.2.gem (100%)
Successfully installed cocoapods-plugins-0.3.2
Fetching: cocoapods-downloader-0.8.0.gem (100%)
Successfully installed cocoapods-downloader-0.8.0
Fetching: xcodeproj-0.20.2.gem (100%)
Successfully installed xcodeproj-0.20.2
Fetching: claide-0.7.0.gem (100%)
Successfully installed claide-0.7.0
Fetching: fuzzy_match-2.0.4.gem (100%)
Successfully installed fuzzy_match-2.0.4
Fetching: cocoapods-core-0.35.0.gem (100%)Fetching: cocoapods-core-0.35.0.gem
Successfully installed cocoapods-core-0.35.0
Fetching: cocoapods-0.35.0.gem (100%)
Successfully installed cocoapods-0.35.0
Parsing documentation for cocoapods-0.35.0
Installing ri documentation for cocoapods-0.35.0
2、Cocoapods 问题汇总:
(1)、之前对新建的一个Swift项目,使用 pod install指令时出现错误:
lixinxingdeMacBook-Pro:SwiftWeather lixinxing$ pod install
Analyzing dependencies
Downloading dependencies
Installing FMDB (2.4)
[!] Error installing FMDB
[!] /usr/local/bin/git clone https://github.com/ccgus/fmdb.git /Users/lixinxing/Desktop/SwiftWeather/Pods/FMDB --single-branch --depth 1 --branch v2.4
Cloning into '/Users/lixinxing/Desktop/SwiftWeather/Pods/FMDB'...
error: RPC failed; result=52, HTTP code = 0
fatal: The remote end hung up unexpectedly
后来对Cocoapods进行卸载及重新安装,但是发现依然如此,后来发现可能是“SOURCES”的问题, 执行gem sources-l,显示:
lixinxingdeMacBook-Pro:SwiftWeather lixinxing$ gem sources -l
*** CURRENT SOURCES ***
http://ruby.taobao.org/
https://ruby.taobao.org/
感觉可能是 https://ruby.taobao.org/ 的问题,可能之前误加多了一个,执行 :gem sources --remove https://ruby.taobao.org/
移除此source后,执行pod install, 安装正常。 但是在实际使用中,一般将source换成 http://ruby.taobao.org/ 后会OK,如果依然出现此问题,那么就要检查网络和服务器的情况。
2、对gem进行升级后出现错误,执行pod search 出现错误:
<p><span style="white-space: pre;"> </span></p><p class="p1"><span class="s1">[!] There was an error reading '/Users/lixinxing/.cocoapods/repos/master/CocoaPods-version.yml'.</span></p><p class="p1"><span class="s1">Please consult http://blog.cocoapods.org/Repairing-Our-Broken-Specs-Repository/ for more information.</span></p>
首次执行pod install OK,当出现Podfile.lock后执行pod install 以及pod update,出现错误,错误指向Podfile.lock, 错误如下:
<p><span style="font-family: Arial, Helvetica, sans-serif;">[!] ERROR: Parsing unable to continue due to parsing error:</span><span></span></p><p class="p1"></p><p class="p1"><span class="s1">contained in the file located at /Users/lixinxing/Desktop/iTools/Podfile.lock</span></p><p class="p1"><span class="s1">PODS:</span></p><p class="p1"><span class="s1"> - MBProgressHUD (0.9)</span></p><p class="p1"><span class="s1"> - TTTAttributedLabel (1.10.1)</span></p><p class="p2"><span class="s1"></span>
</p><p class="p1"><span class="s1">DEPENDENCIES:</span></p><p class="p1"><span class="s1"> - MBProgressHUD (~> 0.9)</span></p><p class="p1"><span class="s1"> - TTTAttributedLabel (~> 1.10.1)</span></p><p class="p2"><span class="s1"></span>
</p><p class="p1"><span class="s1">SPEC CHECKSUMS:</span></p><p class="p1"><span class="s1"> MBProgressHUD: 2038dbcf3dce73215abed6001657043d53aa79a8</span></p><p class="p1"><span class="s1"> TTTAttributedLabel: 94bb04f1e005616002dd2cee0262a0082e533627</span></p><p class="p2"><span class="s1"></span>
</p><p class="p1"><span class="s1">COCOAPODS: 0.35.0</span></p>