一、CocoaPods 的卸载
有的时候我们需要降低Cocoapods的版本来解决第三方库的兼容问题.
1. 移除pod组件
这条指令会告诉你Cocoapods组件装在哪里 :
$ which pod
你可以手动移除这个组件 :
$ sudo rm -rf <path>
2.移除 RubyGems 中的 Cocoapods程序包
Q: RubyGems是什么?
A: RubyGems(简称 gems)是一个用于对 Ruby组件进行打包的 Ruby 打包系统。 它提供一个分发 Ruby 程序和库的标准格式,还提供一个管理程序包安装的工具。
查看gems中本地程序包:
$ gem list
输出
*** LOCAL GEMS ***
activesupport (4.2.5)
bigdecimal (1.2.0)
CFPropertyList (2.2.8)
claide (1.0.0, 0.9.1)
cocoapods (1.0.1, 0.39.0)
cocoapods-core (1.0.1, 0.39.0)
cocoapods-deintegrate (1.0.0)
cocoapods-downloader (1.1.0, 0.9.3)
cocoapods-plugins (1.0.0, 0.4.2)
cocoapods-search (1.0.0, 0.1.0)
cocoapods-stats (1.0.0, 0.6.2)
cocoapods-trunk (1.0.0, 0.6.4)
cocoapods-try (1.1.0, 0.5.1)
colored (1.2)
concurrent-ruby (1.0.2)
escape (0.0.4)
fourflusher (0.3.2)
fuzzy_match (2.0.4)
i18n (0.7.0)
io-console (0.4.2)
json (1.7.7)
libxml-ruby (2.6.0)
minitest (5.8.3, 4.3.2)
molinillo (0.4.5, 0.4.1)
nap (1.0.0)
netrc (0.7.8)
nokogiri (1.5.6)
psych (2.0.0)
rake (0.9.6)
rdoc (4.0.0)
sqlite3 (1.3.7)
test-unit (2.0.0.0)
thread_safe (0.3.5)
tzinfo (1.2.2)
xcodeproj (1.2.0, 0.28.2)
发现Cocoapods的程序包
cocoapods (1.0.1, 0.39.0)
cocoapods-core (1.0.1, 0.39.0)
cocoapods-deintegrate (1.0.0)
cocoapods-downloader (1.1.0, 0.9.3)
cocoapods-plugins (1.0.0, 0.4.2)
cocoapods-search (1.0.0, 0.1.0)
cocoapods-stats (1.0.0, 0.6.2)
cocoapods-trunk (1.0.0, 0.6.4)
cocoapods-try (1.1.0, 0.5.1)
移除程序包之一:(1.0.1与0.39.0版本同操作)
$ sudo gem uninstall cocoapods -v 0.39.0
输出
Successfully uninstalled cocoapods-0.39.0
移除程序包之二:(1.0.1与0.39.0版本同操作)
$ sudo gem uninstall cocoapods-core -v 0.39.0
输出
Successfully uninstalled cocoapods-core-0.39.0
3.然后安装指定版本的Cocoapods
安装:
$ sudo gem install cocoapods -v 0.34.4
安装成功后查看当前版本:
$ pod --version
输出
0.34.1
二、CocoaPods的安装(安装最新版)
1.开启 terminal
2.移除现有 Ruby 默认源
$ gem sources --remove https://rubygems.org/
3.使用新的源
$ gem sources -a https://ruby.taobao.org/
4.验证新源是否替换成功
$ gem sources -l
5.安装 CocoaPods
$ sudo gem install cocoapods
$ pod setup
备注:苹果系统升级 OS X EL Capitan 后安装改为:
$ sudo gem install -n /usr/local/bin cocoapods
$ pod setup
6.更新 gem
$ sudo gem update --system
三、CocoaPods的更新
1.官方更新命令
重新安装:$ sudo gem install cocoapods
更新:$ sudo gem install cocoapods --pre
2.如果以上两个命令都失败,并且报如下错误,可尝试命令:
$ sudo gem install -n /usr/local/bin cocoapods --pre
ERROR: While executing gem ... (Errno::EPERM)
Operation not permitted - /usr/bin/xcodeproj
四、CocoaPods的使用
1.新建工程,并在终端用 cd 指令到文件夹内
$ pod search <第三方>
2.新建 Podfile 文件
$ touch Podfile
3.编辑 Podfile 文件,并写入要添加的第三方库
platform:ios, '8.0'
pod 'AFNetworking', '~> 2.3.1' // 第三方
4.导入第三方库
$ pod install // 不推荐该命令
备注:使用CocoaPods来添加第三方类库时,如果无论是执行pod install还是pod update都卡在了Analyzing dependencies不动,原因在于当执行以上两个命令的时候会升级CocoaPods的spec仓库,加一个参数可以省略这一步,然后速度就会提升不少。加参数的命令如下:
$ pod install --verbose --no-repo-update
$ pod update --verbose --no-repo-update
5.退出终端
五、安装CocoaPods时的常见错误、解决方案
1. Error 1: 执行 pod setup 时,terminal一直停留在如下文字
Performing a deep fetch of the `master` specs repo to improve future performance
这个错误是因为 pod setup 的时候创建master这个库,没成功,之后就算移除镜像重新安装的话,默认是从matser库里获取,导致安装不成功。
解决方案:移除master库,重新创建(再重新setup)。
$ pod repo update --verbose
$ rm -rf ~/.cocoapods/repos/master
$ pod setup
2.Error 2:
Error fetching http://ruby.taobao.org/:
bad response Not Found 404 (http://ruby.taobao.org/specs.4.8.gz)
解决方案:
把安装流程中$ gem sources -a http://ruby.taobao.org/
改为:$ gem sources -a https://ruby.taobao.org/
3.Error 3:
ERROR: While executing gem ... (Errno::EPERM)
Operation not permitted - /usr/bin/pod
解决方案:
苹果系统升级 OS X EL Capitan 后会出现的插件错误,将安装流程 5.安装 CocoaPods 的sudo gem install cocoapods
改为sudo gem install -n /usr/local/bin cocoapods
4.Error 4:
[!] Unable to satisfy the following requirements:
- `AFNetworking (~> 2.3.1)` required by `Podfile`
Specs satisfying the `AFNetworking (~> 2.3.1)` dependency were found, but they required a higher minimum deployment target.
解决方案:Podfile 文件 中 platform:ios, ‘8.0’ 后边的 8.0 是平台版本号 ,一定要加上
5.Error5:
――――――――――MARKDOWN TEMPLATE ――――――――――
### Report
* What did you do?
* What did you expect to happen?
* What happened instead?
### Stack
```
CocoaPods : 0.29.0
Ruby : ruby 2.0.0p247 (2013-06-27 revision 41674) [universal.x86_64-darwin13]
RubyGems : 2.1.11
Host : Mac OS X 10.9.2 (13C64)
Xcode : 5.1 (5B130a)
Ruby lib dir : /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib
Repositories : master - https://github.com/CocoaPods/Specs.git @ bd6736d07b16c98ab7a1dae04697cae002f25a9b
```
### Podfile
```ruby
platform :ios,'8.0'
pod 'MBProgressHUD', '~> 0.8'
```
### Error
```
Psych::SyntaxError - (/Users/MAXJ/.cocoapods/repos/master/CocoaPods-version.yml): mapping values are not allowed in this context at line 3 column 4
/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/psych.rb:205:in `parse'
/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/psych.rb:205:in `parse_stream'
/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/psych.rb:153:in `parse'
/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/psych.rb:129:in `load'
/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/psych.rb:299:in `block in load_file'
/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/psych.rb:299:in `open'
/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/psych.rb:299:in `load_file'
/Library/Ruby/Gems/2.0.0/gems/cocoapods-0.29.0/lib/cocoapods/sources_manager.rb:261:in `version_information'
/Library/Ruby/Gems/2.0.0/gems/cocoapods-0.29.0/lib/cocoapods/sources_manager.rb:222:in `repo_compatible?'
/Library/Ruby/Gems/2.0.0/gems/cocoapods-0.29.0/lib/cocoapods/sources_manager.rb:281:in `master_repo_functional?'
/Library/Ruby/Gems/2.0.0/gems/cocoapods-0.29.0/lib/cocoapods/command.rb:39:in `parse'
/Library/Ruby/Gems/2.0.0/gems/claide-0.4.0/lib/claide/command.rb:179:in `parse'
/Library/Ruby/Gems/2.0.0/gems/cocoapods-0.29.0/lib/cocoapods/command.rb:38:in `parse'
/Library/Ruby/Gems/2.0.0/gems/claide-0.4.0/lib/claide/command.rb:211:in `run'
/Library/Ruby/Gems/2.0.0/gems/cocoapods-0.29.0/lib/cocoapods/command.rb:51:in `run'
/Library/Ruby/Gems/2.0.0/gems/cocoapods-0.29.0/bin/pod:24:in `'
/usr/bin/pod:23:in `load'
/usr/bin/pod:23:in `'
```
――――――――――TEMPLATE END ――――――――――
[!] Oh no, an error occurred.
Search for existing github issues similar to yours:
https://github.com/CocoaPods/CocoaPods/search?q=%28%2FUsers%2FMAXJ%2F.cocoapods%2Frepos%2Fmaster%2FCocoaPods-version.yml%29%3A+mapping+values+are+not+allowed+in+this+context+at+line+3+column+4&type=Issues
If none exists, create a ticket, with the template displayed above, on:
https://github.com/CocoaPods/CocoaPods/issues/new
Don't forget to anonymize any private data!
解决方案:
$ sudo rm -rf ~/.cocoapods/repos/master
$ pod setup
参考文献:
1.http://www.meetrue.com/archives/133
2.http://www.jianshu.com/p/071d30a3af02
3.http://blog.csdn.net/a_ellisa/article/details/51556685