CocoaPods安装使用详解html
2017.12ios
首先,颇有必要了解一下CocoaPods、Ruby和RubyGems,以及它们之间的关系。git
CocoaPods是第三方库的辅助管理工具,依赖于Ruby。github
Ruby是一种简捷的面向对象脚本语言。json
RubyGems至关于Ruby的一个管理工具。xcode
如下几个官网有必要看看,ruby
https://www.ruby-lang.org/en/ide
---------------------------------------CocoaPods------------------------------
CocoaPods官网地址:
简介
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