需求前提:希望App由多個業務組件組裝起來,每個業務模塊都可單獨成為子App。
解耦,快速開發!
流程
- 創建遠程私有庫
- 創建業務組件項目xxx,并同步到私有庫
- 創建組件與外界聯系媒介xxx_Category項目并同步遠端倉庫
- 編寫代碼
- 主項目Podfile本地引用組件項目,并使項目編譯通過.
- 將本地引用改為遠程引用,運行項目并編譯成功,組件化完成.
搭建項目
1.創建遠程私有庫
- github上開了一個orgnization
- 在orgnization中創建私有pod源倉庫,命名為XXXPod(用來存放后面打包的私有庫)
- 在orgnization中創建業務組件項目xxx遠程倉庫
- 在orgnization中創建業務組件項目xxx與外界聯系媒介xxx_Category遠程倉庫
如果有多個便創建多個倉庫,目錄如下:
·
2. 創建業務組件項目
cd 項目存放目錄
//創建組件項目
pod lib create xxx
-
根據提示輸入
image.png -
得到如下工程:
Class:存放組件對外接口
Assets:存放資源
image.png
- 同步代碼到github上(這里就不做多說了)
3.創建組件與外界聯系媒介xxx_Category項目及遠端倉庫
cd 項目存放目錄
//創建組件項目
pod lib create xxx_Category
-
根據提示輸入
image.png -
得到如下工程
image.png 同步代碼到github上
4.編寫組件工程代碼
以跳轉到ExampleViewController為例
1.創建ExampleViewController并編寫需要的業務代碼
2.創建Target_Business1(后面會說明為何這么命名)用以跳轉到ExampleViewController
@interface Target_Business1 : NSObject
- (UIViewController *)Action_viewController:(NSDictionary *)params;
@end
#import "Target_Business1.h"
#import "ExampleViewController.h"
@implementation Target_Business1
- (UIViewController *)Action_viewController:(NSDictionary *)params
{
ExampleViewController *viewController = [[ExampleViewController alloc] init];
viewController.title = params[@"title"];//以設置title為例
return viewController;
}
@end
將業務代碼文件及Target_Business1文件拖入class文件夾重新pod update, 運行成功即可!在這里可以在Example demo中寫個接口跳轉到業務代碼頁面進行調試!
5.編寫xxx_Category中的對外代碼
注意xxx_Category需要依賴CTMediator,在 XXX_Category.podspec文件中添加依賴:
s.dependency 'CTMediator'
創建對外聯系接口文件CTMediator+XXX
#import <CTMediator/CTMediator.h>
NS_ASSUME_NONNULL_BEGIN
@interface CTMediator (BUSINESS1_CATEGORY)
- (UIViewController *)toBusiness1WithParam:(NSDictionary *)param;
@end
NS_ASSUME_NONNULL_END
@implementation CTMediator (BUSINESS1_CATEGORY)
- (UIViewController *)toBusiness1WithParam:(NSDictionary *)param{
return [self performTarget:@"Business1" action:@"viewController" params:param shouldCacheTarget:NO];
}
@end
這里用到的performTarget方法我們可以點擊進去查看源碼,可以看到固定寫法Target_XXX,所以在之前我們創建了命名為Target_XXX的文件!
運行成功后將CTMediator+XXX拖入Classes文件夾,重新pod update,運行!
6. 將兩個組件項目復制到主項目工程目錄中,在Podfile中引用組件
pod 'Business1',:path => 'Business1'
pod 'Business1_Category',:path => 'Business1_Category'
pod update 運行成功即可image.png
7. 主項目工程中引用
#import <CTMediator+BUSINESS1_CATEGORY.h>
UIViewController *viewController = [[CTMediator sharedInstance] toBusiness1WithParam:@{@"title":@"業務1"}];
[self.navigationController pushViewController:viewController animated:YES];
8. 將本地引用改為遠程引用
-
進入私有庫工程找到XXX.podspec,并根據具體需要修改,如:image.png
- 將組件代碼都push到遠程庫
- 上傳到我們的私有pod源倉庫(也就是第一步我們創建的私有pod源倉庫)
cd 到私有庫文件
git tag 0.1.0(注意,這里的tag必須和.podSpec文件的版本一致)
git push --tags
pod repo add 源倉庫名稱 源倉庫遠程地址(第一步創建的私有pod源倉庫地址)
如:pod repo add ConfigPrivatePod https://github.com/Baffin-TM/ConfigPrivatePod.git
這樣我們可以在.cocoapods中看到:image.png
再執行:
pod repo push ConfigPrivatePod Business1.podspec --allow-warnings
可以看到github源倉庫ConfigPrivatePod以及本地.cocoapods中的ConfigPrivatePod將會將會多出一個文件:
image.png
image.png
- 同上,將Business1_Category上傳到遠程私有庫
git tag 0.1.0
git push --tags
#無需在pod repo add 源倉庫名稱 源倉庫遠程地址
pod repo push ConfigPrivatePod Business1_Category.podspec --allow-warnings
9. Podfile引用遠程組件
#一定要加遠程索引庫地址
source 'https://github.com/Baffin-TM/ConfigPrivatePod.git'
source 'https://github.com/CocoaPods/Specs.git'
pod 'Business1'
pod 'Business1_Category'
pod update 運行成功即可
10. 更新庫
1.組件中podspec文件把版本提高一個
2.提交代碼到遠端庫,打上tag,tag和podspec文件里面版本一樣
3.pod repo push 源倉庫名稱 XXX.podspec --allow-warnings