mac cocoapod安裝過程


cocoapod: 自動化管理第三方開發包的一個插件, 廢話不多說, 一個新手只需做如下幾個步驟

1-> 安裝ruby環境(可忽略, 不是必要) 

1.1 首先我們先看看當前你機器上ruby的版本

ruby -v

ruby 2.4.0p0 (2016-12-24 revision 57164) [x86_64-darwin16]

 

1.2 接下來我們來看看服務器上的ruby版本 (建議用rvm)

RVM:Ruby Version Manager, Ruby版本管理器,包括Ruby的版本管理和Gem庫管理(gemset)

1.2.1 安裝rvm

curl -L get.rvm.io | bash -s stable 

注意: 如果安裝結尾出現, 執行下面修復命令

 

rvm fix-permissions

 

如果出現

  * To start using RVM you need to run `source /Users/vp/.rvm/scripts/rvm`
    in all your open shell windows, in rare cases you need to reopen all shell windows.

執行下面命令, 再去安裝

source ~/.rvm/scripts/rvm

1.2.2 加載文件,測試是否安裝正常(按照提示操作)

source ~/.bashrc
source ~/.bash_profile
source ~/.profile
rvm -v

rvm 1.29.2 (latest) by Michal Papis, Piotr Kuczynski, Wayne E. Seguin [https://rvm.io/]

 

1.2.3 獲取rvm列表, 就能看到最新ruby版本了, 如果與你機器的ruby版本不一致可以選擇升級(⊙o⊙)哦

rvm list known

 

 

 

1.3 安裝最新ruby(當前如果當前ruby版本夠用你可以選擇不安裝)

rvm install 2.4

  1.3.1 安裝的過程需要手動按一下enter(回車)鍵

 

  1.3.2 按完enter之后系統還會提示要你開機密碼做驗證

 

注意: 如果安裝失敗提示

Error running 'requirements_osx_port_libs_install curl-ca-bundle automake libtool libyaml libffi libksba',showing last 15 lines of /Users/acewill/.rvm/log/1468253599_ruby-2.3.0/package_install_curl-ca-bundle_automake_libtool_libyaml_libffi_libksba.log

原因是需要安裝Homebrew

ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

安裝完之后在跳到【1.3】

 

1.3.3 檢查版本是否安裝成功

ruby -v

 

2-> 更換gem源
gem是一個管理Ruby庫和程序的標准包,它通過Ruby Gem(如 http://rubygems.org/ )源來查找、安裝、升級和卸載軟件包, 所以一定確保有ruby環境才能下面的操作.

國內人員不使用git自帶的,因為自帶的gem源是國外的網址,taobao為我們提供了一個很不錯的'gem源', 現在的taobao源團隊已經維護使用ruby-china, 所以現在推薦使用ruby-china

// 移除舊的
gem sources --remove https://rubygems.org/
// 添加新的
gem sources --add https://ruby.taobao.org/
// gem sources -ahttps://gems.ruby-china.org/  // 查看當前'' gem sources -l

 

3-> 更新升級gem

sudo gem update -n /usr/local/bin cocoapods -v 1.0.1

注意1: 如果升級提示 ERROR:  While executing gem ... (OpenSSL::SSL::SSLError)

可能是你的ruby版本問題  或者是  源不對 回到【1】

 

4-> 安裝

4.1 安裝cocoapods

sudo gem install -n /usr/local/bin cocoapods

注意1: 如果升級提示

While executing gem ... (Gem::DependencyError)
Unable to resolve dependencies: cocoapods requires cocoapods-core (= 1.2.1), cocoapods-downloader (< 2.0, >= 1.1.3), cocoapods-trunk (< 2.0, >= 1.2.0), molinillo (~> 0.5.7), xcodeproj (< 2.0, >= 1.4.4), colored2 (~> 3.1), ruby-macho (~> 1.1)

可能你的gem版本問題, 執行下面這句

sudo gem update --system

成功之后在返回4.1 

 

注意2:

GemWrappers: Can not wrap missing file: pod
GemWrappers: Can not wrap missing file: sandbox-pod

表示你的 gem出問題了執行下面的命令

gem uninstall rubygems-update

  

4.2 查看本地所有cocoapod的版本列表

gem list

 4.3 如果某個版本不需要可卸載

sudo gem uninstall cocoapods -v 0.35.0
sudo gem uninstall cocoapods-core -v 0.35.0
sudo gem uninstall cocoapods // 卸載所有

 

5-> pod初始化

pod 需要從指定的源中獲取到第三方庫, 所以需要對一個'pod源' 進行初始化, 把所有的庫映射到本地

5.1 更換repo鏡像為國內服務器 

// 移除現有的鏡像
pod repo remove master
// 添加新鏡像源
pod repo add master https://gitcafe.com/akuandev/Specs.git
pod repo update

已知道的源有

https://gitcafe.com/akuandev/Specs.git 
http://git.oschina.net/akuandev/Specs.git 
https://git.coding.net/hging/Specs.git 

注意如果提示

MydeMacBook-Pro:~ taurin$ pod repo add master https://gitcafe.com/akuandev/Specs.git
[!] To setup the master specs repo, please run `pod setup`.

請執行5.2

 

5.2 初始化第三方庫信息: 把第三方庫映射到本地

pod setup

 注意如果提示

error: RPC failed; curl 18 transfer closed with outstanding read data remaining
fatal: The remote end hung up unexpectedly
fatal: early EOF
fatal: index-pack failed
[!] /usr/bin/git clone https://github.com/CocoaPods/Specs.git master --progress

Cloning into 'master'...
remote: Counting objects: 1975217, done.
remote: Compressing objects: 100% (401/401), done.
error: RPC failed; curl 18 transfer closed with outstanding read data remaining
fatal: The remote end hung up unexpectedly
fatal: early EOF
fatal: index-pack failed

執行下面指令

cd ~/.cocoapods/
sudo -rm -rf ~/.cocoapods/ 

返回到 5.2

 

5.3 查找第三方庫

pod search tt

注意: 如果在查找的過程中出現Unable to find a pod with name, author, summary, or description matching `tt`

解決辦法: 需要把以前查找過的json文件刪掉

目錄地址/Users/taojian/Library/Caches/CocoaPods/search_index.json

5.4 更新pod映射庫

pod repo update

5.5 其他操作

  5.4.1 查看當前pod版本

pod --version

 5.4.2 查看repo

pod repo list

  

6-> 創建podfile文件集成第三方庫

6.1 新建Podfile

vim Podfile

 

6.2 編輯podfile文件

- 輸入i:進入編輯狀態
- 輸入dd:刪除當前行
- 按ESC:退出編輯模式
- 輸入:wq:保存並退出


6.3 Podfile文件的格式

以前你這么寫是可以的, 應該是cocoapods0.35.0版本

```objc
platform :ios, '8.0'
pod '框架名字'
pod '框架名字', '~> 版本號'
```

 

1.0.0以后如果你還是那么寫是報錯的, pod不成功的, 需要下面的寫法 (推薦這么寫)

platform :ios, '8.0'
use_frameworks!

// SwiftAFNetwoking 是你APP里targets 名如下圖所示
target 'SwiftAFNetwoking' do

pod 'AFNetworking', '~> 2.6'

end 

 

6.4 解析Podfile,安裝第三方框架

pod install

 

6.5 升級第三方框架

pod update

 

6.6 手動創建podfile文件不用vim命令

直接在已有的項目里把podfile文件復制過來, 利用txt編輯器修改里面的內容, 最后pod install這樣也可以的

 

7-> 出現的問題、與解決辦法: 

7.1. pod install 之后發現沒有xcworkspace文件

解決辦法

// 移除cocoapods
1. sudo gem uninstall cocoapods // 可能會讓你選擇一個版本
// 再安裝cocoapods
2. sudo gem install -n /usr/local/bin cocoapods --pre
// 再install
3. pod install

 

7.2. 但如果cocoapod升級版本以后你會發現上面寫的會提示

解決辦法: 因為cocoapod 格式改了如下

platform :ios, '8.0'
use_frameworks!

// SwiftAFNetwoking 是你APP里targets 名如下圖所示
target 'SwiftAFNetwoking' do

pod 'AFNetworking', '~> 2.6'

end 

再pod install

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM