创建pod库-podspec
#cocoapods #依赖管理 #podspec

pod lib
命令是用来创建 develop pod 库:pod lib create
: 创建一个pod库pod lib lint
: 验证一个pod库
首先在项目目录下执行命令:
pod lib create [podname]

经过一系列的配置(平台,开发语言,测试框架,是否创建Demo),就完成了pod库的创建。
在项目目录下,生成了Demo,源码目录以及配置文件:

- *.podspecpodspec是当前pod库的描述文件
- ExampleDemo 工程
- pod库目录 (SYDCentralPivot)pod库源码目录
- LICENSE开源许可证
- README.md

在
podfile
中可以看到Demo工程以相对路径的方式引入了SYDCentralPivot
库。此时,SYDCentralPivot
属于 Development Pod
,在 Development Pods
目录下,而不是在Pods
目录下。Development Pod
可以直接修改源文件,并在主工程中编译运行。而 普通 Pod
是不可以的直接将需要的源文件拖到
SYDCentralPivot/Class
文件夹下; 在 podspec
文件的配置中,源文件都在该目录下s.source_files = 'SYDCentralPivot/Classes/**/*'
执行
pod install
后,源文件就会出现在 Development Pods
目录下:
在 Demo 工程中,调用pod库的代码

podspec
文件是对pod库命名,地址,版本,源文件,资源文件以及依赖的具体描述。- versionpod库通过tag控制版本,当前 `podspec`文件中的version应该与最新的tag保持一致
- summary,descriptionpod 库的描述
- homepage远端库主页地址
- source远端仓库地址
- source_files源文件路径
- resource_bundles资源文件路径
- public_header_files公共头文件路径
- dependency对于其他pod库的依赖
- frameworks依赖的系统框架
- vendored_frameworks依赖的非系统框架
- libraries依赖的系统库
- vendored_libraries依赖的非系统的静态库
Pod::Spec.new do |s|
s.name = 'SYDCentralPivot'
s.version = '1.1.0'
s.summary = 'A Simple Factory and Router for UIViewController,ServiceModel and some other object'
# This description is used to generate tags and improve search results.
# * Think: What does it do? Why did you write it? What is the focus?
# * Try to keep it short, snappy and to the point.
# * Write the description between the DESC delimiters below.
# * Finally, don't worry about the indent, CocoaPods strips it!
s.description = 'Factory and Router'
s.homepage = 'https://github.com/ExistOrLive/SYDCentralPivot'
# s.screenshots = 'www.example.com/screenshots_1', 'www.example.com/screenshots_2'
s.license = { :type => 'MIT', :file => 'LICENSE' }
s.author = { 'ExistOrLive' => '[email protected]' }
s.source = { :git => 'https://github.com/ExistOrLive/SYDCentralPivot.git', :tag => s.version.to_s }
# s.social_media_url = 'https://twitter.com/<TWITTER_USERNAME>'
s.ios.deployment_target = '9.0'
s.source_files = 'SYDCentralPivot/Classes/**/*'
# s.resource_bundles = {
# 'SYDCentralPivot' => ['SYDCentralPivot/Assets/*.png']
# }
# s.public_header_files = 'Pod/Classes/**/*.h'
# s.frameworks = 'UIKit', 'MapKit'
# s.dependency 'AFNetworking', '~> 2.3'
end
在修改了
podspec
文件后,记得使用pod lib lint
和 pod spec lint
验证修改是否有效。如果希望在Pod库中添加资源文件(图片,音频以及其他一些配置文件等),就需要用到配置
resource_bundles
s.resource_bundles = {
'SYDCentralPivot' => ['SYDCentralPivot/Assets/*.png']
}
而在代码中,并不能够通过MainBundle访问这些资源文件,因为这些文件并不在主工程下,而是在主工程依赖的库中。
// 通过pod库中定义的类获取pod库对应的bundle
let bundle = Bundle(for: ZLServiceManager.self)
// 在通过bundle获取文件内容或者路径
let configPath = bundle.path(forResource: "ZLServiceConfig", ofType: "plist")
这里推送到Github,首先在Github上创建一个仓库,将仓库克隆到本地,将pod库复制到本地的仓库中。
- 修改
podspec
文件,homepage 应该设置为远端库主页地址source 设置为远端库地址version 与 tag 保持一致 - 提交到远端库git add .git commit -m “xxx”git remote add origin 远程代码仓库地址git push origin master 或者 git push -u origin master(一般第一次提交用)git tag 版本号/git tag -a 版本号 -m “version 版本号”(注:这里的版本号必须和podspec里写的版本号一致)git tag 查看版本号是否提交成功git push - -tags
- 可以直接使用地址依赖pod库pod 'SYDCentralPivot', :git => 'https://github.com/ExistOrLive/SYDCentralPivot.git'
当把
podspec
文件推送到公共pod repo后,你就可以通过 pod search
搜索该pod以及直接使用名 字安装pod库pod trunk [COMMAND]
是用来和 Cocoapods API
交互的命令.
- 首次推送pod,需要注册账号

pod trunk push [PATH]
在推送之前,pod 还会验证podspec
文件是够有效。如果不通过的话,就无法推送上去因此在推送之前必须使用pod lib lint
或则pod spec lint
去验证podspec
文件后期pod库更新版本后,同样使用pod trunk push
推送。
subspec
需要指定子库的源文件和头文件 s.subspec 'Core' do |core|
core.ios.deployment_target = '9.0'
core.source_files = 'SYDCentralPivot/Classes/Core/*'
core.public_header_files = 'SYDCentralPivot/Classes/Core/*.h'
end
最近更新 11mo ago