iOS 记录pod组件化学习历程

为啥要pod组件化?

让我们的项目模块化,单个功能独立,能够拆分组合
项目结构更加清晰
多人合作责任更加明确,提高解决问题的效率
通用功能合成组件,让我们以后做其他项目时,可以快速集成

什么时候组件化?
  • 从已有的项目中抽离出单独的模块,然后组件化
  • 如果我们准备实现某个单独的功能,我们可以使用命令pod lib create 我的组件名称创建一个pod项目,然后在这个项目里实现这个功能,这样的好处就是组件化之后,这个项目就是我们组件化的demo,给到别人,别人从demo中能够理解和使用我们的组件
示列:本地化组件

这里我拿刚写好的一个demo项目来做示列


image.png

主要功能就是实现蓝牙与硬件的通信,功能已经完成
先抽离功能文件,以下就是这个功能的所有文件,这些文件都要放到组件的资源文件中


image.png

本地化组件比较简单,最主要的就是.podspec文件,我们最后创建的目录结构
image.png

先看看.podspec文件中的内容


image.png
#
# Be sure to run `pod lib lint TestPod.podspec' to ensure this is a
# valid spec before submitting.
#
# Any lines starting with a # are optional, but their use is encouraged
# To learn more about a Podspec see https://guides.cocoapods.org/syntax/podspec.html
#

Pod::Spec.new do |s|
  s.name             = 'TestPod'
  s.version          = '0.1.0'
  s.summary          = 'A short description of TestPod.'

# This description is used to generate tags and improve search results.
#   * Think: What does it do? Why did you write it? What is the focus?
#   * Try to keep it short, snappy and to the point.
#   * Write the description between the DESC delimiters below.
#   * Finally, don't worry about the indent, CocoaPods strips it!

  s.description      = <<-DESC
TODO: Add long description of the pod here.
                       DESC

  s.homepage         = 'https://github.com/[email protected]/TestPod'
  # s.screenshots     = 'www.example.com/screenshots_1', 'www.example.com/screenshots_2'
  s.license          = { :type => 'MIT', :file => 'LICENSE' }
  s.author           = { '[email protected]' => '[email protected]' }
  s.source           = { :git => 'https://github.com/[email protected]/TestPod.git', :tag => s.version.to_s }
  # s.social_media_url = 'https://twitter.com/'

  s.ios.deployment_target = '8.0'

  s.source_files = 'TestPod/Classes/**/*'
  
  # s.resource_bundles = {
  #   'TestPod' => ['TestPod/Assets/*.png']
  # }

  # s.public_header_files = 'Pod/Classes/**/*.h'
  # s.frameworks = 'UIKit', 'MapKit'
  # s.dependency 'AFNetworking', '~> 2.3'
end

注意以上需要修改的地方,关于LICENSE文件内容

Copyright (c) 2019 [email protected] <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

然后我们创建一个demo,来使用组件

image.png

在demo项目下创建podfile文件
image.png

终端pod install,在项目中使用组件
image.png

对外暴露的class需要使用public进行修饰,如果外部可以继承使用open修饰
到此本地组件化结束,很简单四不四

如果我们想使用之前的命令pod lib create 我的组件名来创建一个组件demo项目也可以,按照提示,我们将生成一个demo项目

image.png

然后将之前抽离的文件,放到这个Class文件中就可以了,也是很简单哒

让我们的组件关联远程仓库

1:创建一个远程仓库makePod,码云,GitHub上都可以,复制远程仓库连接

image.png

2:初始化本地仓库,将本地代码上传到远程仓库

git init
git add .
git commit -m '说明'
git tag '0.1.0' -m '这个标签要打上去哦,因为. podspec文件里有指定tag'
git push -u origin master
// tag也要提交上去哦
git push --tag  

3:添加组件到本地仓库,~/.cocoapods中可以看到这个组件
pod repo add makeTestPod https://github.com/xxxxxx/makePod.git

image.png

4:验证组件

// 验证本地仓库,忽略警告
pod lib lint --allow-warnings --verbose
// 验证远程仓库
pod spec lint --allow-warnings --verbose

5:上传到远程私有索引库
pod repo push makeTestPod TestPod.podspec

image.png

6:查找我们的组件
pod search makeTestPod
报错
[!] Unable to find a pod with name, author, summary, or description matching LMBlueChargeTest
这个错误是没有找到我们的库,不过没关系,我们直接在项目中用试试,修改podfile文件

image.png

pod install
image.png

image.png

你可能感兴趣的:(iOS 记录pod组件化学习历程)