cocoapods创建私有库

作为一名懒人,写博客的事情基本不做,但是最近做pod私有库的时候发现之前的东西忘掉差不多了,于是即兴写一篇,记录下来,以便下次再次回忆。

pod创建私有库主要有几个步骤

  • 确定pod版本。新版本1.8.4版本(没试过1.9.0beta),由于CDN的问题,做私有库容易索引失败,setup master的时候容易失败。建议使用1.7.x版本。
  • 使用pod creat命令创建项目工程。
  • 修改相关库文件。
  • 修改.podspec文件包括:三方依赖、分组、版本号等。
  • 创建两个远程git仓库,并初始化。一个spec用来存储.podspec文件,一个lib库用来存储代码。
  • 关联git库,上传本地文件到私有库。
  • git库打tag。
  • 使用pod lib lint 和 pod spec lint 验证.podspec合法性。
  • 上传.podspec 文件到spec库。
  • 下载测试使用。

正式开始

第一步 创建pod私有库工程

  1. 找到合适的目录使用 pod lib create YDPodDemoLib 其中YDPodDemoLib是我自己创建的私有库名称。
    这一步会有一些自动提示,
➜  PodTest pod lib create YDPodDemoLib            
Cloning `https://github.com/CocoaPods/pod-template.git` into `YDPodDemoLib`.
Configuring YDPodDemoLib template.

------------------------------

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还是mac工程
> iOS

What language do you want to use?? [ Swift / ObjC ]
//使用哪种语言
> ObjC

Would you like to include a demo application with your library? [ Yes / No ]
//是否包含demo工程
> Yes

Which testing frameworks will you use? [ Specta / Kiwi / None ]
//使用那种frameworks
> None

Would you like to do view based testing? [ Yes / No ]
//是否使用试图测试
> Yes

What is your class prefix?
//项目前缀
> YD

Running pod install on your new library.

Analyzing dependencies
Fetching podspec for `YDPodDemoLib` from `../`
Downloading dependencies
Installing FBSnapshotTestCase (2.1.4)
Installing YDPodDemoLib (0.1.0)
Generating Pods project
Integrating client project

[!] Please close any current Xcode sessions and use `YDPodDemoLib.xcworkspace` for this project from now on.
Sending stats
Pod installation complete! There are 2 dependencies from the Podfile and 2 total pods installed.

Ace! you're ready to go!
We will start you off by opening your project in Xcode
 open 'YDPodDemoLib/Example/YDPodDemoLib.xcworkspace'

To learn more about the template see `https://github.com/CocoaPods/pod-template.git`.
To learn more about creating a new pod, see `https://guides.cocoapods.org/making/making-a-cocoapod`.
  1. 替换文件
    进入到YDPodDemoLib/YDPodDemoLib/Classes文件夹下,删除ReplaceMe.m文件。并且将你自己编写的文件拷贝到这个文件夹下,source文件放到Assets下。


    image.png
  2. cd到Example下可以使用pod install,可以在pod目录下看到你添加的文件。如下图:


    image.png

第二步 修改podspec文件

打开demo工程,在最上面能看到一个.podspec文件

  1. 修改source文件
    这里需要修改的文件如下图


    image.png

第三步 关联git仓库

  1. 创建仓库。
    这里需要创建两个仓库:spec、lib。比如我这里创建的是podDemoSpec、YDPodDemoLib。
  2. 关联lib仓库,使用如下一系列命令
$ git remote add origin https://github.com/yidezhang/podDemo/YDPodDemoLib.git
$ git add .
$ git commit -m "Initial commit"
$ git push -u origin master 
//tag 值要和.podspec中的version一致
$ git tag 0.1.0
//推送tag到服务器上
$ git push --tags

第四步 验证.podspec文件合法性

  1. 验证.podspec
    在.podspec文件被设置好后,需要使用pod验证其合法性。使用如下两条命令:
pod lib lint --allow-warnings --verbose  
pod spec lint xxxx.podspec --verbose --allow-warnings 

如果运行命令的时候报错,自行查阅相关文档。各个报错都不一样,一般都是.podspec写的不合法。

第五步 将创建的spec添加到pod里面

其实通过上面的操作,直接使用source修改,通过pod已经可以pod下载私有库了,但是这只是简单的拷贝。要使得私有库更加隐蔽和便于管理,需要在pod repo里面添加我们自己定义的spec文件(也就是.podspec)管理。所以远程需要创建两个git仓库,一个用来管理spec版本,一个存储真正的lib文件。

  1. 进入~/.cocoapods/repos里面,可以看到本地的一些spec索引文件,其中master是pod远程库的搜索目录,我们平时使用pod setup命令的时候就是下载这个master库文件。现在我们要做的是将我们的私有库添加到索引里。

  2. 关联远程spec库。
    这一步其实就是将远程的spec库关联到本地,并且给他起个别名。命令如下:

pod repo add YDPodDemoLibSpec https://github.com/yidezhang/podDemo/podDemoSpec.git

这个时候在~/.cocoapods/repos就会看到上面会有刚刚执行的YDPodDemoLibSpec 文件夹,如下图:


image.png

3.修改版本号
我们每次修改私有库文件,当需要发布版本的时候就需要创建新的tag,上传新的spec文件。命令如下:

//这里需要cd到 YDPodDemoLib主目录下,而不是在demo下面。
pod repo push YDPodDemoLibSpec YDPodDemoLib.podspec --allow-warnings     

最后验证

要使用私有库,需要在podfile里面添加source

source 'https://github.com/yidezhang/podDemo/podDemoSpec.git'

platform :ios, '9.0'

target 'TestLayer' do
  use_frameworks!
  pod 'YDPodDemoLib'
end

验证即可。

后记

需要注意的点:
  1. 制作私有库其实比较简单,只要理清楚spec文件和lib库的归属就好办了。
  2. 但是由于pod版本问题,私有库依赖三方库的时候容易出错,这里要注意引入三方库版本。
  3. pod的分组其实就是嵌套编写,分组的时候注意每个小组三方库的引入版本是否冲突。
简单画了两张图,仅供参考

pod私有库上传的流程大概如图:

image.png

pod私有库使用大概如下:

image.png

如果遇到问题include of non-modular header inside framework module 报错就在podspec文件里加入下面这句话

  s.user_target_xcconfig = { 'CLANG_ALLOW_NON_MODULAR_INCLUDES_IN_FRAMEWORK_MODULES' => 'YES' }

你可能感兴趣的:(cocoapods创建私有库)