前言
项目中经常会遇到第三方库需要更新,而本地的repo库没有,需要执行pod repo update, 导致半天都不能更新,项目就无法运行,从而引起搭建自己的pod私有库,将经常使用的第三方库源码导入到私有库进行管理,这里主要记录一下搭建的流程以及分享一些踩过的坑
私有库搭建流程:
1.创建远程代码仓库和远程索引spec仓库
2.创建本地文件以及podspec文件
3.本地文件和podspec文件分别关联远程的代码仓库和spec仓库
4.使用私有库进行验证
1.创建远程代码仓库和spec仓库
私有库的搭建需要创建两个远程仓库,分别对应代码文件和spec的文件,通过spec文件索引的sourse_file去找到对应存储的代码文件, 直接通过Gitlab创建
2.创建本地文件以及podspec文件
刚开始都是手动创建podspec、LICENSE等文件, 各种报错,因此我们通过终端来自动创建本地文件和podspec文件, cd到电脑的你需要存放的位置,执行pod lib create命令
pod lib create 项目名称
按下面流程执行:
# 选择编程语言
What language do you want to use?? [ Swift / ObjC ]
> Objc
# 在你的项目中是否创建一个demo工程,为了方便测试,我选择了Yes
Would you like to include a demo application with your library? [ Yes / No ]
> Yes
# 测试框架选择哪一个
Which testing frameworks will you use? [ Specta / Kiwi / None ]
> None
#要不要做视图测试
Would you like to do view based testing? [ Yes / No ]
> Yes
# 类前缀名
What is your class prefix?
> T
这里cd的存放位置会生成一个你创建的项目名称, 目录结构如下:
这里的Classes文件是我们用来存放代码文件的地方,如图,这里我把第三方库的源码导入到项目就放在了Classes文件中, Example是一个项目,可以用来测试你导入的代码文件是否可以通过编译,下面来介绍我们的主角podspec文件,首先我们来看一下它的结构
Pod::Spec.new do |s|
s.name = 'timingLibrarys'
s.version = '1.0.0'
s.summary = 'timing的私有库'
# This description is used to generate tags and improve search results.
# * Think: What does it do? Why did you write it? What is the focus?
# * Try to keep it short, snappy and to the point.
# * Write the description between the DESC delimiters below.
# * Finally, don't worry about the indent, CocoaPods strips it!
s.description = <<-DESC
TODO: Add long description of the pod here.
DESC
s.homepage = 'https://www.jianshu.com/p/8b1edc28776b'
# s.screenshots = 'www.example.com/screenshots_1', 'www.example.com/screenshots_2'
s.license = { :type => 'MIT', :file => 'LICENSE' }
s.author = { 'PersonalRhp' => '1836619909@qq.com' }
s.source = { :git => 'http://121.43.48.157/raohonping123456/timingLibrarys.git', :tag => s.version.to_s }
# s.social_media_url = 'https://twitter.com/<TWITTER_USERNAME>'
s.ios.deployment_target = '8.0'
#s.source_files = 'timingLibrarys/Classes/**/*.{h,m}'
s.libraries = "c++","c"
s.subspec 'MJRefresh' do |ss|
ss.source_files = 'timingLibrarys/Classes/MJRefresh/**/*.{h,m}'
ss.resource = 'timingLibrarys/Classes/MJRefresh/MJRefresh.bundle'
ss.ios.deployment_target = '8.0'
end
s.subspec 'AliPlayerSDK_iOS' do |ss|
ss.source_files = 'timingLibrarys/Classes/AliPlayerSDK_iOS/**/*.{h,m}'
ss.ios.deployment_target = '8.0'
ss.public_header_files = 'timingLibrarys/Classes/AliPlayerSDK_iOS/**/*.{h}'
end
s.subspec 'SDWebImage' do |ss|
ss.ios.deployment_target = '8.0'
ss.source_files = 'timingLibrarys/Classes/SDWebImage/**/*.{h,m}', 'timingLibrarys/Classes/FLAnimatedImage/**/*.{h,m}'
end
- homePage是项目主页地址,这里填写可以访问的地址就可以,我就把简书地址放上去了,哈哈
- sourse是项目代码仓库的地址,不要填成了远程索引库的地址,不然执行pod spec lint 验证报错
- ios.deployment_target是支持的pod最低版本,这个必须填,不然执行验证报UIkit not fund
- libraries是工程依赖的library, 导入的第三方库用到了C语言,需要加上s.libraries = "c++","c"才能通过验证
- source_files是pod库的源文件,这里需要与源文件的路径对应起来,不然source_files验证报错
- subspec是进行文件目录分层,使结构更加清晰
具体更多用法参考podspec的语法
我们需要根据我们的Classes导入的源码文件在podspec文件中做对应的修改,这里创建本地文件以及podspec文件这一步就完成了
3.本地文件和podspec文件分别关联远程的代码仓库和spec仓库
我们需要先执行以下pod lib lint 来验证本地的文件是否可以通过,这里我们可以通过pod lib lint --allow-warnings来进行警告的忽略,这里导入的第三方库用到了C语言,因此还需要加上--use-libraries 才可以通过验证,报错的话可以通过pod lib lint --allow-warnings --verbose 查看具体报错的地方,验证通过后执行以下命令
1. git remote add origin 代码仓库地址
2. git add .
3. git commit -m "提交内容"
4. git pull origin master
5. git tag -a '0.0.1' -m "tag版本提交描述" // 对应podspec文件的version
6. git push --tags
7. git push origin master
这一步本地的代码文件就和远程代码仓库关联上了,验证远程索引文件和源码文件是否匹配正确,执行pod spec lint, 验证通过后,将本地的podspec文件提交到远程的spec仓库
pod repo add 本地索引名称 远程索引sepc仓库地址
pod repo push 本地索引名称 项目名称.podspec
4. 使用私有库进行验证
在Podfile文件最上面添加一行:
1.source '远程索引spec仓库地址'
2.添加pod '项目名称'
3.执行pod install
这里pod私有库的搭建就完成了,下面附上更新维护私有库的操作流程
1. 修改podspec文件的version
2. git add .
3. git tag -a '0.0.2' -m "tag"
4. git push --tags
5. git pull origin master
6. 使用项目的工程执行pod update
前言中提出pod repo update 慢的问题,这里附上两种解决方案
- 将项目中的sourse 的源换成国内镜像的源, 如目前国内有清华大学提供的源,后边会更新,可能无效,到时候自己搜一下,看有哪些国内镜像源,替换一下即可
source 'https://mirrors.tuna.tsinghua.edu.cn/git/CocoaPods/Specs.git'
- 通过Shadowsocks设置Github的代理
1. 使用Shadowsocks代理 -> 高级设置 -> 本地Socket5监听端口修改为1080
2. git config --global http.https://github.com.proxy socks5://127.0.0.1:1080 (国内git库不需要走代理,而是只需要github上的代码库走代理)
3. git config --global --unset http.https://github.com.proxy (恢复/移除github上的代码库走代理)
网友评论