[iOS]使用CocoaPods发布私有库

1.创建私有 Spec 仓库

首先,需要一个私有的 Git 仓库来存放你的 Podspec 文件,这个仓库用于索引你所有的私有 Pods。

  • 在 GitHub 或其他 Git 服务上创建一个新的私有仓库,例如,名为 PrivatePodSpecs
  • 克隆这个仓库到本地:
$ git clone https://github.com/yourusername/PrivatePodSpecs.git

 

 

2.准备你的组件库

确保你的库的代码已经在一个可访问的 Git 仓库中(比如 GitHub 的私有仓库)。库中应该包含:

  • 所有源代码
  • 许可证文件
  • README 文件,说明库的功能和使用方法

3.创建 Podspec 文件

在你的库项目的根目录下,执行以下命令来创建一个基本的 Podspec 文件:

$ pod spec create YourLibrary

这将创建一个名为 YourLibrary.podspec 的文件,其中 YourLibrary 是你的库的名字。CocoaPods 会自动填充一些基本的模板内容到这个文件中。

#
#  Be sure to run `pod spec lint MyComponent.podspec' to ensure this is a
#  valid spec and to remove all comments including this before submitting the spec.
#
#  To learn more about Podspec attributes see https://guides.cocoapods.org/syntax/podspec.html
#  To see working Podspecs in the CocoaPods repo see https://github.com/CocoaPods/Specs/
#

Pod::Spec.new do |spec|

  # ―――  Spec Metadata  ―――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
  #
  #  These will help people to find your library, and whilst it
  #  can feel like a chore to fill in it's definitely to your advantage. The
  #  summary should be tweet-length, and the description more in depth.
  #

  spec.name         = "MyComponent"
  spec.version      = "0.0.1"
  spec.summary      = "A short description of MyComponent."

  # 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!
  spec.description  = <<-DESC
                   DESC

  spec.homepage     = "http://EXAMPLE/MyComponent"
  # spec.screenshots  = "www.example.com/screenshots_1.gif", "www.example.com/screenshots_2.gif"


  # ―――  Spec License  ――――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
  #
  #  Licensing your code is important. See https://choosealicense.com for more info.
  #  CocoaPods will detect a license file if there is a named LICENSE*
  #  Popular ones are 'MIT', 'BSD' and 'Apache License, Version 2.0'.
  #

  spec.license      = "MIT (example)"
  # spec.license      = { :type => "MIT", :file => "FILE_LICENSE" }


  # ――― Author Metadata  ――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
  #
  #  Specify the authors of the library, with email addresses. Email addresses
  #  of the authors are extracted from the SCM log. E.g. $ git log. CocoaPods also
  #  accepts just a name if you'd rather not provide an email address.
  #
  #  Specify a social_media_url where others can refer to, for example a twitter
  #  profile URL.
  #

  spec.author             = { "" => "" }
  # Or just: spec.author    = ""
  # spec.authors            = { "" => "" }
  # spec.social_media_url   = "https://twitter.com/"

  # ――― Platform Specifics ――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
  #
  #  If this Pod runs only on iOS or OS X, then specify the platform and
  #  the deployment target. You can optionally include the target after the platform.
  #

  # spec.platform     = :ios
  # spec.platform     = :ios, "5.0"

  #  When using multiple platforms
  # spec.ios.deployment_target = "5.0"
  # spec.osx.deployment_target = "10.7"
  # spec.watchos.deployment_target = "2.0"
  # spec.tvos.deployment_target = "9.0"
  # spec.visionos.deployment_target = "1.0"


  # ――― Source Location ―――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
  #
  #  Specify the location from where the source should be retrieved.
  #  Supports git, hg, bzr, svn and HTTP.
  #

  spec.source       = { :git => "http://EXAMPLE/MyComponent.git", :tag => "#{spec.version}" }


  # ――― Source Code ―――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
  #
  #  CocoaPods is smart about how it includes source code. For source files
  #  giving a folder will include any swift, h, m, mm, c & cpp files.
  #  For header files it will include any header in the folder.
  #  Not including the public_header_files will make all headers public.
  #

  spec.source_files  = "Classes", "Classes/**/*.{h,m}"
  spec.exclude_files = "Classes/Exclude"

  # spec.public_header_files = "Classes/**/*.h"


  # ――― Resources ―――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
  #
  #  A list of resources included with the Pod. These are copied into the
  #  target bundle with a build phase script. Anything else will be cleaned.
  #  You can preserve files from being cleaned, please don't preserve
  #  non-essential files like tests, examples and documentation.
  #

  # spec.resource  = "icon.png"
  # spec.resources = "Resources/*.png"

  # spec.preserve_paths = "FilesToSave", "MoreFilesToSave"


  # ――― Project Linking ―――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
  #
  #  Link your library with frameworks, or libraries. Libraries do not include
  #  the lib prefix of their name.
  #

  # spec.framework  = "SomeFramework"
  # spec.frameworks = "SomeFramework", "AnotherFramework"

  # spec.library   = "iconv"
  # spec.libraries = "iconv", "xml2"


  # ――― Project Settings ――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
  #
  #  If your library depends on compiler flags you can set them in the xcconfig hash
  #  where they will only apply to your library. If you depend on other Podspecs
  #  you can include multiple dependencies to ensure it works.

  # spec.requires_arc = true

  # spec.xcconfig = { "HEADER_SEARCH_PATHS" => "$(SDKROOT)/usr/include/libxml2" }
  # spec.dependency "JSONKit", "~> 1.4"

end

4.编辑 Podspec 文件

打开 YourLibrary.podspec 文件,在编辑器中进行修改,以符合你的库的具体情况。以下是 Podspec 文件的一个例子及其解释:

Pod::Spec.new do |s|
  s.name         = "YourLibrary"
  s.version      = "0.0.1"
  s.summary      = "A short description of YourLibrary."
  s.description  = <<-DESC
                   An optional longer description of YourLibrary.
                   DESC
  s.homepage     = "http://example.com/YourLibrary"
  s.license      = { :type => "MIT", :file => "LICENSE" }
  s.author       = { "Your Name" => "you@example.com" }
  s.source       = { :git => "https://github.com/yourusername/YourLibrary.git", :tag => "#{s.version}" }
  s.source_files = "Sources/**/*.{h,m,swift}"
  s.platform     = :ios, '10.0'
  s.swift_version = '5.0'
end
  • name: 库的名称。
  • version: 库的版本号。这个版本应该与 Git 标签(tag)一致。
  • summary: 库的简短描述。
  • description: 库的详细描述。
  • homepage: 库的主页 URL。
  • license: 许可证类型和文件位置。
  • author: 库的作者信息。
  • source: 指定库的源代码位置,通常是一个 Git 仓库。
  • source_files: 指定包括在库中的源文件。
  • platform: 指定库支持的平台及最低版本。
  • swift_version: 指定所需的 Swift 版本。

注意更新你组件库中的标签或分支

MyComponent.podspec 文件中的 s.source 参数,确保其指向正确的 URL 和分支或标签。

先确保你的组件库有你配置这个分支或标签,然后你再配置分支的名称或tag。

标签(Tag):

标签是指向 Git 仓库中某一特定提交的引用,通常用于标记发布点(如版本发布)。标签是静态的,指向特定的提交,不会随着更多的提交而变化。

在 podspec 文件中使用标签,通常意味着你指定了一个稳定的、用于发布的版本。这是最常见的用法,因为这确保了项目的依赖是固定且可预测的。例如:

s.source = { :git => 'https://gitee.com/fzym/my-component.git', :tag => '0.0.1' }

 分支(Branch):

分支是用于开发新功能、修复错误或进行实验而创建的代码的独立线路。创建分支可以让你在不影响主线(通常是 master 或 main 分支)的情况下开发和测试代码。

在 podspec 文件中指定分支,意味着 CocoaPods 将从这个特定分支拉取代码。这通常用于开发阶段,当你想要使用最新的尚未发布的代码时。

s.source = { :git => 'https://gitee.com/fzym/my-component.git', :branch => 'develop' }

这里,develop 分支可能包含最新的开发中的功能和修复。

 

5.验证 Podspec 文件

在完成编辑后,你需要验证 Podspec 文件来确保配置无误:

$ pod lib lint

 这个命令将检查你的 Podspec 文件是否有错误或者遗漏的必要信息。如果一切顺利,你将看到 "passed validation" 的消息。

6.将 Podspec 推送到你的私有 Spec 仓库

推送到私有 Spec 仓库:

一旦 Podspec 文件准备好并且验证通过,可以将其添加到你的私有 Spec 仓库:

$ pod repo add PrivateRepoName https://github.com/yourusername/PrivatePodSpecs.git
$ pod repo push PrivateRepoName YourLibrary.podspec

 这里 PrivateRepoName 是你给你的私有 Spec 仓库设定的本地名称。

重命名仓库:

如果需要重命名仓库,你可以这样做:

$ pod repo remove OldPrivateRepoName
$ pod repo add NewPrivateRepoName https://github.com/yourusername/PrivatePodSpecs.git

查看所有已添加的仓库:

如果你忘记了你为私有仓库设置的本地别名 PrivateRepoName,你可以很容易地查看你的 CocoaPods 配置来找到所有已添加的私有仓库及其别名。这可以通过在终端运行一个简单的命令来完成。

$ pod repo list

6.使用你的私有库

在项目的 Podfile 中指定你的私有 Spec 仓库和库:

source 'https://github.com/yourusername/PrivatePodSpecs.git'
source 'https://cdn.cocoapods.org/'

platform :ios, '10.0'

target 'YourTarget' do
  use_frameworks!
  pod 'YourLibrary', '~> 0.0.1'
end

然后运行:

$ pod install

7.更新库

当你需要更新你的库时:

  • 更新你的库代码。
  • 修改 Podspec 文件中的版本号,并确保更新 tag。
  • 推送新代码到你的库的 Git 仓库,并创建相应的新 tag。
  • 将更新后的 Podspec 推送到私有 Spec 仓库:
$ pod repo push PrivateRepoName YourLibrary.podspec
  • 在使用该库的项目中,运行 pod update 来拉取最新版本。

8.管理权限

由于你的库和 Spec 仓库是私有的,确保只向需要的团队成员和合作者提供访问权限。

相关推荐

  1. iOS cocoapods 升级

    2024-04-27 05:08:02       9 阅读
  2. 使用 Verdaccio 建立私有npm

    2024-04-27 05:08:02       8 阅读
  3. CocoaPods 安装使用

    2024-04-27 05:08:02       23 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-04-27 05:08:02       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-04-27 05:08:02       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-04-27 05:08:02       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-04-27 05:08:02       20 阅读

热门阅读

  1. NFT学习资料整理

    2024-04-27 05:08:02       12 阅读
  2. 丑数 II

    2024-04-27 05:08:02       24 阅读
  3. 【笔试题汇总】字节跳动2024 春招第二场

    2024-04-27 05:08:02       17 阅读
  4. 洛谷 P1541 [NOIP2010 提高组] 乌龟棋

    2024-04-27 05:08:02       16 阅读
  5. MySQL-笔记-07.试图及索引的应用

    2024-04-27 05:08:02       14 阅读