Skip to content

Commit 565813b

Browse files
更新组件化集成方案
1 parent 53fe5f8 commit 565813b

File tree

7 files changed

+161
-8
lines changed

7 files changed

+161
-8
lines changed

MainProject/MainProject.xcodeproj/project.pbxproj

+13
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
791CFF481DFBCB1E00121A62 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 791CFF461DFBCB1E00121A62 /* Main.storyboard */; };
1414
791CFF4A1DFBCB1E00121A62 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 791CFF491DFBCB1E00121A62 /* Assets.xcassets */; };
1515
791CFF4D1DFBCB1E00121A62 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 791CFF4B1DFBCB1E00121A62 /* LaunchScreen.storyboard */; };
16+
799567B6217F0F7F00B93760 /* HomeViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 799567B5217F0F7F00B93760 /* HomeViewController.m */; };
17+
799567B9217F109200B93760 /* LoginViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 799567B8217F109200B93760 /* LoginViewController.m */; };
1618
B9B84A5302B9AC08C5569ECE /* libPods-MainProject.a in Frameworks */ = {isa = PBXBuildFile; fileRef = C9519F3DA8B7559B24F09783 /* libPods-MainProject.a */; };
1719
/* End PBXBuildFile section */
1820

@@ -28,6 +30,10 @@
2830
791CFF491DFBCB1E00121A62 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
2931
791CFF4C1DFBCB1E00121A62 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = "<group>"; };
3032
791CFF4E1DFBCB1E00121A62 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
33+
799567B4217F0F7F00B93760 /* HomeViewController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = HomeViewController.h; sourceTree = "<group>"; };
34+
799567B5217F0F7F00B93760 /* HomeViewController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = HomeViewController.m; sourceTree = "<group>"; };
35+
799567B7217F109200B93760 /* LoginViewController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = LoginViewController.h; sourceTree = "<group>"; };
36+
799567B8217F109200B93760 /* LoginViewController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = LoginViewController.m; sourceTree = "<group>"; };
3137
949C4EEBD172323874701483 /* Pods-MainProject.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-MainProject.release.xcconfig"; path = "Pods/Target Support Files/Pods-MainProject/Pods-MainProject.release.xcconfig"; sourceTree = "<group>"; };
3238
C9519F3DA8B7559B24F09783 /* libPods-MainProject.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = "libPods-MainProject.a"; sourceTree = BUILT_PRODUCTS_DIR; };
3339
/* End PBXFileReference section */
@@ -78,6 +84,10 @@
7884
791CFF411DFBCB1E00121A62 /* AppDelegate.m */,
7985
791CFF431DFBCB1E00121A62 /* ViewController.h */,
8086
791CFF441DFBCB1E00121A62 /* ViewController.m */,
87+
799567B4217F0F7F00B93760 /* HomeViewController.h */,
88+
799567B5217F0F7F00B93760 /* HomeViewController.m */,
89+
799567B7217F109200B93760 /* LoginViewController.h */,
90+
799567B8217F109200B93760 /* LoginViewController.m */,
8191
791CFF461DFBCB1E00121A62 /* Main.storyboard */,
8292
791CFF491DFBCB1E00121A62 /* Assets.xcassets */,
8393
791CFF4B1DFBCB1E00121A62 /* LaunchScreen.storyboard */,
@@ -229,6 +239,8 @@
229239
791CFF451DFBCB1E00121A62 /* ViewController.m in Sources */,
230240
791CFF421DFBCB1E00121A62 /* AppDelegate.m in Sources */,
231241
791CFF3F1DFBCB1E00121A62 /* main.m in Sources */,
242+
799567B6217F0F7F00B93760 /* HomeViewController.m in Sources */,
243+
799567B9217F109200B93760 /* LoginViewController.m in Sources */,
232244
);
233245
runOnlyForDeploymentPostprocessing = 0;
234246
};
@@ -387,6 +399,7 @@
387399
791CFF531DFBCB1E00121A62 /* Release */,
388400
);
389401
defaultConfigurationIsVisible = 0;
402+
defaultConfigurationName = Release;
390403
};
391404
/* End XCConfigurationList section */
392405
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//
2+
// HomeViewController.h
3+
// MainProject
4+
//
5+
// Created by 刘小壮 on 2018/10/23.
6+
// Copyright © 2018年 刘小壮. All rights reserved.
7+
//
8+
9+
#import <UIKit/UIKit.h>
10+
11+
@interface HomeViewController : UIViewController
12+
13+
@end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
//
2+
// HomeViewController.m
3+
// MainProject
4+
//
5+
// Created by 刘小壮 on 2018/10/23.
6+
// Copyright © 2018年 刘小壮. All rights reserved.
7+
//
8+
9+
#import "HomeViewController.h"
10+
11+
@interface HomeViewController ()
12+
13+
@end
14+
15+
@implementation HomeViewController
16+
17+
- (void)viewDidLoad {
18+
[super viewDidLoad];
19+
// Do any additional setup after loading the view.
20+
}
21+
22+
- (void)didReceiveMemoryWarning {
23+
[super didReceiveMemoryWarning];
24+
// Dispose of any resources that can be recreated.
25+
}
26+
27+
/*
28+
#pragma mark - Navigation
29+
30+
// In a storyboard-based application, you will often want to do a little preparation before navigation
31+
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
32+
// Get the new view controller using [segue destinationViewController].
33+
// Pass the selected object to the new view controller.
34+
}
35+
*/
36+
37+
@end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//
2+
// LoginViewController.h
3+
// MainProject
4+
//
5+
// Created by 刘小壮 on 2018/10/23.
6+
// Copyright © 2018年 刘小壮. All rights reserved.
7+
//
8+
9+
#import <UIKit/UIKit.h>
10+
11+
@interface LoginViewController : UIViewController
12+
@property (nonatomic, weak) id delegate;
13+
@end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
//
2+
// LoginViewController.m
3+
// MainProject
4+
//
5+
// Created by 刘小壮 on 2018/10/23.
6+
// Copyright © 2018年 刘小壮. All rights reserved.
7+
//
8+
9+
#import "LoginViewController.h"
10+
11+
@interface LoginViewController ()
12+
13+
@end
14+
15+
@implementation LoginViewController
16+
17+
- (void)viewDidLoad {
18+
[super viewDidLoad];
19+
// Do any additional setup after loading the view.
20+
}
21+
22+
- (void)didReceiveMemoryWarning {
23+
[super didReceiveMemoryWarning];
24+
// Dispose of any resources that can be recreated.
25+
}
26+
27+
/*
28+
#pragma mark - Navigation
29+
30+
// In a storyboard-based application, you will often want to do a little preparation before navigation
31+
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
32+
// Get the new view controller using [segue destinationViewController].
33+
// Pass the selected object to the new view controller.
34+
}
35+
*/
36+
37+
@end

MainProject/MainProject/ViewController.m

+18
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,27 @@
88

99
#import "ViewController.h"
1010
#import <MGJRouter/MGJRouter.h>
11+
#import "HomeViewController.h"
12+
#import "LoginViewController.h"
13+
14+
const NSString *CTBUserCenterLoginDelegateKey = @"CTBUserCenterLoginDelegateKey";
1115

1216
@implementation ViewController
1317

18+
- (void)viewDidLoad {
19+
[super viewDidLoad];
20+
21+
HomeViewController *homeVC = [[HomeViewController alloc] init];
22+
NSDictionary *params = @{CTBUserCenterLoginDelegateKey : homeVC};
23+
[MGJRouter openURL:@"CTB://UserCenter/UserLogin" withUserInfo:params completion:nil];
24+
25+
[MGJRouter registerURLPattern:@"CTB://UserCenter/UserLogin" toHandler:^(NSDictionary *routerParameters) {
26+
UIViewController *homeVC = routerParameters[CTBUserCenterLoginDelegateKey];
27+
LoginViewController *loginVC = [[LoginViewController alloc] init];
28+
loginVC.delegate = homeVC;
29+
}];
30+
}
31+
1432
- (IBAction)skipToUserCenterModuleAction:(UIButton *)sender {
1533

1634
[MGJRouter openURL:@"CTB://UserCenter/PushMainVC"

README.md

+30-8
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,12 @@ https://git.coding.net/LiuXiaoZhuang/HomePageModule.git
2424

2525
很多人都在这块犯错了,所以这里要着重讲一下。就像`CocoaPods`的开源仓库一样,平时用来开发的代码和发布给别人使用的代码,是不在同一个仓库的。
2626

27-
例如`AFNetworking`,代码仓库是在[AFNetworking代码仓库](https://github.com/AFNetworking/AFNetworking.git)这里,而`AFNetworking`提供给其他人使用的时候,需要先提交到`CocoaPods``Spec`仓库,然后我们从`Pod Install`都是从`Spec`仓库拉去下来的代码。这么说大家理解了吧,不理解就去我博客里留言吧。
27+
例如`AFNetworking`,代码仓库是在[AFNetworking代码仓库](https://github.com/AFNetworking/AFNetworking.git)这里,而`AFNetworking`提供给其他人使用的时候,需要先提交到`CocoaPods``Spec`仓库,然后我们执行`pod install`都是从`Spec`仓库拉去下来的代码。这么说大家理解了吧,不理解就去我博客里留言吧。
2828

2929
例如我们创建的`Spec`仓库是下面的地址,最后提供给其他人的代码都要被提交到这里。创建`Spec`代码仓库和平时创建一样,只是这里暂时先为空,也不需要进行`Clone`操作。
3030

3131
https://git.coding.net/LiuXiaoZhuang/HomePageModuleSpec.git
3232

33-
34-
3533
### 搭建环境
3634

3735
##### 创建.podspec文件
@@ -44,7 +42,9 @@ pod spec create HomePageModule
4442

4543
创建这个文件后,需要修改里面的一些东西,例如这里我用`HomePageModule`私有仓库做例子。这里面修改的东西,表示你的`Spec`私有仓库的一些特征,例如`name``source``version`之类的,在进行`pod search`操作时候的显示结果,也是由这个文件决定的。
4644

47-
需要注意的是,下面的`source`设置为`HomePageModule`仓库而不是`Spec`仓库,这一步很多人都在这里犯错。
45+
下面`source_files`指定的是文件路径,也就是最终导入到项目中的文件。下面的`exclude_files`指定的是不需要的文件路径,这些文件即便在`source_files`中,也不会被包含在`Pods`仓库中。
46+
47+
需要注意的是,下面的`source`设置为`HomePageModule`仓库而不是`Spec`仓库,这一步很多人都在这里犯错。另外,`podspec`文件中可能会有一些`Example`的字段,这些字段应该都进行修改,否则执行`pod`命令会报错。
4848

4949
Pod::Spec.new do |s|
5050
s.name = "HomePageModule"
@@ -63,8 +63,6 @@ pod spec create HomePageModule
6363
s.platform = :ios
6464
end
6565

66-
67-
6866
### 开发过程
6967

7068
##### 验证podspec文件
@@ -107,8 +105,6 @@ pod repo push HomePageModule HomePageModule.podspec
107105

108106
提交给`Spec`私有仓库的文件,可以通过`.podspec`文件的`s.source_files`字段进行过滤,符合过滤条件的文件就都会被提交到`Spec`仓库。例如上面我们将所有`.h``.m`文件都提交到`Spec`仓库,除了这些还可以设置`imageAssets``XIB``Bundle`等文件。
109107

110-
111-
112108
### 使用私有仓库代码
113109

114110
`Spec`私有仓库中有可用的代码后,就可以通过`CocoaPods`命令来使用组件代码了。在`Podfile`文件中需要声明私有仓库地址,例如下面代码。
@@ -127,6 +123,32 @@ pod repo push HomePageModule HomePageModule.podspec
127123

128124
版本控制也非常简单,只需要在`Podfile`中指定某个私有仓库的版本号即可。
129125

126+
### Local Pods
127+
128+
私有仓库调试比较麻烦,不可能是每修改一些代码,就提交到远端仓库,再拉下来验证代码是否有效。所以,为了提升开发效率,可以通过`LocalPods`的方式进行调试。或者不想用远端仓库,也可以直接用`LocalPods`的方案进行本地组件化,但是并不推荐这种方案,因为不能进行物理隔离。
129+
130+
本地`LocalPods`仓库不需要单独创建,直接用之前的组件仓库。在`pods`中使用本地仓库时,需要指定对应路径。指定路径后,执行`pod install`即可集成本地仓库。为了方便指定本地路径,建议将组件仓库尽量放在一个文件夹下,例如叫做`LocalPods`
131+
132+
```objective-c
133+
pod 'Login', :path => './LocalPods/Login/'
134+
```
135+
136+
通过上面的命令集成本地仓库后,`Pods`会生成一个和远程文件夹同级的文件,叫做`Development Pods`,这个文件夹就是本地仓库所在的位置。
137+
138+
此时之前`Login`仓库的产物就被替换为本地`LocalPods``Login`代码的产物了,后面的修改都会影响这个产物。直到调试完成,将代码提交到组件仓库,将`path`路径删除重新执行`pod install`,产物就会被替换为组件仓库的产物。
139+
140+
141+
142+
143+
144+
145+
146+
147+
148+
149+
150+
151+
130152

131153

132154

0 commit comments

Comments
 (0)