以Gitlab为例
####1 你需要新建两个仓库 如:
存放specs
- https://gitlab.123456.cn/iOSDemo/CMSpecs.git
存放代码 - https://gitlab.123456.cn/iOSDemo/LoginComponent.git
2 代码仓库
** 切记 仓库名和项目名一定不要一样, 否则很可能在使用时找不到库 **
新建一个文件夹执行, 将https://gitlab.123456.cn/iOSDemo/LoginComponentRep.git
克隆下来后执行
$ pod lib create LoginComponent
1会让你回答几个问题, 完成之后会多出一个名为LoginComponent文件.
2进入到 ’ LoginComponent/LoginComponent/classes
文件夹下, 删除 replace文件, 并把你的文件放到这个目录下.
3 进入到LoginComponent/Example
目录下 执行 pod update
4 打开Example文件下的工程. 找到 loginComponent.podspec
文件.修改
- s.source 改为 https://gitlab.123456.cn/iOSDemo/√
- s.homepage 改为https://gitlab.123456.cn/iOSDemo/LoginComponent
如果有依赖, 也要在后面写上 - s.dependency ‘MBProgressHUD+WJExtension’
- s.dependency ‘SAMKeychain’
解释一下, 不影响流程, 可以忽略
s.version = ‘0.3.0’
s.source = { :git => ‘https://gitlab.33.cn/iOSDemo/LoginComponentRep.git’, :tag => s.version.to_s }
你能看到 tag => s.version.to_s
这很好理解, 根据tag拉取代码, tag现在等于version, version是0.3.0.
但是version是干嘛的?
command + shift + g 到 ~/.cocoapods/repos下进入master下点开几个文件, 你会看到类似这样的文件
这里的version, 就是这个作用, 会根据version新建文件. 1.1.1 文件夹里面放的是podspec, 也就是你现在改的这个文件.
这样流程就通了. 使用私有库你需要在podfile里添加
source “https://gitlab.123456.cn/iOSDemo/CMSpecs.git”
pod ‘LoginComponent’, ‘0.3.0’
我们一会把podspec上传到这个仓库下. pod回去拉取这个文件. 读取version判断是否要新建文件夹.
读取source 获取代码.
解释结束
修改完成我们需要检查一下, 回到上一级界面
pod lib lint --use-libraries --allow-warnings
验证通过后, 把代码上传到你的代码仓库就行了, 我这里是(https://gitlab.123456.cn/iOSDemo/LoginComponentRep.git
)
Git命令
$ git add .
$ git commit -m"add pod file"
$ git push origin master
很可能会提醒你没有远程仓库, 那么需要你设置一下远程仓库, 如果没有提醒,那么这句不用执行
$ git remote add origin https://gitlab.33.cn/iOSDemo/LoginComponentRep.git
$ git tag 0.3.0
$ git push --tags
Specs仓库
执行
pod repo add CMSpecs https://gitlab.123456.cn/iOSDemo/CMSpecs.git
如果没有报错的话, 在你的~/.cocoapods/repos下就已经多处了一个CMSpecs, 这是你的私有仓库
执行
$ cd ~/.cocoapods/repos/CMSpecs
tip: 后面有个点. 别漏了
$ pod repo lint .
还记得刚才让你修改的 spec文件吗? 把地址放到这里
$ pod repo push CMSpecs ~/Desktop/aaa/LoginComponent.podspec --allow-warnings
好了配置完成
可以测试一下, 新建一个工程, 在podfile中输入
platform :ios,'8.0'
source "https://gitlab.123456.cn/iOSDemo/CMSpecs.git"
source 'https://github.com/CocoaPods/Specs.git'
target 'testPod' do
pod 'LoginComponent', '0.3.0'
end
pod update一下, 应该就能正常导入到项目中
私有库如何依赖私有库
比如私有库A, 用到了私有库B, 首先, B仓库肯定要先建立好, 把上面的过程走一遍, 在创建A仓库时, A仓库的Spec文件增加如下配置
s.dependency ‘RepoBUI’
s.dependency ‘RepoBNetworking’
RepoBUI
RepoBNetworking
就是A仓库要依赖的私有库, 这里只是一个例子.
完成后, 在A项目的Example项目的podfile中 导入RepoBUI, RepoBNetworking 仓库spec仓库所在地址如
source 'https://gitlab.123456.cn/xxx/RepoBUISpecRep.git'
source 'https://gitlab.123456.cn/x'x'x/FZM_RepoBNetworkingSpecRep.git'
source 'https://github.com/CocoaPods/Specs.git' # 这个必须要导入
好了, 如果你RepoBUI
和 RepoBNetworking
配置的没错的话, 这是后执行一下 ‘pod install’ 这两个仓库就进入到A仓库的Pods目录下