准备工作
- 安装cocoapod (已安装请忽略,未安装请找度娘)
- 创建私有的spection存放地址
① http://xxxxxxx/TJRepo.git
- 创建工具类代码存放地址
② http://xxxxxxx/TJKit.git
1. 在本地创建spec仓库
打开终端,在终端输入
# pod repo add [自定义的本地私有库名字] [第一个git仓库地址]
如: pod repo add TJRepo http://xxxxxxx/TJRepo.git
![](https://img.haomeiwen.com/i688600/33e25bdeb05fa16d.png)
项目clone下来之后,在~/.cocoapods/repos/ 文件夹下就会多出一个MyRepo的文件夹,这里就是我们即将存放本地podspec的仓库
![](https://img.haomeiwen.com/i688600/e014d2c8c2f90552.png)
2.配置源码仓库
2.1 创建源码仓库
这次要用到第二个git地址,首先我们需要创建一个空白文件夹【私有仓库Demo】,然后 cd 到该文件目录下执行
pod lib create [仓库名字]
如:pod lib create TJKit
![](https://img.haomeiwen.com/i688600/1cf9d4677563ba64.png)
![](https://img.haomeiwen.com/i688600/6f59627e00c8ce72.png)
如果出现错误请查看 Q1 是否符合你的情况
Pod私有库创建成功。一般来说创建成功会自动打开项目
【私有仓库】文件夹下将会生成一个【TJKit】的文件夹,目录结构如下
![](https://img.haomeiwen.com/i688600/d13b4b28dba369ff.png)
2.2 修改TJKit.podspec文件
# 基础信息
Pod::Spec.new do |s|
s.name = 'TJKit' # 名字
s.version = '0.0.1' # 版本号,需要跟我们上传的tag号一致
s.summary = 'xxxx' #摘要,可自定义
# 描述 可自定义
s.description = <<-DESC
xxxxx
DESC
#项目的主页,同浏览器地址
s.homepage = 'http://xxxxx/TJKit'
# s.screenshots = 'www.example.com/screenshots_1', 'www.example.com/screenshots_2'
s.license = { :type => 'MIT', :file => 'LICENSE' }
s.author = { '作者' => '您的邮箱' }
# clone的地址
s.source = { :git => 'http://xxxxx/TJKit.git', :tag => s.version.to_s }
# s.social_media_url = 'https://twitter.com/<TWITTER_USERNAME>'
# 最低支持的iOS版本
s.ios.deployment_target = '10.0'
# 私有库所在的文件目录
s.source_files = 'TJKit/Classes/**/*'
# 关联的图片资源
# s.resource_bundles = {
# 'TJKit' => ['TJKit/Assets/*.png']
# }
# s.public_header_files = 'Pod/Classes/**/*.h'
# s.frameworks = 'UIKit', 'MapKit'
# s.dependency 'AFNetworking', '~> 2.3'
end
2.3 添加自己的代码
把需要做成私有库的源码放到TJKit/Classes中,然后将原来的placeme.swift文件删除,注意新文件中不能有依赖错误
2.4 检查设置是否正确
# 检查命令
pod lib lint --allow-warnings --verbose
![](https://img.haomeiwen.com/i688600/eefce3342877b13c.png)
验证通过,如遇验证不通过可按照提示进行修改
问题汇总请参考Q&A
2.5 将代码同步到Git上
git add .
git commit -m "pod test"
git remote add origin http://xxxxxxx/TJKit.git
push origin master
2.6 添加tag并同步
# 0.0.1,对应 xxxxx.podspec 中的s.version
git tag -m "First Version" 0.1.0
git push --tags
2.7 向本地的Repo提交podspec文件
# pod repo push [第⼀一步私有库名称] [podspec⽂文件]
如:pod repo push MyRepo TJKit.podspec
有警告是不会通过的,如果想忽略警告,或者查看详情,可以在命令后添加以下参数
--verbose 查看详情
--allow-warnings 警告忽略
![](https://img.haomeiwen.com/i688600/e90edace2e23f4bd.png)
成功了之后,~/.cocoapods/repos/xxxx文件结构如下:
![](https://img.haomeiwen.com/i688600/cb5197540b758466.png)
如果不成功请查看Q2
2.8 更新仓库
pod repo update MyRepo
[图片上传失败...(image-da805f-1562830213674)]
3.使用私有库
xcode新建一个项目,在项目文件夹创建Podfile,创建好之后添加:
pod "TJKit",:git=> 'http://xxxxxx/TJKit.git'
然后执行pod install
Q&A
q1
.pod无法生成项目
错误如下:
/Library/Ruby/Gems/2.3.0/gems/xcodeproj-1.5.8/lib/xcodeproj/project/object/helpers/build_settings_array_settings_by_object_version.rb:27:in `<module:BuildSettingsArraySettingsByObjectVersion>': undefined method `to_set' for #<Array:0x00007f9d7912bfb0> (NoMethodError)
Did you mean? to_s
from /Library/Ruby/Gems/2.3.0/gems/xcodeproj-1.5.8/lib/xcodeproj/project/object/helpers/build_settings_array_settings_by_object_version.rb:8:in `<class:XCBuildConfiguration>'
from /Library/Ruby/Gems/2.3.0/gems/xcodeproj-1.5.8/lib/xcodeproj/project/object/helpers/build_settings_array_settings_by_object_version.rb:4:in `<module:Object>'
...
...
解决方法:升级cocoapod
sudo gem install -n /usr/local/bin cocoapods
q2
Your configuration specifies to merge with the ref 'refs/heads/master'
from the remote, but no such ref was fetched.
项目没有初始化, 这个时候git仓库是空的,需要我们在~/cocoapods/repo/xxxx 下添加一个初始文件,例如添加个readme.md
解决方法:
检查./git/config 文件中git地址是否正确
初始化项目,执行
git config --global user.name "git用户名"
git config --global user.email "邮箱"
cd TJRepo
touch README.md
git add README.md
git commit -m "add README"
git push -u origin master
网友评论