iOS多工程架構(gòu)(二)—— 組件化

上一篇的多工程架構(gòu)是,手動創(chuàng)建多project的方式?,F(xiàn)在這篇是同個pod的方式進行組件化開發(fā)的。

一、創(chuàng)建遠程索引庫

1、我們先在GitHub上創(chuàng)建一個organization
創(chuàng)建 organization
2、添加一個遠程索引庫,填寫相關(guān)信息
遠程索引庫
3、創(chuàng)建本地索引庫,并與遠程索引庫做關(guān)聯(lián)

a、打開終端,pod repo add 本地索引庫的名字 遠程索引庫的地址
例如:pod repo add JerryNetworkManager https://github.com/JerryYJL/JerryNetworkManager.git

b、pod repo查看是否創(chuàng)建成功

二、創(chuàng)建組件

1、開始創(chuàng)建組件

a、cd 到指定目錄,然后pod lib create 組件名
例如 pod lib create JerryNetworkManager
b、而后填上項目相關(guān)信息,便能成功創(chuàng)建組件

2、目錄相關(guān)
目錄
a、podspec文件

該文件是組件的核心配置中心,看一下podspec語法

Pod::Spec.new do |s|
#  組件名
  s.name             = 'JLNetworkingManager'
#  版本號,與tag標簽對應(yīng)
  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'
#  開源協(xié)議
  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"
#  內(nèi)核設(shè)置
  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'
#  所需的系統(tǒng)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相關(guān)

這個一般都是寫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、組件的核心內(nèi)容

這里就可以開始你的代碼秀了

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組件就完成了,下一篇開始主項目跟組件的交流

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容