封装cocoapods SDK私有库和公有库
一、环境配置
1、安装cocoapods的打包插件
sudo gem install cocoapods-packager
2、注册cocoapods账户
1)使用trunk方式注册CocoaPods
$ pod trunk register [email protected] ‘名字’ --verbose
或者
$ pod trunk register [email protected] 名字 --verbose
eg:
$ pod trunk register [email protected] ‘JolieYa’
[!] Please verify the session by clicking the link in the verification email that has been sent to [email protected]
2) 点击邮件中的链接确认即注册成功,查看是否注册成功:
iMac:~ gongairong$ pod trunk me
- Name: JolieYa
- Email: [email protected]
- Since: December 14th, 20:48
- Pods: None
- Sessions:
- December 14th, 20:48 - April 21st, 2018 20:53. IP: 183.14.30.167
二、创建公有Pod库
1)创建远端公有仓库
2)将远程仓库备份到本地
$ git clone https://github.com/Kxx*/frameworkSDK.git
3) 新建个文件夹,存放已经封装好的动态或者静态.framework 和.bundle,如下
4) 进入仓库,推送.framework和.bundle到git服务器
$ cd ./frameworkSDK/
$ git add .
$ git commit -m “提交Framework和Bundle”
$ git push
5)打版本
$ git tag 1.0.1 # 新建tag
$ git push --tags # 推送到远端
$ git tag -d 0.1.0 # 删除本地tag 0.1.0
$ git push origin --delete tag 0.1.0 # 删除远端tag 0.1.0
6)配置.podspec文件
cd到你项目的目录,执行命令新建.podspec文件
$ pod spec create frameworkSDK
Specification created at frameworkSDK.podspec
Pod::Spec.new do |s|
s.name = "frameworkSDK" #名称:pod search搜索的关键词,一定要和.podspec的名称一样,否则报错
s.version = "0.0.1" # 版本号:与打的tag一致,to_s:返回一个字符串
s.summary = "Demo for Pod" # 项目简介:必须比description短
s.description = <<-DESC
描述
DESC
s.homepage = "https://github.com/XX/frameworkSDK" # 项目主页地址:必须填写可以访问到的地址
# s.screenshots = "www.example.com/screenshots_1", "www.example.com/screenshots_2" # 截图
s.license = 'MIT' # 许可证,开源协议
s.author = { "XX" => "[email protected]" } # 作者信息
s.source = { :git => "https://github.com/XX/frameworkSDK.git", :tag => "0.0.1" } # 项目源码所在地址:这里不支持ssh的地址,验证不通过,只支持HTTP和HTTPS,最好使用HTTPS
# s.social_media_url = 'https://twitter.com/<twitter_username>' # 社交网址
s.ios.deployment_target = '8.0' # 支持的pod最低版本
s.platform = :ios, '8.0' # 项目支持平台及版本
s.requires_arc = true #是否使用ARC,如果指定具体文件,则具体的问题使用ARC
s.ios.vendored_frameworks = "xxx/**/*.framework" # 私有库写法
s.ios.vendored_libraries = "xxx/**/*.a” # 私有库写法
s.vendored_frameworks = 'frameworkSDK/*.{framework}' # 放入封装好的.framework地址
s.resource = 'frameworkSDK/*.bundle' # .bundle资源文件
s.source_files = 'frameworkSDK/Classes/**/*' # 代码源文件地址,**/*表示Classes目录及其子目录下所有文件,如果有多个目录下则用逗号分开,如果需要在项目中分组显示,这里也要做相应的设置
s.resource_bundles = {
'PodTestLibrary' => ['frameworkSDK/Assets/*.png']
} #资源文件地址
s.public_header_files = 'frameworkSDK/Classes/**/*.h' # 公开头文件地址
s.frameworks = 'UIKit' #所需的framework,多个用逗号隔开
s.libraries = 'sqlite3' #引入libsqlite3.tbd库
# s.dependency 'AFNetworking', '~> 2.3' # 项目依赖库,不能依赖未发布的库;如果有多个需要填写多个s.dependency
#将文件按文件夹分类
#s.subspec 'Proto' do |sp|
#sp.source_files = '**/proto/**/*'
#sp.requires_arc = false #proto文件夹下的文件使用MRC
#end
end
source_files写法及含义
"frameworkSDK/*
"frameworkSDK/frameworkSDK/*.{h,m}"
"frameworkSDK/**/*.h"
*表示匹配所有文件
*.{h,m}表示匹配所有以.h和.m结尾的文件
**表示匹配所有子目录
s.source常见写法
s.source = { :git => "https://github.com/XX/frameworkSDK.git", :commit => "68defea" }
s.source = { :git => "https://github.com/XX/frameworkSDK.git", :tag => 1.0.0 }
s.source = { :git => "https://github.com/XX/frameworkSDK.git", :tag => s.version }
commit => "68defea" 表示将这个Pod版本与Git仓库中某个commit绑定
tag => 1.0.0表示将这个Pod版本与Git仓库中某个版本的comit绑定
tag => s.version表示将这个Pod版本与Git仓库中相同版本的comit绑定
7) 验证podspec文件
验证:
$ pod lib lint
或者
$ pod lib lint frameworkSDK.podspec
或者
$ pod lib lint frameworkSDK.podspec --verbose
$ pod spec lint
$ pod spec lint frameworkSDK.podspec --verbose
$ pod spec lint frameworkSDK.podspec --verbose --use-libraries --allow-warnings
$ pod lib lint 文件名称.podspec
–verbose会给出详细的信息。
–allow-warnings允许警告,默认有警告的podspec会验证失败。
–fail-fast 遇到错误马上停止,默认会完成全过程再停止。
–use-libraries 如果项目中引用了.a、.framework,在验证和提交时需要加。
–sources 如果此podspec 依赖了其他不包含在官方specs里的pod,则用它来指明源,比如依赖了某个私有库。多个值以逗号分隔。
发布:
$ pod trunk push ./frameworkSDK.podspec #提交到Cocoapods
$ pod trunk me # 查看下pod信息
如有警告或者错误请重新检查你的编写正确性, 如果没有问题会出现:
8)验证Pod库,验证是否成功
$ pod setup
$ pod search frameworkSDK
若一直查找不到该pod库,执行下面代码删除查找的历史索引文件
$ rm ~/Library/Caches/CocoaPods/search_index.json
$ pod search 库名
8) 更新维护
修改如下:
podspec中的版本号, 并重新打tag上传, 再进行新一轮的验证podspec文件和发布
三、创建私有Pod库
1) CocoaPods提供了pod lib create命令创建库项目
$ pod lib create 库名
zhijianuandeMBP:~ YuanZhiJian$ pod lib create 'frameworkSDK'
Cloning `https://github.com/CocoaPods/pod-template.git` into `frameworkSDK`.
Configuring frameworkSDK template.
------------------------------
To get you started we need to ask a few questions, this should only take a minute.
If this is your first time we recommend running through with the guide:
- http://guides.cocoapods.org/making/using-pod-lib-create.html
( hold cmd and double click links to open in a browser. )
What language do you want to use?? [ Swift / ObjC ]
> ObjC
Would you like to include a demo application with your library? [ Yes / No ]
> Yes
Which testing frameworks will you use? [ Specta / Kiwi / None ]
> None
Would you like to do view based testing? [ Yes / No ]
> No
What is your class prefix?
> RK
将上面生成的frameworkSDK. framework拷贝到生成的frameworkSDK 库项目的frameworkSDK目录下
先简单介绍下:
*.podspec:文件是私有库的配置文件
frameworkSDK:该文件夹是存放私有库的类和资源的地方
Example:是根据上图提示,我们选择创建的demo工程,如果选择No,则不会生成此工程
2) 编辑库的Spec描述文件*.podspec
同公有库,编辑好后,导入库到Example中
$ cd Example
$ pod install
或者
$ pod update --no-repo-update
3)验证pod配置文件
$ pod spec lint --sources='私有仓库repo地址,https://github.com/CocoaPods/Specs'
4) 推送SDK项目到GitHub仓库
Example测试项目运行成功并且验证配置文件成功后,上传git仓库
$ git remote add origin https://github.com/XX/frameworkSDK.git # 连接远程仓库并建了一个名叫:origin的别名
$ git remote #查看是否有关联远程库,没有需要设置下
$ git add .
$ git commit -a -m “第一次提交 版本为0.0.1”
$ git pull origin master --allow-unrelated-histories
$ git push origin master
// 打版本
$ git tag 0.0.1 # 提交版本号
$ git push origin 0.0.1 # push到远程分支
或者
$ git tag -a 0.1.0 -m “first release”
$ git push origin --tags
# --allow-unrelated-histories
# git pull origin maste会失败 ,提示:fatal: refusing to merge unrelated histories
# 原因是远程仓库origin上的分支master和本地分支master被Git认为是不同的仓库,所以不能直接合并,需要添加 --allow-unrelated-histories
5)创建Sepc管理库,并发布
创建步骤跟上面码云创建的git私有库同理
在终端执行Specs创建命令
$ pod repo add PrintSpecs https://github.com/XX/frameworkSDK.git #创建远程git仓库与本地cocoapods私有仓库PrintSpecs映射
$ pod repo push PrintSpecs frameworkSDK.podspec # 推送本地私有仓库到远端$ pod repo push 本地repo名 podspec名 --sources='私有仓库repo地址,https://github.com/CocoaPods/Specs' # 推送本地私有仓库到远端
发布成功后,去git看看frameworkSDK的git项目有没有提交成功
查看我们本地的Specs库:
直接Findle ->右键 -> 前往文件夹 -> 输入:~/.cocoapods/repos ->点击前往
6) 推送到CocoaPods官方库上
$ pod trunk push frameworkSDK.podspec
7)更新维护
注:若多人合作开发,在他的有这个仓库权限的前提下执行上述相同的代码添加这个远端库
仓库文件目录如下:
.cocopods
---repos
------master
------BY
---------BYYPrivatePodDemo
------------0.0.1
---------------BYPrivatePodDemo.podspec
------------0.0.2
---------------BYPrivatePodDemo.podspec
删除私有Pod库:
pod repo remove 管理podspec文件的库名
eg: pod repo remove frameworkSDK
在本地删除了私有的Pod库,可以通过以下加回:
pod repd add 管理podspec文件的库名
四、使用
1)公有pod库:项目的Podfile文件
pod ‘pod库名’,’~>版本号’
若公有pod库更新后,直接修改Podfile库的版本后,在Podfile文件目录下执行pod install即可
2) 私有pod库: 项目的Podfile文件
source ‘[email protected]:XXX/cocoapods-app-ios.git’ #私有库地址
source 'https://github.com/CocoaPods/Specs.git' #pod官方库
pod 'frameworkSDK', ‘~>2.1.9’
或者
platform :ios,'8.0'
target 'privateDemo' do
pod 'frameworkSDK',:git => 'https://github.com/XX/frameworkSDK.git' # 远程私有库
#pod 'frameworkSDK',:path => '../Lib/frameworkSDK' # 本地私有库
end
$ pod install --no-repo-update
若私有Pod库更新后,不用修改Podfile文件,在Podfile文件目录下执行pod update即可
3)查看SDK cocoapods信息
$ pod trunk info frameworkSDK
4)删除cocoapods库
$ pod trunk delete 库名 版本 # 删除指定版本
$ pod trunk deprecate 库名 # 整个POD和所有版本,将其设置为过期