前言
使用cocoapods管理iOS开发所使用的第三方库已经是一个非常常见的操作。同样的,利用cocoapods实现组件化开发,对于大型项目的合理分工,版本控制,提高编译效率具有非常明显的作用。这里我对本地私有库和远程私有库的创建进行一个总结,而这是组件化开发的基础。
一.准备
1.创建工程文件
2.生成pod库配置文件
bogon:XTProtocolManager xiaotei$ pod spec create XTProtocolManager
当然,你也可以手动创建一个podspec文件,将必要内容粘贴进去
Pod::Spec.new do |s|
s.name = "XTProtocolManager"
s.version = "0.0.1"
s.ios.deployment_target = '7.0'
s.summary = "XTProtocolManager是一个iOS组件化开发的组件之一,主要用来管理模块跳转"
s.homepage = "https://github.com/dingpuyu/XTProtocolManager.git"
s.license = { :type => "MIT", :file => "LICENSE" }
s.author = { "dingpuyu" => "ding13525163308@163.com" }
s.social_media_url = "http://twitter.com/dingpuyu"
s.source = { :git => "https://github.com/dingpuyu/XTProtocolManager.git", :tag => s.version }
s.source_files = "XTProtocolManager/XTProtocolManager/*.{h,m}"
s.requires_arc = true
end
s.name:名称,pod search 搜索的关键词,注意这里一定要和.podspec的名称一样,否则报错
s.version:版本号
s.ios.deployment_target:支持的pod最低版本
s.summary: 简介
s.homepage:项目主页地址
s.license:许可证
s.author:作者
s.social_media_url:社交网址,这里我写的Twitter,如果你写Twitter的话,你的podspec发布成功后会@你
s.source:项目的地址
s.source_files:需要包含的源文件
s.resources: 资源文件
s.requires_arc: 是否支持ARC
s.dependency:依赖库,不能依赖未发布的库
s.dependency:依赖库,如有多个可以这样写
3.编辑podspec文件
二.本地私有库
1.为库工程创建本地git仓库
1.进入库工程文件夹
2.git初始化
3.查看文件状态,可以发现文件都还没有添加到本地库中
4.添加文件到缓冲区
5.从缓冲区提交文件到本地代码仓库
6.打标签查看标签及删除标签的命令,这里我们打上0.0.1的标签
完成这些操作,就可以去编辑podspec文件的source和source_file了
7.编辑podspec
工程的目录结构是这样的
此时配置podspec文件的source和source_file如下
s.source = { :git => "", :tag => s.version }
s.source_files = "XTProtocolManager/XTProtocolManager/*.{h,m}"
2.创建并编辑Podfile
使用终端进入工程主目录如图2.1
1.执行命令$pod init
成功则可以在当前目录下看到Podfile文件
2.使用vim命令进行编辑
target 'XTComponentBase' do
pod 'XTProtocolManager', :path=>'../XTProtocolManager/XTProtocolManager.podspec'
end
此时如果直接pod install的话,会报错很多,我们来一个一个解决
[!] The `XTProtocolManager` pod failed to validate due to 3 errors.
[!] The validator for Swift projects uses Swift 3.0 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 2.3, run:
`echo "2.3" > .swift-version`:
- ERROR | license: Sample license type.
- WARN | homepage: The homepage has not been updated from default
- ERROR | source: The Git source still contains the example URL.
- WARN | summary: The summary is not meaningful.
- ERROR | description: The description is empty.
① 协议问题
将默认的s.license修改为s.license = { :type => "MIT", :file => "LICENSE" }
创建协议很简单,一个名为LICENSE的空文件拷贝如下内容,只需要将前边的版权修改一下即可
MIT License
Copyright (c) 2017 dingpuyu
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
咱们程序员并不care 警告,但是改一改内容就能很容易的解决警告。
接下来就把描述给补充一下,让它能够install通过
② s.description的修改
描述这里,要以这种格式写,不会出错。
③ homepage一般写的是git上的路径,我这里用的就是github得链接
④ source 这个比较关键,一定是要可用的git路径其中可以有四种设置方式
s.source = { :git => "./XTProtocomManager", :tag => s.version }
s.source = { :git => "https://github.com/dingpuyu/XTProtocolManager.git", :commit => "881daa" }
s.source = { :git => "https://github.com/dingpuyu/XTProtocolManager.git", :tag => 0.0.1 }
s.source = { :git => "https://github.com/dingpuyu/XTProtocolManager.git", :tag => s.version }
第一种是本地git仓库
commit => "68defea" 表示将这个Pod版本与Git仓库中某个commit绑定
tag => 1.0.0 表示将这个Pod版本与Git仓库中某个版本的comit绑定
tag => s.version 表示将这个Pod版本与Git仓库中相同版本的comit绑定
④ summary就是一段概要,根据项目作用写一写就好了。
3.pod install
如果顺利的话,本地私有库就已经可以用了
三.创建远程私有库
这里我们使用的是github作为远程仓库,如果是私有项目的话就不要用github,可以自己搭建git或者使用coding
步骤如下:
1.spec repo创建
①创建一个podspec仓库
②关联pod spec库,这里如果第一次做操作,需要输入账号密码
bogon:XTComponentBase xiaotei$ pod repo add XTPrivateLib https://github.com/dingpuyu/XTPrivateLib.git
Cloning spec repo `XTPrivateLib` from `https://github.com/dingpuyu/XTPrivateLib.git`
③查看
2.组件代码仓库创建
① 创建远程代码仓库
② 添加仓库关联及提交代码到远程仓库
bogon:XTProtocolManager xiaotei$ git remote add origin https://github.com/dingpuyu/XTProtocolManager.git
将本地库的代码推到远程库
bogon:XTProtocolManager xiaotei$ git push -f origin master
Counting objects: 36, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (29/29), done.
Writing objects: 100% (36/36), 20.65 KiB | 0 bytes/s, done.
Total 36 (delta 4), reused 0 (delta 0)
remote: Resolving deltas: 100% (4/4), done.
To https://github.com/dingpuyu/XTProtocolManager.git
+ 22c24f5...6b370f7 master -> master (forced update)
将本地创建的标签推到远程库
bogon:XTProtocolManager xiaotei$ git push --tags
Counting objects: 1, done.
Writing objects: 100% (1/1), 180 bytes | 0 bytes/s, done.
Total 1 (delta 0), reused 0 (delta 0)
To https://github.com/dingpuyu/XTProtocolManager.git
* [new tag] 0.0.1 -> 0.0.1
删除标签可以这样来
bogon:XTProtocolManager xiaotei$ git push origin :0.0.1
To https://github.com/dingpuyu/XTProtocolManager.git
- [deleted] 0.0.1
git在使用过程中也会有各种各样的问题,有遇到什么问题的,可以给我留言
3.向私有spec repo中提交podspec
① 在本地组件工程目录编辑podspec文件,如果没有创建,使用pod spec create XXX
此时的source应该如下,tag就是刚刚设置0.0.1
s.source = { :git => "https://github.com/dingpuyu/XTProtocolManager.git", :tag => s.version }
②进行验证
③上传podspec
bogon:XTProtocolManager xiaotei$ pod repo push XTPrivateLib XTProtocolManager.podspec
Validating spec
-> XTProtocolManager (0.0.1)
Updating the `XTPrivateLib' repo
Already up-to-date.
Adding the spec to the `XTPrivateLib' repo
- [Add] XTProtocolManager (0.0.1)
Pushing the `XTPrivateLib' repo
To https://github.com/dingpuyu/XTPrivateLib.git
9610a4d..505f5a5 master -> master
4.如何使用呢?
① 查找其路径
bogon:XTProtocolManager xiaotei$ pod search XTProtocolManager
-> XTProtocolManager (0.0.1)
这是一个组件管理工具 XTProtocolManager.
pod 'XTProtocolManager', '~> 0.0.1'
- Homepage: https://github.com/dingpuyu/XTProtocolManager
- Source: https://github.com/dingpuyu/XTProtocolManager.git
- Versions: 0.0.1 [XTPrivateLib repo]
② 修改Podfile文件
source 'https://github.com/dingpuyu/XTPrivateLib.git'
source 'https://github.com/CocoaPods/Specs.git' #官方仓库的地址
target 'XTComponentBase' do
pod 'XTProtocolManager'
end
③ 下载
bogon:XTComponentBase xiaotei$ pod install
Analyzing dependencies
Downloading dependencies
Installing XTProtocolManager (0.0.1)
Generating Pods project
Integrating client project
Sending stats
Pod installation complete! There is 1 dependency from the Podfile and 1 total pod installed.
好了,到这里就结束了私有库的创建,现在打开工程就可以看到自己的组件工程了