如何使用CocoaPods制作私有库

7,802 阅读5分钟

「这是我参与2022首次更文挑战的第3天,活动详情查看:2022首次更文挑战」。

前言

在前面的章节中有介绍过CocoaPods的使用:

CocoaPods公有库以及私有库的区别

制作Framework以及上传至CocoaPods库

此篇文章会着重介绍下制作过程以及记录过程遇到的一些问题和解决办法。

制作过程

首先,我们要先了解整个制作过程,这样我们来快速的了解下制作的整体流程,以便于我们在后面的制作中有一个清晰的步骤。

  1. 创建私有的源仓库 (如果已经存在源仓库,忽略此步)
  2. 创建私有的Pod仓库
  3. 添加私有库源文件 (添加代码、资源、包等等)
  4. 修改.podSpec文件
  5. 验证.podSpec文件
  6. 提交代码至远程仓库
  7. 推送.podSpec文件

创建私有的源仓库

1.在远端仓库中,创建一个源索引库PodSpecs。 (远端仓库可以选择githubgitlab码云

2.将远端索引库添加到本地源中(使用终端输入以下命令)

/**
pod repo add [repoName] [source]
*/ 
pod repo add component https://github.com/component/specs.git

创建私有的Pod仓库

  1. 在远端仓库中,创建一个源代码pod库。 注意⚠️:这里先不着急关联到本地。
  2. 选择一个目录下,创建本地Pod库工程。
/**
pod lib create  Pod库名
*/
pod lib create RTCComponent

执行完成后,命令行会有一系列的问题,按需填写即可;

To get you started we need to ask a few questions, this should only take a minute.

If this is your first time we recommend running through with the guide: 
 - https://guides.cocoapods.org/making/using-pod-lib-create.html
 ( hold cmd and double click links to open in a browser. )


What platform do you want to use?? [ iOS / macOS ]
 > iOS

What language do you want to use?? [ Swift / ObjC ]
 > Swift

Would you like to include a demo application with your library? [ Yes / No ]
 > yes

Which testing frameworks will you use? [ Quick / None ]
 > none

Would you like to do view based testing? [ Yes / No ]
 > no

命令执行完后会帮我们创建一个Workspace,里面包含了两个Project, RTCComponent是我们Pod库的运行Demo工程;Pods是我们开发库的代码工程。

项目层级

添加私有库源文件

我们可以在Pods/Development Pods/RTCComponent目录下导入我们的代码文件或者framework。在这一步就是将我们代码以及资源文件都放置到项目中,导入的文件以及资源目录后面需要在.podSpec文件中配置, 这里就不过多说明。

修改.podSpec文件

Pod::Spec.new do |s|
  s.name             = 'RTCComponent'
  s.version          = '0.1.0'
  s.summary          = '组件'
  s.description      = 'xxxxxxxxxxxxxxxxx'
  s.homepage         = 'http://xxxx/component'
  # s.screenshots     = 'www.example.com/screenshots_1', 'www.example.com/screenshots_2'
  s.license          = { :type => 'MIT', :file => 'LICENSE' }. 
  s.author           = { 'xxxxxxxx' => 'xxxxxxxxx@qq.com' }
  s.source           = { :git => 'http://xxxx/component/RTCComponent.git', :tag => s.version.to_s }
  # s.social_media_url = 'https://twitter.com/<TWITTER_USERNAME>'
  s.ios.deployment_target = '9.0'

  # 这里是源文件的路径 
  s.source_files = 'RTCComponent/Classes/**/*'
  
  # 这里是资源文件的路径 
  # s.resource_bundles = {
  #   'RTCComponent' => ['RTCComponent/Assets/*.png']
  # }
  # 这里是头文件的路径 
  # s.public_header_files = 'Pod/Classes/**/*.h'

  # 如果导入了framework
  s.frameworks = 'OpenAL', 'Accelerate'

  # 如果依赖了library(记得把lib前缀,以及.tbd去掉)
  s.libraries  = 'sqlite3', 'resolv', 'c++', 'z'

  # 如果依赖了三方pod库
  s.dependency 'HandyJSON', '~> 5.0.2'
  s.dependency 'TXLiteAVSDK_Professional', '~> 8.9.10382'
  s.dependency 'SwiftyBeaver', '~> 1.9.3' # Log
  s.dependency 'SwifterSwift/SwiftStdlib'

  # 如果需要修改pod中的target设置,写在这里
  s.pod_target_xcconfig = { 'VALID_ARCHS' => 'x86_64 armv7 arm64', 'ENABLE_BITCODE' => 'NO' }
  #  s.user_target_xcconfig = { 'ENABLE_BITCODE' => 'NO' }
end

这里如果需要设置请参照 CocoaPods官网文档

验证.podSpec文件

cd到该目录下;执行以下操作:

/** 网络校验 */
pod spec lint 

/** 本地校验 */
pod lib lint

常用附加处理:

  • 查看详细信息,请在命令后加入 --verbose
  • 忽略警告,请在命令后加入 --allow-warnings
  • 使用本地库,请在命令后加入 --use-libraries
  • 检查所有问题,请在命令后加入 --no-clean
  • 依赖了私有库,需要添加源,请在命令后加入 --sources=注意:如果依赖了公有库,还需要添加公有库源:https://github.com/CocoaPods/Specs--sources=私有库名,https://github.com/CocoaPods/Specs

提交代码至远程仓库

将本地pod库工程提交推送到远程仓库,打上Tag发布。 1.提交本地仓库,并推送

/** cd Pod工程目录 */  
cd PodProject
/** git commit -m "提交信息" */  
git commit -m "commit"
/** git branch -M 分支 */  
git branch -M main
/** git remote add origin git仓库地址 */  
git remote add origin https://github.com/XXXX.git
/** git push -u origin 分支 */  
git push -u origin main

2.打上Tag发布

/** 新建tag git tag [tagName] */
git tag "0.0.1" 
/** 推送单个tag至远端 git push origin [tagName] */
git push origin "0.0.1" 
/** 推送本地所有tag  git push origin --tags */
git push origin --tags

推送.podSpec文件

检查下本地repo,如果有,继续下一步,反之,则重新在添加一次(pod repo add [name] [源仓库git地址]);

pod repo list

本地仓库检查无误后,开始将创建的Pod库中的.podspec文件推送至指定源仓库; 首先,还是cd.podspec文件的目录下,执行以下操作:

/** 推送命令: pod repo push [repoName] [name].podspec */ 
pod repo push component RTCComponent.podspec 

到这里所有流程已经完成;整个过程中的验证过程可能是最容易出问题的,如果验证通过,那么发布就不会有什么问题,所以如果验证没有通过的话,发布也是不会成功的;

文章的最后,我会把制作过程中所遇到的问题一一汇总。(后续如果有遇到的其他问题,也会在这里更新)

问题汇总(Error)

  • 问题1: 校验失败
** BUILD FAILED **
The following build commands failed:
    CompileSwift normal x86_64
    CompileSwiftSources normal i386 com.apple.xcode.tools.swift.compiler
    CompileSwift normal i386
    CompileSwift normal arm64
(4 failures)
Testing with `xcodebuild`. 

解决办法:在.podspec文件中加入;参考iOS 指令集架构

s.pod_target_xcconfig = { 'VALID_ARCHS' => 'x86_64 armv7 arm64' }
  • 问题2: 组件中依赖的第三方库中有framework或者.a文件, pod install 报错
target has transitive dependencies that include statically linked binaries:

解决办法: 在podfile文件中加入以下代码;

pre_install do |installerPod::Installer::Xcode::TargetValidator.send(:define_method, :verify_no_static_framework_transitive_dependencies) {}
end
  • 问题3: Xcode setting ENABLE_BITCODE
You must rebuild it with bitcode enabled (Xcode setting ENABLE_BITCODE) 

解决办法:将target下 ENABLE_BITCODE 设置为 NO

参考文档:

CocoaPods官方制作文档

用 CocoaPods 私有库提高团队的整体效率

GitHub 将使用 main替换掉 master等术语