1. 程式人生 > >CocoaPods 的解除安裝、安裝、更新及常見錯誤

CocoaPods 的解除安裝、安裝、更新及常見錯誤

原文地址

一、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