CocoaPods创建维护私有库

3,222 阅读3分钟

Cocoapods是iOS开发的包管理工具。本文主要介绍如何利用它管理自己或公司内部的组件库。 主要过程包括: 1、创建私有库空间Repo 2、创建pod项目 3、将pod提交到Repo

创建私有Spec Repo

Spec Repo是所有Pods的集合,是一个Git仓库。 例如GitHub上的项目通过Cocoapods管理后,默认会在~/.cocoapods/repos目录下的master或trunk。 因此我们需要创建类似的Spec Repo。 步骤如下: 1、在Git平台创建你的私有仓库,例如Github/Gitlab/Coding等平台,创建完成后会得到.git链接。 2、添加Spec Repo到cocoapods repo,执行命令

# pod repo add [Private Repo Name] [Git clone URL]
$ pod repo add YourSpecs https://github.com/xxx/yourSpecs.git

成功后会在~/.cocoapods/repos目录下看到/yourSpecs文件夹。

创建podspec

1、创建模版库
$ pod lib create testLib
# ...
# Running pod install on your new library.
# ...
# We will start you off by opening your project in Xcode

此步骤需要根据提示回答问题,生成模版工程。 成功后会自动执行 pod install,并且xcode自动打开Example/testLib.xcworkspace

2、在Git远端创建项目仓库并关联
# cd testLib项目
$ git remote add origin https://github.com/xxx/TestLib.git  #添加远端仓库
$ git push origin master     #提交到远端仓库
3、开发pod项目

将源码导入Pod/Classes,在Example中pod update后,会在Pods子工程看到添加的文件。

将pod提交到Repo

当pod组件开发完成后,需要通过测试并提交版本到Repo。

1、配置podspec文件

打开.podspec文件 修改s.source为你的Git路径。 以及homepageversion等信息。

s.source = { :git => "https://github.com/xxx/TestLib.git"}
2、提交代码,打标
$ cd testLib/
$ git add .
$ git commit -s -m "Initial Commit of Library"
$ git remote add origin https://github.com/xxx/TestLib.git
$ git push origin master
$ git tag -m "tag message" 0.1.0
$ git push --tags
3、验证
$ pod lib lint
$ pod lib lint TestLib.podspec --sources='https://github.com/CocoaPods/Specs.git,https://github.com/xxx/yourSpecs.git' #关联了内部pod的验证
# --allow-warnings 忽略警告
# --verbose 进度详情

testLib passed validation. 为验证通过。

4、提交

Spec Repo提交podspec必须保证podspec验证通过。 提交命令为:

$ pod repo push yourSpecs testLib.podspec  #前面是本地Repo名字 后面是podspec名字

成功后~/.cocoapods/repos/yourSpecs下会看到testLib

使用制作好的pod

在完成这一系列步骤之后,我们就可以在正式项目中使用这个私有的Pod了。 只需要在项目的Podfile中增加如下配置:

source 'https://github.com/xxx/yourSpecs.git'
pod 'testLib', '0.1.0'

持续维护Pod的操作步骤

  • 1、修改Pod代码(引入库需要在spec文件配置 s.dependency后在example pod update)
  • 2、example测试
  • 3、编写readme、update文档
  • 4、修订podspec文件版本等信息
  • 5、验证命令:pod lib lint
  • 6、testPod项目引用测试
  • 7、提交子项目代码到远端
  • 8、打标
    • git tag -m "first release" 0.1.0
    • git push --tags
  • 9、同步到 Specs
    • pod repo push yourSpecs xxx.podspec --allow-warnings
  • 10、到本地及远端目录查验版本及文件
    • ~/.cocoapods/repos/yourSpecs

常见错误

官方 CocoaPods issues

常见异常

  • 编译异常:include of non-modular header inside framework module

    • 解决办法:
      • 1、检查umbrella.h 中的头文件
      • 2、Select the .h file in the project navigator. In the target membership area on the right there is a drop down menu next to the target. Select "public" there (probably "project" is selected right now).
      • 3、buldsetting all中设置 Allow Non-modular Includes In Framework Modules 为 YES
  • 编译异常:Specs satisfying the xxxKit dependency were found, but they required a higher minimum deployment target.

    • 解决办法:platform :ios, '8.2' 调整版本号
  • pod repo push yourSpecs 时:specification does not validate

    • 解决办法--allow-warnings
  • ERROR | [iOS] file patterns: The source_files pattern did not match any file.

    • 参考链接进入相应文件夹,创建文件夹与source_files文件路径对应 (~/Library/Caches/CocoaPods/Pods/External/ProjectName/035cb9aa62b9d49f904fad1119b8)
  • pod repo push yourSpecs testLib.podspec: No such file or directory

    • pod repo push yourSpecs testLib.podspec --verbose --use-libraries --allow-warnings

参考

wtlucky's Blog

官方配置说明