创建私有 Pods 库

简介

CocoaPods 作为 iOS 开发中最常用的框架管理工具,主要用于一些开源库在项目中的引用。也可用于私有项目的子模块的管理。因此,Pods库可以分为 公有 和 私有 两种形式。

公有,即 .spodspec 文件传到 CocoaPods 的仓库(Specs)中,所有人都能引用的,常见的公有 Pods 库有 AFNetWorking、SDWebImage等。创建共有 Pod 库需要注册 CocoaPods 账户。

而私有,是某个公司或个人私有的,可以是搭建在内网的 git 仓库或者可创建私有仓库的平台上(如 github、coding等),还可以本机创建。

概述过程

  1. 创建远端私有版本库(如 YSPrivateRepo),将其添加到本地 repo。
  2. 创建远端私有代码库,将仓库克隆到本地(给需要的人访问仓库的权限)。
  3. 本地代码库添加文件,同公有库三要素,共享文件夹、描述文件、LICENSE文件。
  4. 验证 .podspec 文件通过,打 tag,push 到远端。
  5. 将 .podspec 文件传到 2 中建立的本地版本库。
  6. 查找、使用私有库。
  7. 更新维护。

1. 创建远端私有版本库(略)

// pod repo add 本地版本库名 远端版本库URL

pod repo add YSPrivateRepo https://git.coding.net/TimerYJ/YSPrivateRepo.git

完成之后,进入磁盘目录 ~/.cocoapods/repos 可以看到增加了一个 YSPrivateRepo 目录。

ios 打私有库 ios pod私有库_ios 打私有库

注意:创建仓库时要选择 LICENSE 文件,类型一般为 MIT。

注意:区分 代码库版本库

2. 创建远端私有代码库(略)

3. 代码库添加文件

创建 .podspec 文件模板:

pod spec create YSCocoapodsPrivateSpec

修改完后的内容:( 附:podspec文件各属性与值含义 )

Pod::Spec.new do |s|
  s.name         = "YSCocoapodsPrivateSpec"
  s.version      = "1.0.0"
  s.summary      = "私有 Pod Spec 库测试"
  s.description  = "制作一个私有的 Pod Spec 库,用于测试模块化项目"
  s.homepage     = "https://coding.net/u/TimerYJ/p/YSCocoapodsPrivateSpec/git"
  s.license      = { :type => "MIT", :file => "LICENSE" }

  s.author       = { "YJ" => "yuan***@gmail.com" }
  s.platform     = :ios, "8.0"

  s.source       = { :git => "https://git.coding.net/TimerYJ/YSCocoapodsPrivateSpec.git", :tag => "#{s.version}" }
  s.source_files  = "Classes/**/*.{h,m}"
  s.public_header_files = "Classes/**/*.h"

  # s.framework  = "SomeFramework"
  # s.frameworks = "SomeFramework", "AnotherFramework"
end

注意:有的代码托管平台(如 oschina)建立的私有库使用 s.license = "MIT" 可能找不到 LICENSE 正式,可以改为上述写法指明文件。

4. 验证 .podspec 文件

// pod lib lint (YSCocoapodsPrivateSpec.podspec)(--private)(--allow-warnings)

pod lib lint

验证通过后如下图:

ios 打私有库 ios pod私有库_版本库_02

打 tag,push 到代码库。

注意:tag 标签 与 .podspec 的 version 要保持一致)

不打 tag 或者 tag 标签与 version 不一致会报错,如下:

ios 打私有库 ios pod私有库_iOS_03

5. 将 .podspec 文件传到版本库中

// pod repo push 本地版本库名 .podspec文件名

pod repo push YSPrivateRepo YSCocoapodsPrivateSpec.podspec

若出现错误,可以更新本地版本库重试:

// 如:[!] The repo `MyRepo` at `../.cocoapods/repos/YSPrivateRepo` is not clean

pod repo update YSPrivateRepo

成功后如下图:

ios 打私有库 ios pod私有库_git_04

6. 查找、使用

pod search YSCocoapodsPrivateSpec

ios 打私有库 ios pod私有库_git_05

成功查找就可以导入到项目中使用了,要注意的是,含私有 Pods 时,Podfile 文件开头要给出私有库的版本库地址。而同时包含公有和私有库时,还需要加上公有库的版本库地址。

// Podfile 内容

source 'https://git.coding.net/TimerYJ/YSPrivateRepo.git'    #私有版本库
source 'https://github.com/CocoaPods/Specs.git'      #公有版本库

platform:ios,'8.0'
target "YSCocoapodsTest" do
# 公有库
pod 'YSCocoapodsPublicSpec'
pod 'AFNetworking'
# 私有库
pod 'YSCocoapodsPrivateSpec'

end

7. 维护

更改

重复 3、4、5

删除

删除本地版本库、远程仓库。

// 删除本地版本库
pod repo remove YSPrivateRepo