iOS-OC中级

CocoaPods 创建私有Pod

2019-06-24  本文已影响0人  秀才不才

此处省略一万字……
废话不多说,直接进入正题!

一、创建我们需要pod的项目

1.另起一个路径,执行如下命令:
$ cd ~/Desktop/
//这个命令的作用是创建一个pod项目,它会自动给你生成spec文件
$ pod lib create DYCategoryKit
//pod lib create NAME使用默认模版,如果想使用其他模版,可以添加--template-url=URL参数。
//pod lib create NAME:创建标准目录结构、模板文件。pod lib create NAME不是创建pod的唯一方法,但它是最简洁的方法
2.执行完这个命令之后会有如下几个选项,根据你的需求填写就👌了
image.png
3.选项填完之后回车,会自动创建一个项目,名称就是DFCategoryKit
4.在DFCategoryKit里面有两个文件夹
Assets:存放图片资源等等
Classes:存放源码,默认会有一个ReplaceMe.m文件 (比如SDWebImage pod里的类文件)

我们只需要把要上传的代码放入Classes文件即可,如果需要查看更改后demo效果,在Example中pod update即可更新修改的文件.

5.在github或gitlab上创建新的pod项目,这里我创建的地址是https://github.com/SenDylan/DYCategoryKit.git
6.进入DFCategoryKit项目,修改podspec文件,这里可以用Sublime Text或者Xcode (文本编辑器不推荐,因为可能会改变"的格式,造成lint不通过),我这里直接在xcode中进行更改,根据你的需要进行相应更改。
  Pod::Spec.new do |s|
  s.name             = 'DYCategoryKit' //名称,pod search 搜索的关键词,注意这里一定要和.podspec的名称一样,否则报错
  s.version          = '0.1.0' //版本号
  s.summary          = 'A short description of DYCategoryKit.' //简介
  s.description      = <<-DESC
  TODO: Add long description of the pod here.
                       DESC
  s.homepage         = 'https://github.com/SenDylan/DYCategoryKit' //项目主页地址
  s.license          = { :type => 'MIT', :file => 'LICENSE' } //许可证
  s.author           = { 'SenDylan' => '*******' } //作者
  s.social_media_url: //社交网址,你的podspec发布成功后会@你
  s.source           = { :git => 'https://github.com/SenDylan/DYCategoryKit.git', :tag => s.version.to_s } //:项目的地址
  s.ios.deployment_target = '8.0' //支持的pod最低版本
  s.source_files = 'DYCategoryKit/Classes/**/*' //需要包含的源文件
  s.resources: 资源文件
  s.requires_arc: 是否支持ARC
  s.dependency:依赖库,不能依赖未发布的库,如果有多个可以写多个s.dependency
7.执行$ pod lib lint --allow-warnings (--verbose加上显示详情)
//pod lib lint NAME:验证你创建的pod是否符合规范,是否可以通过CocoaPods使用。
-> DYCategoryKit (0.1.0)
DFCategoryKit passed validation.
//如果用到了私有Pod
$ pod lib lint --sourcehttps://github.com/SenDylan/DYSpecs.git,https://github.com/CocoaPods/Specs.git --allow-warnings
//如果用到了lib 静态库
$ pod lib lint --sourcehttps://github.com/SenDylan/DYSpecs.git,https://github.com/CocoaPods/Specs.git --use-libraries --allow-warnings

终端如此显示便是lint成功了.

8.继续执行如下命令
$ git remote add origin https://github.com/SenDylan/DYCategoryKit //这里是你需要pod的项目地址,不是私有库的地址
$ git add .
$ git commit -m "注释描述"
$ git push origin master
//注意:如果你创建项目的时候生成了README或者license文件,那么这里你push的时候可能会push不了,如是你可用
$ git push origin master -f 强制提交,会覆盖之前的文件
9.为仓库打tag,这个tag需要和spec文件中的版本保持一致
$ git tag -m "你的描述" 0.1.0
$ git push --tags

二、创建私有的spec文件仓库(类似公共的CocoaPods Specs

1.首先在github或gitlab上创建一个空项目,这里起名为DYSpec.
2.在终端执行如下命令:
//pod repo add 私有库索引名字 github/gitlab 项目地址
$ pod repo add DYSpecs  https://github.com/SenDylan/DYSpecs.git

三、关联库

1.将你的代码库(DYCategoryKit)关联到索引库里(DYSpecs)
//执行如下命令
// pod repo push 最开始建立的私有库名称 pod项目中的spec文件,--allow-warnings --verbose 忽略警告,打印详细日志
$ pod repo push DYSpecs DYCategoryKit.podspec --allow-warnings
Validating spec --allow-warnings --verbose
 -> DYCategoryKit (0.1.0)
Updating the `DYSpecs' repo
Your configuration specifies to merge with the ref 'refs/heads/master'
from the remote, but no such ref was fetched.
Adding the spec to the `DYSpecs' repo
 - [Add] DYCategoryKit (0.1.0)
Pushing the `DYSpecs' repo
2.创建成功之后可能会出现search不到的情况,此时可删除本地搜索索引,然后再进行搜索
$ rm ~/Library/Caches/CocoaPods/search_index.json
$ pod search DYCategoryKit
3.私有仓库的使用你应该懂的,需要在Podfile添加source源
//如果还要添加公有pod,还需要加上这行
source 'https://github.com/CocoaPods/Specs.git'
//私有pod仓库索引
source 'https://github.com/SenDylan/DYSpecs.git'
platform :ios, "8.0"  
target "XXX" do  
    pod 'DYCategoryKit','~>0.1.0'  
end

Tips: 后期改动代码提交只需修改1.6的版本号重复1.7~1.9,3.1即可 小技巧:防止项目中的文件误操作可进行加锁:终端执行chmod 444 文件路径

四、私有Pod1引用私有Pod2

1.修改podspec文件
在 Pod1.podspec 文件 中添加 s.dependency '私有库名', '~>版本号' 
Podfile 添加 source 'Pod2 索引地址' 
用到共有库 添加共有索引地址 source 'https://github.com/CocoaPods/Specs.git'

五、错误信息

$ pod lib lint --sourcePod2索引地址() --allow-warnings

Example:pod lib lint --sourcehttps://github.com/SenDylan/DYSpecs.git,https://github.com/CocoaPods/Specs.git --allow-warnings
$ git tag 'v0.0.1'
$ git push --tags
可能你用到了lib 静态库,在命令行加上
$ pod repo push DYSpecs DYCategoryKit.podspec  --use-libraries --allow-warnings

参考自:这里

上一篇 下一篇

猜你喜欢

热点阅读