Cocoapods 搭建私有库

169 阅读3分钟

前言

创建私有库之前,我们 需要建立两个git库,一个是远程Spec索引库(下文中的 gitee.com/zhou_jun_pe… ),一个远程组件库(下文中的 gitee.com/zhou_jun_pe… )。

建立远程索引库

我们可以在公司的git服务器上,新建一个空白的git 库。然后通过Cocoapod在本地创建一个Specs私有库:

 pod repo add SpecName https://gitee.com/zhou_jun_peng/spec.git

在本地的 ~/.cocoapod/repos 文件夹下,会生成一个SpecName 的 本地 Spec 库,开始这个文件夹是空的。

2022-11-18-16-39-06-image.png

远程索引库跟本地Specs库关联:

~/.cocoapod/repos/SpecName 目录下, 新建一个 ReadMe.md,将本地的Specs库和远程库进行关联:

touch ReadMe.md
git add ReadMe.md
git commit -m "初始化"
git add remote SpecName https://gitee.com/zhou_jun_peng/spec.git
git push origin master

生成一个本地私有库组件

pod lib create SpecName

会要求你填一些基本信息:

2022-11-18-16-06-28-image.png

完成之后工程目录如下:

2022-11-18-16-21-41-image.png

  • Example : 私有库组件的示例代码
  • SpecName :私有库组件的代码和资源
  • SpecName.podspec :私有库组件的版本、git地址等信息

在Example 的根目录下执行:

pod install

2022-11-18-16-27-28-image.png

配置 podspec文件:

2022-11-18-16-28-42-image.png

#
# Be sure to run `pod lib lint SpecName.podspec' to ensure this is a
# valid spec before submitting.
#
# Any lines starting with a # are optional, but their use is encouraged
# To learn more about a Podspec see https://guides.cocoapods.org/syntax/podspec.html
#

Pod::Spec.new do |s|
  #组件名称
  s.name             = 'SpecName'  
  #组件版本 
  s.version          = '0.1.0'        
  s.summary          = 'A short description of SpecName.'       

# 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://github.com/“zhoujunpeng”/SpecName'    //个人主页
  # s.screenshots     = 'www.example.com/screenshots_1', 'www.example.com/screenshots_2'
  s.license          = { :type => 'MIT', :file => 'LICENSE' }
  s.author           = { '“zhoujunpeng”' => 'zhoujunpeng1992@gmail.com' }

  #源码仓库
  s.source           = { :git => 'https://gitee.com/zhou_jun_peng/spec-name-project.git', :tag => s.version.to_s }   
  # s.social_media_url = 'https://twitter.com/<TWITTER_USERNAME>'

  s.ios.deployment_target = '10.0'

  #源码路径
  s.source_files = 'SpecName/Classes/**/*'        

  # s.resource_bundles = {
  #   'SpecName' => ['SpecName/Assets/*.png']
  # }

  # s.public_header_files = 'Pod/Classes/**/*.h'
  # s.frameworks = 'UIKit', 'MapKit'
  # s.dependency 'AFNetworking', '~> 2.3'    //依赖的第三方库
end

将以下信息修改成我们自己的:

  • s.name: 私有库组件名称
  • s.version:私有库组件版本
  • s.source:私有库组件的git地址
  • s.source_files:私有库组件的源码路径

提交修改后的文件到我们的远程组件库:

git add .
git commit -m "0.1.0" 
git remote add origin https://gitee.com/zhou_jun_peng/spec-name-project.git
git push origin master 

2022-11-18-16-36-25-image.png

2022-11-18-16-37-16-image.png

打上标签:

git tag -a "0.1.0" -m "0.1.0"  //当前标签,应该跟podspec 的 s.version 保持一致
git push --tags

2022-11-18-16-38-03-image.png

2022-11-18-16-41-10-image.png

验证本地代码:

pod spec lint --allow-warnings

2022-11-18-17-18-05-image.png

提交podspec文件到本地 Spec库:

pod repo push SpecName SpecName.podspec --allow-warnings

2022-11-18-17-31-06-image.png 提交成功后,在 ~/.cocoapod/repos/SpecName 下面就能看到我们私有组件库的对应版本了:

2022-11-18-17-31-58-image.png

使用本地私有库

更改工程文件的 Podfile 文件

source 'https://gitee.com/zhou_jun_peng/spec.git' // 添加私有库git地址
pod 'SpecName'    //依赖私有库组件

一些遇到的错误

1、当提交podspec到私有库的时候,出现了:

2022-11-18-17-30-11-image.png 在 ~/.cocoapod/repos/SpecName 里面新建一个 ReadMe.md 文件,并提交到Spec 远程库,再次提交就可以。

2、如果多次尝试提交不行的情况下:

pod repo remove SpecName
pod repo add SpecName https://gitee.com/zhou_jun_peng/spec.git
git add .
git commit -m "初始化"
git push origin master

尝试先移除本地Spec Repo,再重新关联添加。

3、podspec 配置子目录

在我成功提交了私有库组件之后,发现私有库原有的目录结构没有了,我们需要修改podspec配置:

 // 原始的配置文件
 s.source_files = 'SpecName/Classes/**/*'

 // 修改后的配置文件
 s.source_files = 'SpecName/Classes/*'
 s.subspec '子目录1' do |ss|
      ss.source_files = 'SpecName/Classes/子目录1/*'
    end
 s.subspec '子目录2' do |ss|
      ss.source_files = 'SpecName/Classes/子目录2/*'
    end

4、验证本地代码时,如果包含私有库

pod spec lint --allow-warnings --sources='私有库Git地址,https://github.com/CocoaPods/Specs.git'