如何制作Pod库

作者: iTruda | 来源:发表于2019-04-24 20:32 被阅读342次

需求:

  1. 管理一些常用的类或者第三方的SDK;
  2. 组件化开发;

具体步骤:

1. 打开终端,进入要建立私有库项目工程的路径,并执行pod lib create [pod name] 创建并下载Cocoapods模板;

过程中会有一些选项,按需选择即可,所有选项选择完成后会自动打开工程。

选项如下:

➜  XHC-iOS-Libs pod lib create EncryptionMethods
Cloning `https://github.com/CocoaPods/pod-template.git` into `EncryptionMethods`.
Configuring EncryptionMethods 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

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

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

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

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

What is your class prefix?
 > LS

Running pod install on your new library.

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

[!] Please close any current Xcode sessions and use `EncryptionMethods.xcworkspace` for this project from now on.
Sending stats
Pod installation complete! There is 1 dependency from the Podfile and 1 total pod installed.

[!] Automatically assigning platform `ios` with version `9.3` on target `EncryptionMethods_Tests` because no platform was specified. Please specify a platform for this target in your Podfile. See `https://guides.cocoapods.org/syntax/podfile.html#platform`.

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

To learn more about the template see `https://github.com/CocoaPods/pod-template.git`.
To learn more about creating a new pod, see `http://guides.cocoapods.org/making/making-a-cocoapod`.
➜  XHC-iOS-Libs 

2. 添加源码文件

将源码文件复制到文件夹路径:[YourPodName]/[YourPodName]/Classes 下,如图:

代码放置路径.png
3. 创建私有库获取 git 地址,并完善[YourPodName].podspec 配置文件

复制链接,下面编辑 [YourPodName].podspec 更改 s.homepages.source 使用

私有库地址.png
4. 编辑 CocoaPods 配置文件 [YourPodName].podspec(可以直接在 Xcode 中编辑,也可以用 Sublime Text 或 Visual Studio Code 等编辑都可以)

我的编辑,仅做参考

s.summary          = 'XHC EncryptionMethods pod Use.'
s.homepage         = 'http://172.17.0.18/XHC-iOS-Libs/EncryptionMethods.git'
s.source           = { :git => 'http://172.17.0.18/XHC-iOS-Libs/EncryptionMethods.git', :tag => s.version.to_s }
  • 如果 homepage 有更好的展示页面,可以直接填你的展示页面;
  • 如果是 MRC 的,不支持 ARC 可添加下面代码 ;
s.requires_arc = false
  • 还有其他一些属性配置,有兴趣的小伙伴可以自己试试;
编辑podspec.png
5. 进入我们的 Example 文件,执行 pod update --no-repo-update 指令,安装本地库源码
➜  Example git:(master) ✗ pod update --no-repo-update
Update all pods
Analyzing dependencies
Fetching podspec for `EncryptionMethods` from `../`
Downloading dependencies
Installing EncryptionMethods 0.1.0
Generating Pods project
Integrating client project
Sending stats
Pod installation complete! There is 1 dependency from the Podfile and 1 total pod installed.

查看 Example 下的项目已经导入成功


pod 库导入.png
5. 本地 pod 配置文件验证合法性

为了保证项目的正确性,尤其是 pod 配置文件的正确性,在正式提交前,我们需要执行以下本地验证。在本地验证正常的情况下,再上传发布还是比较稳妥的。

返回我们的项目根目录,执行 pod 本地验证指令:

  • pod lib lint (如果有错误,可能会产生很多警告⚠️和错误❌)
  • 其他参数:
Usage:

    $ pod lib lint

      Validates the Pod using the files in the working directory.

Options:

    --quick                                           Lint skips checks that would
                                                      require to download and build
                                                      the spec
    --allow-warnings                                  Lint validates even if warnings
                                                      are present
    --subspec=NAME                                    Lint validates only the given
                                                      subspec
    --no-subspecs                                     Lint skips validation of
                                                      subspecs
    --no-clean                                        Lint leaves the build directory
                                                      intact for inspection
    --fail-fast                                       Lint stops on the first failing
                                                      platform or subspec
    --use-libraries                                   Lint uses static libraries to
                                                      install the spec
    --use-modular-headers                             Lint uses modular headers during
                                                      installation
    --sources=https://github.com/artsy/Specs,master   The sources from which to pull
                                                      dependent pods (defaults to
                                                      https://github.com/CocoaPods/Specs.git).
                                                      Multiple sources must be
                                                      comma-delimited.
    --platforms=ios,macos                             Lint against specific
                                                      platforms(defaults to all
                                                      platforms supported by the
                                                      podspec).Multiple platforms must
                                                      be comma-delimited
    --private                                         Lint skips checks that apply
                                                      only to public specs
    --swift-version=VERSION                           The SWIFT_VERSION that should be
                                                      used to lint the spec. This
                                                      takes precedence over a
                                                      .swift-version file.
    --skip-import-validation                          Lint skips validating that the
                                                      pod can be imported
    --skip-tests                                      Lint skips building and running
                                                      tests during validation
    --silent                                          Show nothing
    --verbose                                         Show more debugging information
    --no-ansi                                         Show output without ANSI codes
    --help                                            Show help banner of specified
                                                      command

终端展示如下:

➜  EncryptionMethods git:(master) ✗ pod lib lint         

 -> EncryptionMethods (0.1.0)
    - NOTE  | xcodebuild:  note: Using new build system
    - NOTE  | [iOS] xcodebuild:  note: Planning build
    - NOTE  | [iOS] xcodebuild:  note: Constructing build description

EncryptionMethods passed validation.
➜  EncryptionMethods git:(master) ✗ pod lib lint --allow-warnings   

 -> EncryptionMethods (0.1.0)
    - NOTE  | xcodebuild:  note: Using new build system
    - NOTE  | [iOS] xcodebuild:  note: Planning build
    - NOTE  | [iOS] xcodebuild:  note: Constructing build description

EncryptionMethods passed validation.
➜  EncryptionMethods git:(master) ✗ 

  • 如果出现警告⚠️ - WARN | summary: The summary is not meaningful.
    编辑更改一下 s.summary 即可消除警告
  • pod lib lint --verbose 可查看详细的 log
    podspec 本地验证成功.png

至此,我们的源码已经导入、样例工程已经验证执行、podspec 配置文件本地已经验证,那么我们是不是就可以直接在 pod 里面使用了呢?答案是不行的!目前只是处于本地状态,并没有发布,所以还是不能使用的。

6. pod 库项目发布到私有仓库

在项目工程文件下执行 git 相关指令,并添加 tag ,发布到 git
分别执行以下命令

➜  EncryptionMethods git:(master) ✗ git remote add origin http://172.17.0.18/XHC-iOS-Libs/EncryptionMethods.git
➜  EncryptionMethods git:(master) ✗ git add .
➜  EncryptionMethods git:(master) ✗ git commit -m "v0.1.0"      
➜  EncryptionMethods git:(master) git push origin master 
➜  EncryptionMethods git:(master) git tag 0.1.0
➜  EncryptionMethods git:(master) git push origin 0.1.0 

相关指令执行结束后,此时我们再去看我们的 git 项目:


项目发布.png

现在我们就可以直接在 Podfile 文件中添加如下代码就可以用 pod 来添加我们的库了

pod 'EncryptionMethods', :git => 'http://172.17.0.18/XHC-iOS-Libs/EncryptionMethods.git'

但是机智的同学发下此时执行 pod search EncryptionMethods并不能找到相关的库,提示如下:

[!] Unable to find a pod with name, author, summary, or description matching `EncryptionMethods`

为了能让 pod 搜索到我们的库,我们还需要把我们的 EncryptionMethods.podspec 文件发布到我们的 PodSpecs 库,方便我们管理各种 pod 库

7. 发布 .podspec 文件到私有库
  • 首先我们需要创建自己的 Specs 管理库;
  • 创建之后获取到 git 地址:git@172.17.0.18:XHC-iOS-Libs/Podspecs.git
  • 在终端执行 Specs 创建指令 pod repo add [REPO_NAME] [SOURCE_URL]
    注意: a. 此指令是在本地创建一个 [REPO_NAME] 并且拉取 [SOURCE_URL] 的所有 podSpec,如果 [SOURCE_URL] 为新创建的仓库,则本地 [REPO_NAME] 文件夹下只包含一个 .git 文件夹
    b. 如果需要管理多个 podSpec,pod repo add [REPO_NAME] [SOURCE_URL] 执行一次即可,后续添加其它 pod 库时,执行 pod repo push [REPO_NAME] [SPEC_NAME.podspec] 就行
➜  EncryptionMethods git:(master) pod repo add EncryptionMethodsSpec http://172.17.0.18/XHC-iOS-Libs/Podspecs.git
Cloning spec repo `EncryptionMethodsSpec` from `http://172.17.0.18/XHC-iOS-Libs/Podspecs.git`

  • 执行发布命令 pod repo push [REPO_NAME] [SPEC_NAME.podspec] ,直接发布,此指令会将[SPEC_NAME.podspec]部署到本地(~/.cocoapods/repos/[REPO_NAME])并且发布到远程 PodSpecs 仓库即上面的 git 地址:git@172.17.0.18:XHC-iOS-Libs/Podspecs.git, 终端展示如下:
➜  EncryptionMethods git:(master) ✗ pod repo push EncryptionMethodsSpec EncryptionMethods.podspec

Validating spec
 -> EncryptionMethods (0.1.0)
    - NOTE  | xcodebuild:  note: Using new build system
    - NOTE  | [iOS] xcodebuild:  note: Planning build
    - NOTE  | [iOS] xcodebuild:  note: Constructing build description

Updating the `EncryptionMethodsSpec' repo

Your configuration specifies to merge with the ref 'refs/heads/master'
from the remote, but no such ref was fetched.

Adding the spec to the `EncryptionMethodsSpec' repo

 - [Add] EncryptionMethods (0.1.0)

Pushing the `EncryptionMethodsSpec' repo


[!] 'EncryptionMethods' uses the unencrypted 'http' protocol to transfer the Pod. Please be sure you're in a safe network with only trusted hosts. Otherwise, please reach out to the library author to notify them of this security issue.
➜  EncryptionMethods git:(master) ✗ 
  • 发布成功之后,我们来看一下我们的 Specs 的 git 项目:


    Podspecs.png
8. 查看我们本地的 PodSpecs 库:

你的 repo 结构应该是这样的:

repo 结构.png

直接 Findle -> 前往 -> 前往文件夹 -> 输入:~/.cocoapods/repos -> 点击前往

本地的 Specs 库.png

至此,我们的私有库创建发布结束。但是我们注意到,EncryptionMethodsSpec 没有 README.md ,为了后期的维护更新,以及团队小伙伴的使用方便,建议完备一下信息,新建一个 README.md 文件,添加一些版本记录信息等。

9. 编辑 Podspecs 的 README.md

终端输入 vi ~/.cocoapods/repos/EncryptionMethodsSpec/README.md,编辑 README.md
a. 按 i 进入编辑状态;
b. 开始写入要编辑的信息;
c. 按 esc 退出编辑状态;
d. 按 :wq 保存更改并推出(切记输入法应在英文输入状态)

# EncryptionMethodsSpec

项目 | 地址 | 版本 | 日期 | 作者
:-: | :-: | :-: | :-: | :-:
EncryptionMethods | http://172.17.0.18/XHC-iOS-Libs/EncryptionMethods.git | 0.1.0 | 2019.4.24 | iTruda

README.md 效果展示:

EncryptionMethodsSpec

项目 地址 版本 日期 作者
EncryptionMethods http://172.17.0.18/XHC-iOS-Libs/EncryptionMethods.git 0.1.0 2019.4.24 iTruda

终端执行以下命令,将更改 push 到 Podspecs 私有仓库

➜  EncryptionMethods git:(master) ✗ cd ~/.cocoapods/repos/EncryptionMethodsSpec
➜  EncryptionMethodsSpec git:(master) ✗ git add .
➜  EncryptionMethodsSpec git:(master) ✗ git commit -m "add README.md"

再去 Podspecs 私有仓库看看效果

Podspecs 私有仓库.png

根据自己的需要,自行编辑吧,到此我们整个添加完毕。
最后我们需要在 Podfile 文件中添加 source 'http://172.17.0.18/XHC-iOS-Libs/Podspecs.git' 就可以查到我们的 pod 库了,接下来在终端去执行 pod search EncryptionMethods 看看效果

-> EncryptionMethods (0.1.0)
   XHC EncryptionMethods pod Use.
   pod 'EncryptionMethods', '~> 0.1.0'
   - Homepage: http://172.17.0.18/XHC-iOS-Libs/EncryptionMethods.git
   - Source:   git@172.17.0.18:XHC-iOS-Libs/EncryptionMethods.git
   - Versions: 0.1.0 [EncryptionMethodsSpec repo]
(END)

我们直接在 Podfile 文件中添加 pod 'EncryptionMethods', '~> 0.1.0',然后执行 pod install就可以使用了。

使用Podfile管理Pods依赖库版本
pod 'AFNetworking'               //不显式指定依赖库版本,表示每次都获取最新版本  
pod 'AFNetworking', '2.0'        //只使用2.0版本  
pod 'AFNetworking', '> 2.0'      //使用高于2.0的版本  
pod 'AFNetworking', '>= 2.0'     //使用大于或等于2.0的版本  
pod 'AFNetworking', '< 2.0'      //使用小于2.0的版本  
pod 'AFNetworking', '<= 2.0'     //使用小于或等于2.0的版本  
pod 'AFNetworking', '~> 0.1.2'   //使用大于等于0.1.2但小于0.2的版本  
pod 'AFNetworking', '~>0.1'      //使用大于等于0.1但小于1.0的版本  
pod 'AFNetworking', '~>0'        //高于0的版本,写这个限制和什么都不写是一个效果,都表示使用最新版本 

接下来让我们的愉快的玩耍吧!!!

相关文章

  • 制作Pod库

    目录 一、公有Pod库制作 二、私有Pod库制作 三、subspec子库的制作 四、遇到的坑 一、公有Pod库制作...

  • 如何制作Pod库

    需求: 管理一些常用的类或者第三方的SDK; 组件化开发; 具体步骤: 1. 打开终端,进入要建立私有库项目工程的...

  • 创建自己的pod库并上传

    参考:如何制作自己的Pod库 - 简书 报错 ··· error: include of non-modular ...

  • iOS:CocoaPods制作私有库

    本章制作私有库方法省去了繁琐的Pod校验,快速简单制作私有库。 创建索引库 我使用的是GitHub 制作Pod 再...

  • 制作自己的Cocoapods

    制作自己的Cocoapods 创建自己私有pod库,官方推荐使用 pod lib create [pod name...

  • Cocopods 提示target has transitive

    制作自己的pod库时,依赖其他第三方库,在pod install时提示: 解决方案:在自己的pod库.podspe...

  • Pod库制作

    CocoaPods是iOS开发用于管理第三方库的 不可获取的 工具,在使用别人库的同时,我们有时候也想做一个自己的...

  • iOS 制作本地库Development Pods

    前言:制作一个有相互依赖关系的本地库,之前写的私有库和公开pod库的制作步骤都没有写如果有依赖关系应该如何处理。 ...

  • 制作 CocoaPods 开源库

    CocoaPods 开源库的制作过程: 添加私有Pod仓库,用来存储私有Pod库的podspec文件,类似Coco...

  • 如何制作自己的Pod库

    1、创建仓库: 2、将仓库克隆本地: git clone https://github.com/236021963...

网友评论

    本文标题:如何制作Pod库

    本文链接:https://www.haomeiwen.com/subject/lnweuftx.html