前言
業(yè)界已經有許多技術文章進行了組件化的指導了,這里就不再贅述。
本文主要講的是小型團隊和小型項目對于「本地組件化」的實踐。
為什么要提出組件化本地化的概念呢?對于小型項目和小型團隊來說,可能自身的項目不需要用到組件化,但是又想學習和利用組件化的技術,組件化的本地化恰好可以滿足這個需求。
那么何為本地化的組件化呢?對于常規(guī)的組件化來說,每一個模塊或者說組件都需要創(chuàng)建為一個遠端的組件庫,別的模塊從遠端加載這個組件來使用,這樣就需要管理非常多的庫。而本地化的組件化只需要創(chuàng)建一個遠端的代碼庫,存放所有的代碼,進行版本管理,跟平常的代碼管理一樣,所有的組件也是放在這個庫中,無需創(chuàng)建多個組件庫,所以叫做本地化的組件化。
技術思路
本地化的組件化的技術思路是這樣的,項目使用 workspace 來管理所有的 Project,其中的一個 Project 為主項目,其他的為不同的組件,新增組件就在這個 workspace 中新建一個 Project。
運行主項目就選用主項目 Project,運行單一組件就選擇這個組件 Project 進行運行。
而跨模塊的通信使用的是基于反射的遠程接口調用封裝,基于 CTMediator 開源庫。
Demo實踐
比如現在要開發(fā)一款記錄類型的APP,可以記錄身體信息,比如身高體重等,基本的業(yè)務邏輯是,先進行登錄,然后進入主功能界面,提供首頁記錄功能和我的頁面兩個 Tab。
以下是具體的實踐過程:
創(chuàng)建 workspace
如圖方式創(chuàng)建一個名為 Records.xcworkspace 的 workspace

創(chuàng)建 Project
- 首頁
- 我的
- 登錄
- 身體信息組件
- 公共組件
按照功能,劃分為這幾個組件,分別創(chuàng)建4個 project,其中首頁和我的放在主 project中,身體信息、登錄和公共組件為單一組件,公共組件放一下功能分類,公共類,宏定義,后臺接口文檔之類的。
command + shift + N 創(chuàng)建 Project
注意要放在 Records 這個 workspace 中

創(chuàng)建完成如圖

構造組件
舉個例子,先完成登錄組件,把項目切換到 登錄

完成登錄的基本業(yè)務邏輯,運行沒有錯誤之后,進行組件化的工作
podspec 文件
在終端創(chuàng)建 podspec 文件
pod spec create 文件名
使用這種方法創(chuàng)建的 podspec 文件會帶有大量的注釋
touch xx.podspec
使用這種方法會創(chuàng)建出一個空白的 podspec 文件
我們創(chuàng)建跟組件同名的 Login.podspec 文件, 詳細語法參考 Podspec語法參考
Pod::Spec.new do |s|
s.name = 'Login'
s.version = '0.0.1'
s.summary = '登錄組件'
s.description = <<-DESC
TODO: Add long description of the pod here.
DESC
s.homepage = 'https://gitee.com/xxx'
# s.license = { :type => 'MIT', :file => 'LICENSE' }
s.author = { 'xxx' => 'xxxxxxxxx@qq.com' }
s.source = { :git => 'https://gitee.com/xxx/Login.git', :tag => s.version.to_s }
s.ios.deployment_target = '9.0'
s.source_files = 'Login/Login/*.{h,m}'
s.subspec 'Controller' do |ss|
ss.source_files = 'Login/Login/Controller/*.{h,m}'
end
s.subspec 'Router' do |ss|
ss.source_files = 'Login/Login/Router/*.{h,m}'
end
s.subspec 'View' do |ss|
ss.source_files = 'Login/Login/View/*.{h,m}'
end
s.dependency 'CTMediator'
s.prefix_header_contents = <<-EOS
#ifdef __OBJC__
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#endif
EOS
end
CTMediator
這是其他組件調用本組件的入口,通過創(chuàng)建 CTMediator 的分類,獲取該組件入口控制器,比如當前登錄組件就是通過 CTMediator+Login 分類中的方法獲取登錄控制器。

至此,登錄組件已經完成,按照上訴步驟,完成其他組件的構建
組件的調用
構造 Podfile 文件
在 workspace 的根目錄下創(chuàng)建 Podfile 文件,并完成構建

source 'https://github.com/CocoaPods/Specs.git'
# Uncomment the next line to define a global platform for your project
platform :ios, '9.0'
workspace 'Records.xcworkspace'
target 'Record' do
project 'Record/Record.xcodeproj'
pod 'CTMediator'
#調用登錄組件
pod 'Login', :path => '../Record/Login/'
inhibit_all_warnings!
end
#登錄組件
target 'Login' do
project 'Login/Login.xcodeproj'
inhibit_all_warnings!
end
在終端使用以下命令完成本地組件的加載
pod update --no-repo-update
加載完成后,可以在 Pods 中的 Development Pods 看到加載的組件

切換到主工程 Record,進行登錄組件的調用

在需要用到組件的地方,導入組件中的CTMediator分類文件(CTMediator+Login.h),調用分類方法,獲取控制器,進行控制器的使用跳轉,demo 是在 AppDelegate 中調用
#import <Login/CTMediator+Login.h>
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
UIViewController *loginVc = [[CTMediator sharedInstance] loginVc];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:loginVc];
self.window.rootViewController = nav;
[self.window makeKeyAndVisible];
return YES;
}
至此,本地化組件化的基本實踐流程已完成,具體請參考Demo。