iOS 开发继续加油iOS面试

如何制作Pod库

2019-04-24  本文已影响342人  iTruda

需求:

  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 }
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 本地验证指令:

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) ✗ 

至此,我们的源码已经导入、样例工程已经验证执行、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 文件到私有库
➜  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`

➜  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) ✗ 
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的版本,写这个限制和什么都不写是一个效果,都表示使用最新版本 

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

上一篇 下一篇

猜你喜欢

热点阅读