CocoaPods的原理、安装及使用(绝对干货)

CocoaPods是iOS项目的依赖管理工具,该项目源码在Github上管理。开发iOS项目不可避免地要使用第三方开源库,CocoaPods的出现使得我们可以节省设置和第三方开源库的时间。在使用CocoaPods后,我们只需要把用到的开源库放到一个名为Podfile的文件中,然后执行pod install。Cocoapods就会自动将这些第三方开源库的源码下载下来,并且为我们的工程设置好响应的系统依赖和编译参数。

一、CocoaPods原理。

CocoaPods的工作主要是通过ProjectName.xcworkspace来组织的,在打开ProjectName.xcworkspace文件后,发现Xcode会多出一个Pods工程。

1.库文件引入及配置:

库文件的引入主要由Pods工程中的Pods-ProjectName-frameworks.sh脚本负责,在每次编译的时候,该脚本会帮你把预引入的所有三方库文件打包的成ProjectName.a静态库文件,放在我们原Xcode工程中Framework文件夹下,供工程使用。

2.Resource文件:

Resource资源文件主要由Pods工程中的Pods-ProjectName-resources.sh脚本负责,在每次编译的时候,该脚本会帮你将所有三方库的Resource文件copy到目标目录中。

3.依赖参数设置:

在Pods工程中的的每个库文件都有一个相应的SDKName.xcconfig,在编译时,CocoaPods就是通过这些文件来设置所有的依赖参数的,编译后,在主工程的Pods文件夹下会生成两个配置文件,Pods-ProjectName.debug.xcconfig、Pods-ProjectName.release.xcconfig。

二、安装CocoaPods

1、打开终端,移除现有 Ruby 默认源(因为ruby 的软件源 https://rubygems.org 使用的是亚马逊的云服务,所以被墙了,需要更新一下 ruby 的源,使用下面2行代码将官方的 ruby 源替换成国内淘宝的源)。

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

$ gem sources -a https://ruby.taobao.org/

2、验证新源是否替换成功

$ gem sources -l

如果替换成功,则显示为:

*** CURRENT SOURCES ***

https://ruby.taobao.org/

注:此处为更新

由于淘宝的ruby源已经好久没更新了,感觉已经被放弃,故我们可以用

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

3、安装 CocoaPods

$ sudo gem install cocoapods

$ pod setup

注意:苹果系统升级 OS X EI Capitan 后安装改为:

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

$ pod setup

安装可能遇到一下问题,下面列出几个我遇到过的:

(1).

Performing a deep fetch of the `master` specs repo to improve future performance

因为pod steup的时候创建master这个库,没成功,之后就算移除镜像重新安装的话 默认是从matser库里获取,导致安装不成功。

解决办法是移除master库,重新创建。

pod repo upadte --verbose

rm -rf ~/.cocoapods/repos/master

重新setup(需要较长时间)

pod setup

(2)

[!] /usr/bin/git clone https://github.com/CocoaPods/Specs.git master --progress

Cloning into 'master'...

fatal: unable to access 'https://github.com/CocoaPods/Specs.git/': SSLRead() return error -9806

这个可能跟网络有关吧,可以重新setup

pod setup

也可以直接通过连接clone

1.通过finder的前往文件夹进行查看      ~/.cocoapods/repos

2.通过终端,进入到  ~/.cocoapods/repos

3.然后通过  git clone https://github.com/CocoaPods/Specs.git

(3)

Cloning into 'master'...

remote: Counting objects: 1167221, done.

remote: Compressing objects: 100% (222/222), done.

error: RPC failed; curl 56 SSLRead() return error -36| 384.00 KiB/s

fatal: The remote end hung up unexpectedly

fatal: early EOF

fatal: index-pack failed

终端输入

xcode-select -p

查看Xcode本地路径 (替换下面的)

sudo xcode-select -switch  /Applications/Xcode 7.3.1.app/Contents/Developer

然后setup。

(4).

$ pod setup

Setting up CocoaPods master repo

[!] /usr/bin/git clone https://github.com/CocoaPods/Specs.git master

Cloning into ‘master’…

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

试过以上所有解决办法,还是没有安装成功,重新安装(终极方法)。

首先,删除.cocoapods目录,在终端输入以下命令:

sudo rm -rf ~/.cocoapods/

然后,重新下载安装,在终端输入以下命令:

pod setup

(5)出现ERROR:  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)

执行

sudo gem update --system

在终端中输入:

sudo nvram boot-args="rootless=0"; sudo reboot

然后又出现   ERROR:  While executing gem ... (Errno::EPERM)

Operation not permitted - /usr/bin/xcodeproj

然后你的电脑会重启

之后再输入

sudo gem install cocoapods -V

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

三、CocoaPods的使用。

(1)可以用CocoaPods的搜索功能验证一下。在终端中输入:

pod search AFNetworking

(2)具体使用步骤(此处项目名称为“podTest“):

方法一:

<1> cd 项目文件的位置;

<2> 在项目文件的根目录下,执行如下命令:

pod init

此命令会自动在项目文件的根目录下,自动创建一个Podfile文件。

然后,在其中添加代码

pod ‘AFNetworking’, ‘~>3.1.0’

<3> 执行如下命令

pod install

方法2:

<1>.终端中,cd到项目总目录

<2>.终端输入 vim Podfile ,键盘输入 i,进入编辑模式,输入

platform :ios, '8.0'

pod ‘AFNetworking’, ‘~>3.1.0’

然后按Esc,并且输入“ :”号进入vim命令模式,然后在冒号后边输入wq,回车后发现PodTest项目总目录中多一个Podfile文件。

<3>.终端cd到项目总目录,然后输入 pod install.

注:这时候可能会报错,如

[!] The dependency `AFNetworking (~> 3.1.0)` is not used in any concrete target.

[!] Your Podfile has had smart quotes sanitised. 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文件,改为以下两种格式都可以。

写法1:

platform :ios, '8.0'

#use_frameworks!个别需要用到它

target 'Podtest' do

pod 'AFNetworking', '~> 3.1.0''

end

写法2:

platform :ios, '8.0'

#use_frameworks!个别需要用到它

def pods

pod 'AFNetworking', '~> 3.1.0''

end

target 'Podtest' do

pods

end

你可能感兴趣的:(CocoaPods的原理、安装及使用(绝对干货))