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

原文地址:https://www.cnblogs.com/aitdsr/p/8146434.html