Gray's coding life.

优雅地使用Cocoapods

Word count: 955 / Reading time: 4 min
2018/06/15 Share

很多人都知道cocoapods,知道怎么写podfile,知道怎么引入库。下面来介绍下cocoapods的一些拓展用法。

安装Cocoapods

如果没有梯子的需要先执行以下命令,把下载资源替换成国内源。

1
2
gem sources --remove https://rubygems.org/
gem sources -a https://gems.ruby-china.org/

执行以下命令查看当前源,确保已经替换成功,否则再执行以上命令。

1
gem sources -l

安装cocoapods,后面执行setup的时候时间稍长,文件大约600M,建议使用版本1.5以上。

1
2
sudo gem install cocoapods
pod setup

Podfile

最基本的用法如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
target 'BaseFramework' do
# Uncomment the next line if you're using Swift or would like to use dynamic frameworks
# use_frameworks!

# Pods for BaseFramework

pod 'AFNetworking', '~> 3.0'

pod 'Masonry', '~> 1.0.2'

pod 'SDWebImage', '~>3.7'

target 'BaseFrameworkTests' do
inherit! :search_paths
# Pods for testing
end

target 'BaseFrameworkUITests' do
inherit! :search_paths
# Pods for testing
end

end

通过上面的方法可以引入已经上传到Git上面并且打好tag的库。如果想更新了代码,则需要先push代码到github,然后打好新的tag才能实现对podfile的支持。
那如果不想把代码放到github上呢?我希望更新代码之后直接用pod update命令就可以把更新了的代码库引入我的工程。
可以用下面的写法:

1
pod 'BaseFramework', :path =>'../BaseFrameworkDir'

这个写法的意思是执行路径../BaseFrameworkDir下的BaseFramework.podspec文件,按照podspec文件已经编写好的规则来导入文件到工程里。

podspec文件

这里建议用命令行生成podspec文件,格式和注释都比较规范。

1
pod spec create XXX

接下来看看生成的podspec文件里比较重要的内容

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
s.name         = "BaseFramework" #工程的名字
s.version = "0.0.1" #工程的版本
s.summary = "Gray's modularization BaseFramework." #工程的摘要
s.description = "Gray's BaseFramework demo" #工程的描述
s.homepage = "http://graydeng.BaseFramework" #工程的首页
s.license = "MIT" #工程的证书
s.author = { "Gray" => "denggray@163.com" } #工程的作者
s.ios.deployment_target = "8.0" #工程的编译版本
s.source = { :git => "http://graydeng/BaseFramework.git", :tag => "#{s.version}" } #工程的git地址
s.source_files = "Classes", "Classes/**/*.{h,m}" #工程需要引入的文件
s.exclude_files = "Classes/Exclude" #工程不需要引入的文件
s.public_header_files = "Classes/**/*.h" #工程需要暴露出来的头文件
s.resources = "Resources/*" #工程需要引入的资源文件(图片,xib等)
s.resource_bundles = {'Resources' => 'XXX.framework/Resources/XXX.bundle'} #工程需要引入的bundle
s.frameworks = "ImageIO" #工程依赖的framework
s.vendored_frameworks = [] #工程依赖的第三方framework
s.libraries = "iconv", "xml2" #工程依赖的library
s.vendored_libraries = [] #工程依赖第三方的library
s.requires_arc = true #工程是否用arc规则
s.dependency "AFNetworking", "~> 3.0" #工程依赖的第三方库

封装一个库最重要的还是source_files,把必须用到的文件导入到主工程。
其次就是资源文件resources、frameworks、libraries。
如果涉及到使用mrc的文件还需要设置一下requires_arc的参数

1
s.source_files  = "Classes/**/*.{h,m}"

这个配置表示在导入的时候会在Classes文件夹下找到所有的.h和.m文件并导入。

1
s.public_header_files = "Classes/**/*.h"

这个配置只在打包framework的时候起作用,选择暴露出来的头文件。

1
s.resources = "Resources/*"

这里配置工程的资源文件,包括图片、json文件、bundle文件等

1
s.frameworks = "ImageIO"

这个库依赖于系统库ImageIO,在pod入这个库的时候会带入这个系统库

1
s.libraries = "iconv", "xml2"

这个库依赖于系统Library iconv和xml2,在pod入这个库的时候会带入这个系统Library。注意这里Library的名字只取lib后面的字符。

1
s.dependency "AFNetworking", "~> 3.0"

工程依赖的第三方库,写法和podfile一致。

CATALOG
  1. 1. 安装Cocoapods
  2. 2. Podfile
  3. 3. podspec文件