CocoaPods安裝和使用201712


CocoaPods安裝使用詳解

2017.12

 

 

首先,很有必要了解一下CocoaPods、Ruby和RubyGems,以及它們之間的關系。

 

CocoaPods是第三方庫的輔助管理工具,依賴於Ruby。

 

Ruby是一種簡捷的面向對象腳本語言。

 

RubyGems相當於Ruby的一個管理工具。

 

 

 

以下幾個官網有必要看看,

https://cocoapods.org

https://www.ruby-lang.org/en/

http://guides.rubygems.org

 

  

 

---------------------------------------CocoaPods------------------------------

 

CocoaPods官網地址:

https://cocoapods.org

 

簡介

    CocoaPods是Swift和Object-C Cocoa工程的輔助管理工具。

 

 

安裝

CocoaPods是使用Ruby構建的,並且可以使用OS X上默認的Ruby進行安裝,官方建議使用默認的ruby,

不用升級RubyGems和Ruby,如果真的需要,可參考本文后半部分

 

 

查看gem的資源下載地址(如果添加過其它地址,都會顯示出來)

gem source –l

輸出結果為*** CURRENT SOURCES *** https://rubygems.org/

而這個地址我們訪問不到,所以需要換掉。

 

刪除無用的資源地址

gem sources --remove https://rubygems.org/

 

添加可用的地址

gem sources -a https://gems.ruby-china.org/

 

 

在此要感謝https://ruby-china.org平台的貢獻 

 

然后安裝cocoapods

sudo gem install cocoapods

 

此時可能會出現錯誤

ERROR:  SSL verification error at depth 1: unable to get local issuer certificate (20)

ERROR:  You must add /O=Digital Signature Trust Co./CN=DST Root CA X3 to your local trusted store

解決方法:gem source –l查看地址是否有錯誤,有則改正;如果沒錯嘗試使用sudo gem install -n /usr/local/bin cocoapods

 

ERROR:  While executing gem ... (OpenSSL::SSL::SSLError)

    hostname "upyun.gems.ruby-china.org" does not match the server certificate的錯誤,

解決方法:可嘗試把source移除再重新添加,然后再執行sudo gem install cocoapods或sudo gem install -n /usr/local/bin cocoapods

 

安裝完成后,執行

pod setup

此操作時間較長,也可以現在不執行這句,不過在之后的使用中仍然會有setup操作。

 

 

CocoaPods的使用

以下使用都是在工程根目錄進行。

 

在終端中cd到工程根目錄,

 

創建Podfile

touch Podfile

 

打開Podfile

open –e Podfile

 

搜索需要用的第三方庫,如果已經在Podfile中添加了可忽略此步

pod search 庫名

 

出現問題

[!] Unable to find a pod with name, author, summary, or description matching `庫名`

[!] Skipping `0` because the podspec contains errors.

解決方法:在工程根目錄執行

rm ~/Library/Caches/CocoaPods/search_index.json

再進行搜索

 

將需要的第三方庫按照指定格式添加到Podfile中保存

 

在工程中安裝Pods

pod install

 

出現問題

     - Use the `$(inherited)` flag, or

 - Remove the build settings from the target.

解決方法:在Build Settings -> Other linker flags 中添加$(inherited)完成之后運行pod update

 

出現問題

[!] Invalid `Podfile` file: syntax error, unexpected keyword_end, expecting end-of-input.

[!] Smart quotes were detected and ignored in your Podfile. To avoid issues in the future, you should not use TextEdit for editing it. If you are not using TextEdit, you should turn off smart quotes in your editor of choice.

解決方法:Podfile格式不對,修改Podfile(空行沒什么影響),格式參考:

platform :ios, '8.0'

target 'carsharing' do

pod 'AFNetworking', '~> 3.1.0'

end

 

 

查看CocoaPods版本

pod --version

 

 

 

更新CocoaPods,再次安裝即可

sudo gem install cocoapods

 

 

有關CocoaPods的主要操作

 

 

 

---------------------------------------更新RubyGems------------------------------

查看rubygems版本

gem -v

 

 

更新rubygems版本

sudo gem update –system

 

 

 

 

---------------------------------------更新Ruby------------------------------

 

更新ruby

有時候會由於ruby版本較低導致出錯,比如更新完RubyGems后,在終端運行sudo gem update時出錯,

 

 

查看ruby當前版本

ruby –v

 

 

 

更新需要用到第三方的管理工具,ruby官網介紹的有幾種工具,可以使用homebrew進行更新,網址:

https://www.ruby-lang.org/en/documentation/installation/#homebrew

 

 

Homebrew工具:

 

 

進入Homebrew官網,可以看到Homebrew的安裝方法

https://brew.sh/index_zh-cn.html

 

 

在終端運行

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"安裝homebrew,

 

 

等待執行完,運行

brew install ruby

 

至此出現了Error: Xcode alone is not sufficient on Sierra.的錯誤

 

按照提示執行

xcode-select –install

 

 

 

等待完成后執行

brew install ruby

安裝完查看ruby版本發現沒有更新,重啟終端即可

 

 

 ---END

 


免責聲明!

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



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