ios 组件化之Cocoapods私有库详解以及问题解决方案

2,134 阅读4分钟

如何制作私有仓库

在做组件化操作之前有个必须的操作,那就是如何制作私有仓库,以及私有仓库之间的引用问题。

私有仓库 => 私有仓库 => 私有组件

1、创建远端Spec仓库

该仓库的目的作用就是存储私有库spec索引

2、创建本地索引库并和远程索引库进行关联

  • 本地添加spec仓库
pod repo add [Spec仓库名] [Spec仓库地址]

例如:pod repo add PrivatePod git@github.com:xxxx/PrivatePod.git

  • 查看
pod repo list
PrivatePod
- Type: git (master)
- URL:  git@github.com:xxxx/PrivatePod.git
- Path: /Users/aba/.cocoapods/repos/PrivatePod
  • 移除本地索引PrivatePod
pod repo remove PrivatePod

3、提交私有组件

  • 指定到组件目录
cd 组件文件路径
  • 添加标签
git tag -a 0.0.1 -m 'release 0.0.1'
  • 上传至远端
git push origin --tags
  • 提交索引文件至远程索引库
pod repo push [Spec仓库名] [私有库索引文件名(.podspec)]

例如:pod repo push PrivatePod blockchain-ios.podspec --allow-warnings

忽略警告在后面添加--verbose --allow-warnings

如果添加第三方库并包含静态包时需使用--use-libraries

采用CTMediator组件化时刻,Swift发布组件需带上--use-modular-headers

例如:pod repo push PrivatePod KJCategories.podspec --verbose --allow-warnings --use-libraries --use-modular-headers

  • Podspec参数说明
--help      显示指定命令的帮助横幅
--verbose   显示更多调试信息
--silent    显示所有信息
--allow-warnings   忽略警告
--use-libraries    使用静态库安装
--use-modular-headers       OC与Swift混编必须添加
--skip-import-validation    跳过验证pod是否可以导入
--skip-tests     在验证期间跳过构建和运行测试
--use-json       在将其推送到repo之前,将podspec转换为JSON
--swift-version=VERSION     在标记规范时应该使用的SWIFT_VERSION.这优先于规范中指定的Swift版本或. Swift版本文
  • 成功之后更新索引
pod setup

到此私有Pod与制作就差不多完成

Pod私有组件使用

  • 第一种:链接地址使用
pod 'blockchain-ios',:git => 'https://github.com/xxxx/blockchain-ios'
  • 第二种:更换Source源
source 'git@github.com:xxxx/PrivatePod.git'
  • Podfile文件内容大致如下:
# Uncomment the next line to define a global platform for your project

source 'https://github.com/CocoaPods/Specs.git'
source 'git@github.com:xxxx/PrivatePod.git' # 私有索引

platform :ios, '10.0' # 这个版本为所有CocoaPods里面`s.ios.deployment_target`支持的最低版本
inhibit_all_warnings!
use_frameworks!

## 远端私有组件
def private_pods
  
  ## 私有组件库
  pod 'KJCategories'
end

## 本地组件
def modules_pods
  ## 发现模块
  pod 'WMDiscover', :path => '../WMModules/WMDiscover'
  ## 我的模块
  pod 'WMMine', :path => '../WMModules/WMMine'
  ## 钱包首页
  pod 'WMWallet', :path => '../WMModules/WMWallet'
  ## DApp
  pod 'WMDApp', :path => '../WMModules/WMDApp'
  
end

target 'MainProject_Example' do

  ## Root管理
  pod 'RootManager', :path => '../RootManager'

  ## 主tabBar
  pod 'AppMain', :path => '../AppMain'

  ## 路由组件
  pod 'Mediator', :path => '../Mediator'

  ## 公共部分
  pod 'FeatBox', :path => '../FeatBox'

  ## 数据库部分
  pod 'Database', :path => '../Database'

  private_pods
  
  modules_pods

end

target 'MainProject_Tests' do
  inherit! :search_paths

end

Pod快捷上传至私有库

  • 正常我们上传至私有库都是通过
pod repo push [Spec仓库名] [私有库索引文件名(.podspec)]

  • 由于上述方式比较费时间,可以采用如下方式:

    • 确保 podspec 文件正确,pod lib lint --allow-warnings如果确信podspec没问题,亦可省略该步骤
    • 克隆私有仓库至本地,git clone 仓库地址
    • 将要上传库 podspec 文件按正确格式要求放入私有仓库
      • Ex: 版本0.0.2则文件夹名命名为0.0.2
    • 然后正常上传提交代码至远端仓库
    • 添加本地标签,git tag -a 0.0.1 -m 'release 0.0.1'
    • 上传标签至远端,git push origin --tags
    • 最后更新本地私有库 repo 索引,pod repo update PrivatePod
  • 大致层级结构如下:


常见错误总结

  • 1、报错:remote: Support for password authentication was removed on August 13, 2021. Please use a personal access token instead.

    • 解决方案:将 KJCategories.podspec 文件的 Source 更换为SSH地址
    s.source = { 
      :git => 'git@github.com:Condy/KJCategories.git', 
      :tag => s.version.to_s 
    }
    
  • 2、报错:Failed to open TCP connection to github.com:443

    • 解决方案:在Safari浏览器打开https://github.com/CocoaPods/Specs.git
  • 3、报错:Your configuration specifies to merge with the ref 'refs/heads/master' from the remote, but no such ref was fetched.

    • 解决方案:由于[Spec仓库]是个空仓库,需要在里面随便放点东西,例如README.md
  • 4、报错:Specs satisfying the KJCategories dependency were found, but they required a higher minimum deployment target.) during validation.

    • 原因分析:三方依赖库KJCategories支持的最低版本s.ios.deployment_target = '9.0',而本库的podspec文件中指定的最低支持版本低于该三方依赖库
    • 解决方案:将本库的最低支持版本修改至大于或等于三方依赖库

最后

  • 再附上一个开发加速库KJCategoriesDemo地址 🎷喜欢的老板们就点个星吧,别犹豫了。🌟

✌️.