CocoaPods是非常好用的一个iOS依赖管理工具,使用它可以方便的管理和更新项目中所使用到的第三方库,以及将自己的项目中的公共组件做成私有库交由它去管理。其工作原理就是在GitHub上面有个Spec Repo,它里面包括成千上万的podspec文件索引目录。然后我们在项目集成的时候可以通过它去找到我们需要的开源代码。
本文通过三个部分介绍通过CocoaPods搭建私有库的流程。
一、创建私有索引库Spec Repo
什么是Spec Repo?它是所有的Pods的一个索引,实际是一个Git仓库,remote端在GitHub上,里面存放着所管理的所有Pods的.podspec文件,当你使用了CocoaPods后它会被clone到本地的~/.cocoapods/repos目录下,可以进入到这个目录看到cocoapods文件夹就是公用Spec Repo了,目录的结构如下:
├── Specs
└── [SPEC_NAME]
└── [VERSION]
└── [SPEC_NAME].podspec
1. 创建远端私有索引库
在GitHub、Gitlab或公司的git服务器上创建一个私有仓库,本文测试时所有库都搭建在腾讯云上,私有Spec Repo取名为TestSpec,切记要初始化该仓库。
2. 本地添加私有索引库
pod repo add [Private Repo Name] [Your GitHub HTTPS clone URL]
pod repo add TestSpec https://git.dev.tencent.com/hxl1010/TestSpec.git
完成后效果为:
pod repo list命令查看如下:
备注:
- 可能会有不成功的情况,参考:http://blog.csdn.net/zhangphil/article/details/47981757 可以将SourceTree的Git换成系统内置的git 再执行此操作,就会提示输入用户名和密码了,这个时候输入gitlab的用户名和密码即可。
- 如果有其他合作人员共同使用这个私有Spec Repo的话在他有对应Git仓库的权限的前提下执行相同的命令添加这个Spec Repo即可。
二、创建私有库
1. 创建本地私有库
pod lib create 私有库名字:创建私有库的标准格式,会使用git-template默认的模板创建私有库
pod lib create TestPod
还可以通过--template-url参数,指定我们自己的私有库模板地 址
pod lib create --verbose --template-url='https://git.dev.tencent.com/hxl1010/TestGitTemplate.git' TestPod
按照提示,输入对应配置:
➜ Project pod lib create TestPod
Cloning `https://github.com/CocoaPods/pod-template.git` into `TestPod`.
Configuring TestPod template.
security: SecKeychainSearchCopyNext: The specified item could not be found in the keychain.
------------------------------
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:
- https://guides.cocoapods.org/making/using-pod-lib-create.html
( hold cmd and double click links to open in a browser. )
What platform do you want to use?? [ iOS / macOS ]
> iOS
What language do you want to use?? [ Swift / ObjC ]
> Swift
Would you like to include a demo application with your library? [ Yes / No ]
> Yes
Which testing frameworks will you use? [ Quick / None ]
> None
Would you like to do view based testing? [ Yes / No ]
> No
security: SecKeychainSearchCopyNext: The specified item could not be found in the keychain.
security: SecKeychainSearchCopyNext: The specified item could not be found in the keychain.
security: SecKeychainSearchCopyNext: The specified item could not be found in the keychain.
security: SecKeychainSearchCopyNext: The specified item could not be found in the keychain.
security: SecKeychainSearchCopyNext: The specified item could not be found in the keychain.
security: SecKeychainSearchCopyNext: The specified item could not be found in the keychain.
Running pod install on your new library.
Analyzing dependencies
Downloading dependencies
Installing TestPod (0.1.0)
Generating Pods project
Integrating client project
[!] Please close any current Xcode sessions and use `TestPod.xcworkspace` for this project from now on.
Pod installation complete! There is 1 dependency from the Podfile and 1 total pod installed.
[!] Automatically assigning platform `iOS` with version `9.3` on target `TestPod_Example` because no platform was specified. Please specify a platform for this target in your Podfile. See `https://guides.cocoapods.org/syntax/podfile.html#platform`.
Ace! you're ready to go!
We will start you off by opening your project in Xcode
open 'TestPod/Example/TestPod.xcworkspace'
To learn more about the template see `https://github.com/CocoaPods/pod-template.git`.
To learn more about creating a new pod, see `https://guides.cocoapods.org/making/making-a-cocoapod`.
2. 在GitHub或Gitlab等上创建远端仓库
3. 将本地私有库关联到远端库
在私有库主目录下执行如下命令:
git remote add origin https://git.dev.tencent.com/hxl1010/TestPod.git
4. 编写私有库代码并提交
4.1 修改TestPod.podspec文件
版本号(s.version)、简介(s.summary)、描述(s.description),如果依赖其他库则在s.dependency中添加依赖的第三方库或私有库,添加完依赖库以后需要在Example目录下执行pod update
Pod::Spec.new do |s|
//名称,pod search 搜索的关键词,注意这里一定要和.podspec的名称一样
s.name = 'TestPod'
//版本号,每一个版本对应一个tag
s.version = '0.1.0'
s.summary = '这里是私有库摘要'
s.description = <<-DESC
这里是私有库描述说明
DESC
//项目主页地址
s.homepage = 'https://git.dev.tencent.com/hxl1010/TestPod'
# s.screenshots = 'www.example.com/screenshots_1', 'www.example.com/screenshots_2'
//许可证
s.license = { :type => 'MIT', :file => 'LICENSE' }
//作者
s.author = { 'HXL' => 'cquhxl@163.com' }
//项目仓库地址
s.source = { :git => 'https://git.dev.tencent.com/hxl1010/TestPod.git', :tag => s.version.to_s }
//社交网址
# s.social_media_url = 'https://twitter.com/<TWITTER_USERNAME>'
s.ios.deployment_target = '8.0'
//源文件路径
s.source_files = 'TestPod/Classes/**/*'
//资源文件路径
# s.resource_bundles = {
# 'TestPod' => ['TestPod/Assets/*.png']
# }
# s.public_header_files = 'Pod/Classes/**/*.h'
# s.frameworks = 'UIKit', 'MapKit'
//依赖库
# s.dependency 'AFNetworking', '~> 2.3'
end
“*” 表示匹配所有文件
“**” 表示匹配所有子目录
4.2 编写代码
私有库源码建议放在 TestPod/TestPod/Classes中
4.3 提交代码
方法1:可通过SourceTree等工具提交代码并设置tag;
方法2:在项目根目录下通过终端提交
git add .
git commit -s -m "初始化项目"
git remote add origin https://git.dev.tencent.com/hxl1010/TestPod.git
git push origin master
git tag -m "第一版" 0.1.0
git push --tags
备注:
- tag要和在podspec中s.version保持一致,cocoapods内部会根据version去查找仓库中的tag进行匹配。
5. 校验私有库
在上传spec文件前我们可以做一个验证来节省时间。如果不先本地验证,直接远程验证,比较费时。在项目根目录下执行:
5.1 验证本地podspec文件
本地验证不会验证 s.source 中的tag
如果是公有库,只需要此命令即可
pod lib lint
如果该项目依赖其他私有库,需要添加私有仓库地址,通过--sources指定私有仓库地址
pod lib lint --sources='https://git.dev.tencent.com/hxl1010/TestSpec.git,https://github.com/CocoaPods/Specs'
5.2 验证远程podspec
远程验证会验证 s.source 中的tag,如果此时没有打上相应的标签则会报错
pod spec lint
如果依赖的库有私有库,需要加上私有库的地址
pod spec lint --sources=https://git.dev.tencent.com/hxl1010/TestSpec.git,https://github.com/CocoaPods/Specs.git
备注:
- pod spec lint相对于pod lib lint会更为精确,后者相当于只验证一个本地仓库,前者会同时验证本地仓库和远程仓库。
- 当代码里有警告时会验证失败,可通过参数--allow-warnings允许存在警告。
6. 发布私有库
如果私有库还依赖于其他的私有库,可以通过 --sources 来指定所有私有库的地址,这一点和"校验私有库"中的 --sources是一致的。
pod repo push TestSpec TestPod.podspec --sources='https://git.dev.tencent.com/hxl1010/TestSpec.git,https://github.com/CocoaPods/Specs'
终端中发布命令执行成功后:
➜ TestPod git:(master) pod repo push TestSpec TestPod.podspec --sources='https://git.dev.tencent.com/hxl1010/TestSpec.git,https://github.com/CocoaPods/Specs' --allow-warnings
Validating spec
-> TestPod (0.1.0)
- WARN | [iOS] swift: The validator used Swift `4.0` by default because no Swift version was specified. To specify a Swift version during validation, add the `swift_versions` attribute in your podspec. Note that usage of a `.swift-version` file is now deprecated.
- NOTE | xcodebuild: note: Using new build system
- NOTE | [iOS] xcodebuild: note: Planning build
- NOTE | [iOS] xcodebuild: note: Constructing build description
- NOTE | [iOS] xcodebuild: warning: Skipping code signing because the target does not have an Info.plist file and one is not being generated automatically. (in target 'App' from project 'App')
Updating the `TestSpec' repo
Adding the spec to the `TestSpec' repo
- [Add] TestPod (0.1.0)
Pushing the `TestSpec' repo
三、测试集成
在需要集成私有库的Podfile文件最顶部添加如下描述,然后执行pod install
platform :ios, '10.0'
source 'https://github.com/CocoaPods/Specs.git'
source 'https://git.dev.tencent.com/hxl1010/TestSpec.git' //增加该行是为了保证公有库的正常使用
use_frameworks!
target 'Test' do
pod 'TestPod'
pod 'DebugTool'
end