iOS CocoaPods集成&使用

什么是CocoaPods

CocoaPods是iOS项目的依赖管理工具。一般开发iOS项目时都需要使用CocoaPods来管理第三方组件以及私有库的组件。

https://guides.cocoapods.org

CocoaPods安装

CocoaPods安装可以使用Mac系统自带的RubyGems进行安装。

在安装CocoaPods之前一般需要更新Ruby镜像:

1

2

3

// 移除淘宝源

gem sources --remove  https://ruby.taobao.org/ 

gem source -a https://gems.ruby-china.com

打开Terminal,然后输入如下命令进行安装:

1

sudo gem install cocoapods

CocoaPods使用

Podfile

Pofile文件详细描述了一个或多个工程中targets的依赖关系。

一个podfile配置的例子:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

source 'https://github.com/CocoaPods/Specs.git'

platform :ios, '6.0'

inhibit_all_warnings!

xcodeproj 'MyProject'

pod 'ObjectiveSugar''~> 0.5'

target :test do

  pod 'OCMock''~> 2.0.1'

end

post_install do |installer|

  installer.pods_project.targets.each do |target|

    puts #{target.name}

  end

end

Dependencies(依赖项)

Podfile描述用于各个用户target中的依赖项

  • pod 是声明指定依赖的方法
  • podspec 提供了一个简单的API用于创建podspec
  • target 允许在工程中限定依赖项只在指定的targets中生效

pod

指定工程的一个依赖。一个依赖项通过pod名和可选的版本号来声明。

1

2

3

4

5

6

// 忽略版本号,指定到最新的版本

pod 'YYModel'

// 固定某一个版本号

pod 'YYModel''1.0.0'

对于版本号的操作除了指定与不指定,可以做如下操作:

>0.1 高于0.1的任何版本

>=0.1 版本0.1和任何更高版本

<0.1 低于0.1的任何版本

<=0.1 版本0.1和任何较低的版本

~>0.1.2 版本0.1.2到0.2版本之间,不包括0.2

Build Configurations(编译配置)

默认情况下,依赖项会被安装在所有target的build configuration中。为了调度或者处于其它原因,依赖项只能在指定的target中被启用。

1

pod 'Doraemonkit''1.0.0', :configurations => ['Debug','ADHoc']

Subspec

当申明安装一个Pod时,它将安装所有定义在podspec里面默认的subspec。

1

2

3

4

5

6

// 可以这样申明

pod 'QueryKit/Attribute'

// 也可以这样申明

pod 'QueryKit', :subspec => ['Attribute''QuerySet']

Using the files from a local path(使用本地文件)

如果想用一个自己开发的本地的Pod,可以使用 path 选项:

1

pod 'IMModule', :path => '/Users/Desktop/Work/Project/IMModule'

From a podspec in the root of a library repository(引用仓库根目录的podspec)

使用仓库中的 master 分支:

1

pod 'AFNetworking', :git => 'https://github.com/gowalla/AFNetworking.git'

使用仓库中的其它分支:

1

pod 'AFNetworking', :git => 'https://github.com/gowalla/AFNetworking.git', :branch => 'dev'

使用仓库中的某个tag:(推荐使用)

1

pod 'AFNetworking', :git => 'https://github.com/gowalla/AFNetworking.git', :tag => '0.7.0'

使用仓库中的某一个提交纪录:

1

pod 'AFNetworking', :git => 'https://github.com/gowalla/AFNetworking.git', :commit => '082f8319af'

猜你喜欢

转载自blog.csdn.net/yezuiqingxin/article/details/120367804