美文网首页iOS
iOS多工程架构(二)—— 组件化

iOS多工程架构(二)—— 组件化

作者: 虞小虞 | 来源:发表于2022-01-21 15:45 被阅读0次

    上一篇的多工程架构是,手动创建多project的方式。现在这篇是同个pod的方式进行组件化开发的。

    一、创建远程索引库

    1、我们先在GitHub上创建一个organization 创建 organization 2、添加一个远程索引库,填写相关信息 远程索引库 3、创建本地索引库,并与远程索引库做关联

    a、打开终端,pod repo add 本地索引库的名字 远程索引库的地址
    例如:pod repo add JerryNetworkManager https://github.com/JerryYJL/JerryNetworkManager.git

    b、pod repo查看是否创建成功

    二、创建组件

    1、开始创建组件

    a、cd 到指定目录,然后pod lib create 组件名
    例如 pod lib create JerryNetworkManager
    b、而后填上项目相关信息,便能成功创建组件

    2、目录相关
    目录
    a、podspec文件

    该文件是组件的核心配置中心,看一下podspec语法

    Pod::Spec.new do |s|
    #  组件名
      s.name             = 'JLNetworkingManager'
    #  版本号,与tag标签对应
      s.version          = '0.1.5'
    #  组件的描述
      s.summary          = 'A short description of JLNetworking.'
    
    # 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/JLNetWorking/JLNetworking'
      # s.screenshots     = 'www.example.com/screenshots_1', 'www.example.com/screenshots_2'
    #  开源协议
      s.license          = { :type => 'MIT', :file => 'LICENSE' }
    #  作者信息
      s.author           = { 'Jerry' => '110*****@qq.com' }
    #  git地址,版本号
      s.source           = { :git => 'https://github.com/JLNetWorking/JLNetworking.git', :tag => s.version.to_s }
      # s.social_media_url = 'https://twitter.com/<TWITTER_USERNAME>'
    
    #  支持的iOS最低版本
      s.ios.deployment_target = '11.0'
    #  指定Swift编译版本
      s.swift_version = "5.0"
    #  内核设置
      s.pod_target_xcconfig = { 'VALID_ARCHS' => 'x86_64 armv7 arm64' }
    #  必备项,代码源文件地址,如果有多个目录下则用逗号分开,否则"public_header_files"等不可用
      s.source_files = 'JLNetworking/Classes/**/*'
      
    #  公开头文件地址
      # s.public_header_files = 'Pod/Classes/**/*.h'
    #  所需的系统framework,多个用逗号隔开,不需要后缀名
      # s.frameworks = 'UIKit', 'MapKit'
    #  资源路径
      s.resource_bundles = {
        'JLNetworkingManager' => ['JLNetworkingManager/Assets/**/*']
      }
    #  依赖第三方
      s.dependency 'Moya/RxSwift'
      s.dependency 'RxCocoa'
      s.dependency 'HandyJSON'
      s.dependency 'SwiftyJSON'
      s.dependency 'SnapKit'
      
    end
    
    b、example文件,主要写demo相关

    这个一般都是写demo,给别人看这个组件是怎么用的,还有跑起来是啥效果之类的

    c、Podfile文件

    这里可以导入你的demo需要的第三方,且不会引入到你的组件里面

    use_frameworks!
    
    platform :ios, '11.0'
    
    target 'JLNetworking_Example' do
      pod 'JLNetworkingManager', :path => '../'
    
      target 'JLNetworking_Tests' do
        inherit! :search_paths
      end
    end
    
    d、组件的核心内容

    这里就可以开始你的代码秀了

    3、上传

    git add .

    git commit -m 'xxx'

    git remote add origin https://github.com/JLNetWorking/JLNetworking.git

    git push origin master

    git tag 版本号(需与podspec中的版本号一致)

    git push --tags

    4、podspec验证

    pod spec lint --verbose --allow-warnings --sources='https://github.com/JLNetWorking/JLNetworking.git'

    解释

    --verbose:打印错误

    --allow-warnings:允许警告,默认有警告的podspec会验证失败

    --sources:如果依赖了其他不包含在官方specs里的pod,则用它来指明源,比如依赖了某个私有库。多个值以逗号分隔

    5、推送

    推送分为2种情况,一个是私有库的推送,例如公司自己的gitLabel;第二个是公有库,例如前面的GitHub

    a、私有库的推送

    私有库的推送比较直接

    pod repo push JLNetworking JLNetworking.podspec --verbose --allow-warnings --sources=https://github.com/JLNetWorking/JLNetworking.git

    b、公有库的推送

    公有库的推送就比较麻烦,因为需要推送到cocoapods,所以第一次推送需要注册账号

    注册账号
    pod trunk register 邮箱 '名字' --description='macbook air' --verbose
    注册完会收到一份邮件,需要点击验证,验证完之后可以查看个人信息
    pod trunk me
    如果信息正确,就可以推送了
    pod repo push JLNetworking JLNetworking.podspec --verbose --allow-warnings --sources=https://github.com/JLNetWorking/JLNetworking.git

    5、验证

    pod search JLNetworkingManager
    如果没有搜到,可能就是本地仓库没有更新

    更新repo库,然后再搜
    pod repo update

    pod组件就完成了,下一篇开始主项目跟组件的交流

    相关文章

      网友评论

        本文标题:iOS多工程架构(二)—— 组件化

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