Cocoapods 通常用来管理第三方库,当咱们本身封装了一个功能模块时也可使用 Cocoapods 发布给其余人使用,如下是我按照官方教程实际操做的流程。html
假设咱们已经完成了一个功能模块的封装,以 HelloPods 为例。首先,咱们要在 GitHub 上建立一个名为 HelloPods 的仓库,接着咱们要将本地封装好的代码提交到 GitHub 上。具体操做以下:git
echo "# HelloPods" >> README.md
git init
git add .
git commit -m 'commit'
git remote add origin https://github.com/hiXgb/HelloPods.git
git push -u origin master
git tag 0.0.1
git push --tag
执行完以上操做后咱们就成功将代码提交到了 GitHub 上,接下去要作的是发布到 Cocoapods 上。github
首先,咱们执行 pod spec create
生成 HelloPods.podspec 文件,生成的模板文件有大量注释,咱们只须要其中一部份内容,整理后的内容以下:swift
Pod::Spec.new do |spec|
spec.name = 'HelloPods'
spec.version = '0.0.1'
spec.license = { :type => 'MIT' }
spec.homepage = 'https://github.com/hiXgb/HelloPods'
spec.authors = { 'xgb' => 'xieguobiyi@gmail.com' }
spec.summary = "Learn about creating Podspec's and the Spec repo."
spec.source = { :git => "https://github.com/hiXgb/HelloPods.git", :tag => "0.0.1" }
spec.source_files = '*.{h,m}'
spec.requires_arc = true
end复制代码
HelloPods.podspec 文件编辑完成后执行 pod spec lint
验证 podspec 文件是否合法,结果以下:缓存
验证经过后须要在 Cocoapods 上注册 trunk,执行bash
pod trunk register xieguobiyi@gmail.com 'xgb'复制代码
而后查收邮箱点击连接便可完成注册,注册完成后能够经过 pod trunk me
查询注册信息ide
而后再执行 工具
pod trunk push HelloPods.podspec --allow-warnings复制代码
不出意外的话咱们能看到以下结果测试
至此咱们就完成了库的发布,后续就能够按照其余第三方库同样的用法来使用咱们本身的库了~ui
若是一个库有多个子模块,咱们能够经过添加 subspec 使结构更加清晰。假设 HelloPods 下有两个子模块,一个 Util,一个 Model,咱们首先修改 HelloPods.podspec 文件,修改后的内容以下:
Pod::Spec.new do |spec|
spec.name = 'HelloPods'
spec.version = '0.0.3'
spec.license = { :type => 'MIT' }
spec.homepage = 'https://github.com/hiXgb/HelloPods'
spec.authors = { 'xgb' => 'xieguobiyi@gmail.com' }
spec.summary = "Learn about creating Podspec's and the Spec repo."
spec.source = { :git => "https://github.com/hiXgb/HelloPods.git", :tag => "0.0.3" }
spec.source_files = '*.{h,m}'
spec.requires_arc = true
spec.subspec 'Util' do |util|
util.source_files = 'Util/*.{h,m}'
end
spec.subspec 'Model' do |model|
model.source_files = 'Model/*.{h,m}'
end
end复制代码
修改完成后再重复前面的步骤,将子模块都先提交到 GitHub 上,而后修改 tag 为 0.0.3,接着再执行 pod trunk push HelloPods.podspec --allow-warnings
将修改后的内容发布到 Cocoapods trunk 上,发布完成后咱们再执行 pod search 'HelloPods'
结果以下:
[!] The validator for Swift projects uses Swift 2.3 by default, if you are using a different version of swift you can use a
.swift-version
file to set the version for your Pod. For example to use Swift 3.0, run:echo "3.0" > .swift-version
.
执行 echo "3.0" > .swift-version
便可解决
warning: Could not find remote branch 0.0.1 to clone.
fatal: Remote branch 0.0.1 not found in upstream origin
执行
git tag 0.0.1
git push --tag复制代码
ERROR | File Patterns: File patterns must be relative and cannot start with a slash (source_files).
这个错误主要是实际文件目录和配置文件里的没有匹配上,须要根据实际项目文件结构具体配置,在上述例子里的配置是
spec.source_files = '*.{h,m}'复制代码
能够依次尝试如下几种方法:
rm -rf ~/Library/Caches/CocoaPods复制代码
pod update
以上就是我跟着教程实际操做的流程和遇到问题的记录,最重要的仍是要本身动手操做一遍,但愿你们在操做过程当中也能有收获,enjoy~