美文网首页iOS-OC中级
CocoaPods 创建私有Pod

CocoaPods 创建私有Pod

作者: 秀才不才 | 来源:发表于2019-06-24 17:21 被阅读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
  • What platform do you want to use?? [ iOS / macOS ],选择iOS,直接回车默认选择第一个
  • What language do you want to use?? [ Swift / ObjC ],选择Swift。CocoaPods会把你的库设置为framework。
  • Would you like to include a demo application with your library? [ Yes / No ],选择Yes。如果你想要包含一个示例工程,或计划在app中测试你的库,这里需要选择Yes,以便模板文件为你的库创建Xcode工程。也就是如果你计划为pod添加截图,这里需要选择Yes。
  • Which testing frameworks will you use? [ Quick / None ],选择Quick。提交库到CocoaPods前应当进行测试。CocoaPods推荐使用其附带的测试framework,而非Apple的XCTest。在Objective-C中,可以选择Specta/Expecta或Kiwi,其区别如下:
    Specta/Expecta:通过不同podspecs的模块化方法。
    Kiwi:是对Stubs/Mocks/Expections的一种一体化方法。
    CocoaPods已经在MyLib-Tests.pch文件中添加了所有必要的包含和设置,因此不必将它们包含在每个文件中。
  • Would you like to do view based testing? [ Yes / No ],选择Yes。根据你建立的仓库,你可能会发现基于快照的测试是验证视图不同部分不同操作的最佳方法,这里建议使用FBSnapShotTestCase。如果你使用的是Specta/Expecta,那么其会包含一个pod来改进语法。
  • What is your class prefix?如果你选择的语言是ObjC,其最后会要求提供类前缀
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
  • pod repo add 执行完毕后,cocoapods 会把DYSpecs clone 在 ~/.cocoapods/repos 目录

三、关联库

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'

五、错误信息

  • .Encountered an unknown error (Unable to find a specification for xxxx depended upon by xxxx
$ pod lib lint --sourcePod2索引地址() --allow-warnings

Example:pod lib lint --sourcehttps://github.com/SenDylan/DYSpecs.git,https://github.com/CocoaPods/Specs.git --allow-warnings
  • Remote branch not found in upstream origin
$ git tag 'v0.0.1'
$ git push --tags
  • The PodTest.podspec specification does not validate.
可能你用到了lib 静态库,在命令行加上
$ pod repo push DYSpecs DYCategoryKit.podspec  --use-libraries --allow-warnings

参考自:这里

相关文章

  • Cocoapods 创建私有库及使用

    Cocoapods 创建私有库 一、创建pod 私有仓库 ​ 目的:此用于保存podspec文件。pod...

  • CocoaPods 私有仓库的创建教程

    CocoaPods 私有仓库的创建教程 创建私有pod首先需要一个容器,类似于 The CocoaPods Mas...

  • Pod私有库制作

    ##Pod私有库制作 ####目录 +安装CocoaPods +创建远程内部私有Spec Repo仓库 +模板创建...

  • 二、创建私有Pod

    使用Cocoapods创建私有podspec Pod存放位置 /.cocoapods/master 其中Specs...

  • 制作自己的Cocoapods

    制作自己的Cocoapods 创建自己私有pod库,官方推荐使用 pod lib create [pod name...

  • CocoaPods私有库走过的坑

    CocoaPods创建私有库遇到的坑 1、私有库依赖自己仓库的私有库执行pod spec lint时,需要在pod...

  • 创建cocoapods 私有库。

    创建cocoapods 私有库。 pod lib create BlinkingLabel创建模板,后面可以考虑直...

  • CocoaPods 创建私有Pod

    此处省略一万字……废话不多说,直接进入正题! 一、创建我们需要pod的项目 1.另起一个路径,执行如下命令: 2....

  • iOS制作私有组件

    1.创建私有Pod项目 建议使用Cocoapods的一个工具创建项目,加入我们创建一个私有库UserCenterM...

  • 使用Cocoapods创建私有Pod

    前言 Cocoapods是非常好用的一款iOS第三方库管理工具,使用它可以非常方便的管理和更新项目中使用到的第三方...

网友评论

    本文标题:CocoaPods 创建私有Pod

    本文链接:https://www.haomeiwen.com/subject/vipoqctx.html