iOS 私有库组件化:CocoaPods搭建私有仓库

因为公司中有多个iOS项目进行开发,所以需要搭建一些基础功能库供多个项目使用,用CocoaPods的方式管理私有库。

这里以其中一个库作为例子来介绍进行私有仓库组件化(因为项目中已经有私有库了,重新理一遍流程仅供参考学习,还有私有库维护的命令记录)

一、CocoaPods公共仓库

  • 查看 cocoapods 本地目录

前往文件夹,输入:

Ruby 复制代码
~/.cocoapods/repos

跳转至本地cocoapods目录,如下所示。

cocoapods本地目录如下所示:

tsinghua-git-cocoapods:是cocoapods对应的公共git仓库(这里用的是清华源,如果是原源的是master)

iosSpecs:是自己创建的私有仓库地址

二、搭建自己的pods私有库

1. 创建私有的Spec Repo

  • 在github上创建一个iosSpecs仓库

具体步骤:登录github --> 点击右上角"+" --> 选择 new repository --> 输入Repository name为 iosSpecs ,选择仓库类型为 private,点击Create repository。

到此为止,iosSpecs私有仓库就创建好了。

  • 在终端以下执行命令,将仓库添加到 ~/.cocoapods/repos 目录下
Ruby 复制代码
pod repo add iosSpecs https://github.com/xxx/iosSpecs.git

2. 创建pods工程

  • 在桌面创建一个test文件夹目录,终端 cd 到 test 目录

  • 执行终端命令

Ruby 复制代码
pod lib create pod_test

如下图所示,依次输入的次序:ios、swift、no、none、no

  • 成功后,会在test目录下创建一个pod_test工程。

3. 提交pods库到github上

  • 首先在 github 创建一个私有仓库,步骤如上面所示。

  • 在终端,cd 到 pod_test 目录下,执行以下终端命令

Ruby 复制代码
$ git init
$ git add .
$ git commit -am "第一次提交" 
// 即第一个步骤中创建的仓库地址
$ git remote add origin https://github.com/xxx/pod_test.git
$ git push origin master 
// podspec文件中获取Git版本控制的项目需要tag号
$ git tag -m "first release" "0.1.0" 
$ git push --tags

4. 配置pod_test的podspec文件

  • 打开 pod_test 目录下的 pod_test.podspec 文件,这里以项目中的拓展库 YZExtension 为例子,具体解释在项目中都有标注了:
Ruby 复制代码
#
# Be sure to run `pod lib lint YZExtension.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             = 'YZExtension'
  s.version          = '1.3.0'
  s.summary          = 'ios extension 扩展库'

# 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
                  ios 组件 extension 扩展库
                         DESC
    # 这里写自己的git仓库地址
    s.homepage         = 'http://git.xxxxxx.cn/xxxxxx/YZExtension.git'
    # s.screenshots     = 'www.example.com/screenshots_1', 'www.example.com/screenshots_2'
    s.license          = { :type => 'MIT', :file => 'LICENSE' }
    # 这里写自己的联系地址
    s.author           = { 'xxx' => 'xxxxxxxxx@qq.com' }
    # 这里写自己的git仓库地址
    s.source           = { :git => 'http://git.xxxxxx.cn/xxxxxx/YZExtension.git', :tag => s.version.to_s }
    # s.social_media_url = 'https://twitter.com/<TWITTER_USERNAME>'
    # iOS开发目标版本(与实际项目一致就好)
    s.ios.deployment_target = '11.0'
    # swift版本
    s.swift_version = '5.0'

    # modulemap要配置在source_files中,配置在module_map中不起作用

    s.source_files = 'YZExtension/Classes/**/*'

#  s.preserve_path = "YZExtension/Classes/OCClass/module.modulemap"
#  s.xcconfig = { "SWIFT_INCLUDE_PATHS" => "$(PODS_ROOT)/YZExtension/OCClass"}

#  s.static_framework = true

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

    # s.public_header_files = 'Pod/Classes/**/*.h'
    # s.frameworks = 'UIKit', 'MapKit'
    # 依赖外部的库
    s.dependency 'Kingfisher', '~> 6.3.1'
end
  • 终端 cd 到 pod_test 目录,验证 podspec 文件,验证时可以忽略这些警告,使验证通过:
Ruby 复制代码
pod spec lint --allow-warnings

5. 在Example工程修改podfile文件

修改podfile文件,并执行 pod update

lua 复制代码
use_frameworks!
target 'pod_test_Tests' do
    #pod 'pod_test', :path => '../'
    pod 'pod_test', :path => '../pod_test.podspec'
end

6. 把pod_test.podspec提交到iosSpecs仓库

cd 到 pod_test目录,执行以下命令

css 复制代码
pod repo push [本地Spec Repo名称][podspec文件路径]
$ pod repo push iosSpecs pod_test.podspec

如果报错误(红色文字)

解决办法1:

Ruby 复制代码
cd ~/.cocoapods/repos/iosSpecs,git clean -f

解决办法2:移除~/.cocoapods/repos/目录下的Myspec,执行添加命令,再重新执行提交

Ruby 复制代码
pod repo add iosSpecs https://github.com/xxx/iosSpecs.git

提交成功后,打开 ~/.cocoapods/repos/ 目录,可以看到我们提交的私有仓库了,在对应的spec仓库地址也能看到提交记录。

三、使用

到主项目的工程里的podfile添加:

Ruby 复制代码
#私有spec仓库的地址,而不是某个pod仓库的地址
source 'https://github.com/xxx/iosSpecs'

# 注意要指定版本号!
target 'yzay' do
    pod 'pod_test', '0.1.0' # 私有库
end

再执行pod install,可以在新工程中的pods文件夹中看到 pod_test对应的文件夹。

到此,算是完成pods私有库的搭建了。

四、更新pods

例如:创建一个String+.swift的文件,里面的实现的String的扩展方法

注意:

  • 新建的文件必须放在 Classes 文件夹内

  • 如果这个文件是需要对外公开的,需要在 class 前面加上 public

  • pods工程每次修改后,在podspec文件修改版本号,提交修改推送到git仓库,给git仓库的提交记录打上标签。

在实际运行中,到仓库目录中执行以下命令:

Ruby 复制代码
// 验证仓库,忽视警告
pod spec lint --allow-warnings
// 推送私有库,忽视警告
pod repo push iosSpecs YZExtension.podspec --allow-warnings
  • 主项目中,如同更新第三方仓库一样,指定私有库到最新版本

然后在项目的目录中,打开终端执行命令:

Ruby 复制代码
pod repo update

至此,私有库更新完成!

五、发布spec

发布主要是用于公开的framework类的组件用于发布到cocoapods

  • 发布之前需要有cocoapods trunk账号,注册密令如下:
Ruby 复制代码
$ pod trunk register [Your-Email] '[Your-Name]' --description='[Your-Desc]'
> [Your-Email]: 任意邮件
> [Your-Name]: 推荐使用github上使用的Name
> [Your-Desc]: 使用电脑的描述
// eg:
$ pod trunk register xxxx@163.com 'pod_test' --description='pod_test'
// 注册完成后,[pod trunk me]查看信息
$ pod trunk me
  • 验证podspec:pod spec lint

  • 发布podspec

Ruby 复制代码
$ pod trunk push [NAME].podspec
或者
$ pod trunk push [NAME].podspec --allow-warnings

4、成功发布后,看看能不能search到

ruby 复制代码
/ 先更新一下repo
$ pod repo update
$ pod search 'pod_test'

最最最后,完结撒花

相关推荐
YungFan1 小时前
iOS开发之网络代码进化史
ios·swift
SunshineBrother7 小时前
shell脚本,怎么查找项目中的重复图片
ios
月未央7 小时前
HarmonyOS Next 开发系列:Provider和Consumer状态修饰器实践
ios·harmonyos
北京自在科技1 天前
【Find My功能科普】防盗黑科技如何改变生活?
科技·ios·生活·findmy
水木姚姚2 天前
图形界面控件编程(iOS)
人工智能·python·macos·ios·xcode
书弋江山2 天前
Flutter 调用原生IOS接口
flutter·ios·cocoa
q567315232 天前
使用ASIWebPageRequest库编写Objective-C下载器程序
android·开发语言·macos·ios·objective-c·iphone
星海拾遗2 天前
debug_unpack_ios failed: Exception: Failed to codesign 解决方案(亲测有效)
flutter·ios
水木姚姚2 天前
视频软件编程(iOS)
macos·ios·objective-c·音视频·xcode
L_Jason先生2 天前
iOS 聊天 IM 消息收发管理工具
前端·ios·设计模式·cocoa·责任链模式·适配器模式