创建自己的Cocoapods

首先我们来想一下Cocoapods的工作逻辑。
Podfile中的默认source为https://github.com/CocoaPods/Specs.git,这便是一个Spec repo,也就是Pod的索引,pod update的时候会去这个repo中进行检索,如果检索到这个pod,便会去对应Pod的podspec中读取对应的路径下载安装。

接下来想想我们要做什么,首先我们需要建立一个远程仓库作为Spec repo,这里建立一个仓库叫做CYRepo,然后我们再建立一个Pod库CYKit,并将这个Pod库与CYRepo关联起来,最后我们便可以pod install我们对应的项目了。

最后完成的目录结构如图所示:

image.png

一、创建我们的索引仓库。

我们在自己的gitlab服务器上创建一个远程仓库,地址为:http://xxxxxxxxxxx/droog/CYRepo.git
然后将我们的远程仓库添加到我们的Repo中

pod repo add [私有库名] [远程仓库URL]

这时候可以通过pod repo查看对应的repo,可以在终端看到对应的CYRepo跟相应的地址。

二、创建我们的Pod库

pod lib create CYKit

按照提示创建成功后,看到文件目录里的CYKit.podspec,便是我们这个Pod库的配置文件,内容编辑如下

Pod::Spec.new do |s|

  s.name         = "CYKit"
  s.version      = "1.0.1"
  s.platform     = :ios, "11.0"

  s.summary      = "this is a practice project of pod."
  s.homepage     = "https://www.apple.com"
  s.license              = { :type => "MIT", :file => "LICENSE" }
  s.author             = { "xcode" => "[email protected]" }

  s.source       = { :git => "http://xxx.xxx.xxx.xx/droog/CYKit.git" }
  s.source_files  = "CYKit/**/*.swift"
  s.resource     = 'CYKit/CYKit.bundle'
  
  s.framework  = "UIKit","Foundation"
  s.swift_version = '5.0'
  s.requires_arc = true

end
对应文件夹如图所示
image.png

然后将我们的CYKit推到对应的git地址。
假如我们想要更新内容,比如我们指定podspec的

s.version      = "1.1.0"

给项目添加标签1.1.0。

三、将Pod库跟索引库建立联系

接着我们需要将指定的版本推送到远程仓库,

pod repo push CYRepo CYKit.podspec --allow-warnings

将我们的podspec文件推送到我们的CYRepo中.

四、建立测试项目,编辑Podfile文件测试Pod库

OK,让我们试一下,新建一个项目CYKitTest并编辑Podfile文件.

target 'CYKitTest' do
  # Comment the next line if you don't want to use dynamic frameworks
  # 使用其他来源地址
  source 'http://182.92.213.80/droog/CYRepo.git’
  # 使用官方默认地址(默认)
  source 'https://github.com/CocoaPods/Specs.git'
  
  use_frameworks! :linkage => :static

#  :git => 'http://xxxxxxx//CYKit.git'
  pod 'CYKit', '~> 1.1.0'
end

执行pod install之前我们可以先更新一下索引的CYRepo

pod repo CYRepo update

更新完成后运行pod install,便可以使用了。

你可能感兴趣的:(创建自己的Cocoapods)