-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFFSchemaManager.m
More file actions
292 lines (238 loc) · 9.68 KB
/
Copy pathFFSchemaManager.m
File metadata and controls
292 lines (238 loc) · 9.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
//
// FFSchemaManager.m
//
// Created by wujiangwei on 14/10/28.
// Copyright (c) 2014年 Kevin.Wu. All rights reserved.
//
#import "FFSchemaManager.h"
#import "NSURL+FFURLShemaParse.h"
#import <objc/runtime.h>
NSString * const kFFSchemaName = @"name";
NSString * const kFFSchemaKeyIsNeedLogin = @"needlogin";
NSString * const kFFSchemaKeyTabIndex = @"tabitemindex";
NSString * const kFFSchemaKeyIsPresent = @"ispresent";
@interface FFSchemaManager ()
@property (strong, nonatomic) NSString *appName;
@property (strong, nonatomic) NSDictionary *supportedSchema;
@property (copy, nonatomic) schemaLoginBlock isLoginBlock;
//custom
@property (strong, nonatomic) NSMutableArray *customTabbarControllerClassNameArray;
@end
@implementation FFSchemaManager
{
@private
NSString *_appName;
NSString *_storyboardName;
BOOL _vcPushAnimation;
}
+ (FFSchemaManager *)sharedInstance
{
static FFSchemaManager *_sharedInstance = nil;
static dispatch_once_t oncetoken;
dispatch_once(&oncetoken, ^{
_sharedInstance = [[FFSchemaManager alloc] init];
});
return _sharedInstance;
}
- (NSString *)appName
{
if (_appName == nil) {
_appName = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleName"];
_appName = [_appName lowercaseString];
}
return _appName;
}
- (instancetype)init {
self = [super init];
if (self) {
NSString *file = [[NSBundle mainBundle] pathForResource:@"FFSchema" ofType:@"plist"];
NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:file];
_supportedSchema = dict;
_vcPushAnimation = YES;
}
return self;
}
#pragma mark - config schema
- (void)configStoryboardSchema:(NSString *)storyboardName
{
_storyboardName = [storyboardName copy];
}
- (void)configSchema:(NSString *)configSchemaName isLoginBlock:(schemaLoginBlock)block
{
self.appName = configSchemaName;
self.isLoginBlock = block;
}
- (void)configSchemaVCPushAnimation:(BOOL)vcPushAnimation
{
_vcPushAnimation = vcPushAnimation;
}
#pragma mark - schema tools
- (BOOL)isAppSchema:(NSURL *)url
{
return [[[url scheme] lowercaseString] isEqualToString:self.appName];
}
- (BOOL)canOpenUrl:(NSURL *)url
{
if (url != nil) {
if ([self isAppSchema:url]) {
NSString *schema = [url host];
return [[_supportedSchema allKeys] containsObject:schema];
}else{
return [[UIApplication sharedApplication] canOpenURL:url];
}
}
return NO;
}
#pragma mark - open schema
- (BOOL)openURL:(NSURL *)url
{
if ([self canOpenUrl:url]) {
if ([self isAppSchema:url]) {
NSString *schema = [url host];
NSDictionary *vcParam = [url queryDictionary];
return [self openSchema:schema params:vcParam];
}else{
NSLog(@"FFSchemaManager Log: system schema:%@", url);
[[UIApplication sharedApplication] openURL:url];
}
}
NSLog(@"FFLog: invalid schema url:%@, please check your schema head,it must be %@", url, self.appName);
return NO;
}
- (BOOL)openSchema:(NSString *)schema params:(NSDictionary *)params {
NSDictionary *schemaDic = self.supportedSchema[schema];
NSString *className = schemaDic[kFFSchemaName];
if (!className) {
NSLog(@"FFSchemaManager Log: className is nil, check your FFSchema.plist config");
return NO;
}
NSInteger isTabItem = [schemaDic[kFFSchemaKeyTabIndex] integerValue];
NSInteger isPresent = [schemaDic[kFFSchemaKeyIsPresent] integerValue];
// 登录处理
BOOL needLogin = [schemaDic[kFFSchemaKeyIsNeedLogin] boolValue];
if (self.isLoginBlock != nil && needLogin) {
if (!self.isLoginBlock()) {
//BUGBUG Login
return NO;
}
}
[self pushViewController:schema className:className withParams:params tabItem:isTabItem isPresent:isPresent];
return YES;
}
#pragma mark - private method
- (UINavigationController *)currentNavViewController {
UIViewController *rootViewController = [UIApplication sharedApplication].keyWindow.rootViewController;
if ([rootViewController isKindOfClass:[UITabBarController class]]) {
UITabBarController *tabBarController = (UITabBarController *)rootViewController;
if ([tabBarController.selectedViewController isKindOfClass:[UINavigationController class]]) {
rootViewController = (UINavigationController *)tabBarController.selectedViewController;
}
}
if ([rootViewController isKindOfClass:[UINavigationController class]]) {
return (UINavigationController *)rootViewController;
}
if ([rootViewController isKindOfClass:[UIViewController class]]) {
return rootViewController.navigationController;
}
return nil;
}
- (NSArray *)getAppSchemas {
NSDictionary *infoDict = [[NSBundle mainBundle] infoDictionary];
NSArray *dictArray = [infoDict objectForKey:@"CFBundleURLTypes"];
NSMutableArray *schemaArray = [NSMutableArray array];
for (NSDictionary *dict in dictArray) {
NSArray *parts = [dict objectForKey:@"CFBundleURLSchemes"];
if ([parts count] > 0) {
[schemaArray addObjectsFromArray:parts];
}
}
return schemaArray;
}
- (void)pushViewController:(NSString *)schema className:(NSString *)className withParams:(NSDictionary *)params tabItem:(NSInteger)tabbarIndex isPresent:(NSInteger)isPresent{
//If rootViewController is UITabBarController,do select tabbar Index
UIViewController *rootViewController = [UIApplication sharedApplication].delegate.window.rootViewController;
BOOL isRootTabController = [rootViewController isKindOfClass:[UITabBarController class]];
if (_customTabbarControllerClassNameArray.count > 0) {
for (NSString *customClassName in _customTabbarControllerClassNameArray) {
isRootTabController = [rootViewController isKindOfClass:NSClassFromString(customClassName)];
if (isRootTabController == YES) {
break;
}
}
}
if (tabbarIndex >= 0 && isRootTabController) {
//对于以tabViewController的App 0就是首页,无需做任何事情
if (tabbarIndex == 0) {
return;
}
UINavigationController *navi = (UINavigationController *)[(UITabBarController *)rootViewController selectedViewController];
if (!navi) {
NSLog(@"FFSchemaManager Log: rootViewController is UITabBarController,but it do not have a selectedViewController,anything error?");
return;
}
if (navi.topViewController.presentedViewController) {
[navi.topViewController.presentedViewController dismissViewControllerAnimated:NO completion:nil];
}
[navi.topViewController dismissViewControllerAnimated:NO completion:nil];
[navi popToRootViewControllerAnimated:NO];
if (tabbarIndex < ((UITabBarController *)rootViewController).viewControllers.count) {
[(UITabBarController *)rootViewController setSelectedIndex:tabbarIndex];
}
((UITabBarController *)rootViewController).hidesBottomBarWhenPushed = YES;
//此处不能return
}
UINavigationController *currentNavViewController = [self currentNavViewController];
if (!currentNavViewController) {
NSLog(@"FFSchemaManager Log:can not get current navigation viewcontroller,it's nil");
return;
}
//init destine ViewController
Class desVCClass = NSClassFromString(className);
UIViewController *desViewController = nil;
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wundeclared-selector"
if (_storyboardName.length > 0) {
//storyboard style
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:_storyboardName bundle:nil];
desViewController = [storyboard instantiateViewControllerWithIdentifier:className];
//need host + params
if (class_respondsToSelector(desVCClass, @selector(setSchema:params:))) {
[desViewController performSelector:@selector(setSchema:params:) withObject:schema withObject:params];
}
//just need params
else if (class_respondsToSelector(desVCClass, @selector(setSchemaParam:))) {
[desViewController performSelector:@selector(setSchemaParam:) withObject:params];
}
}else{
//xib or code style
//need host + params
if (class_respondsToSelector(desVCClass, @selector(initWithScheme:params:))) {
desViewController = [[desVCClass alloc] performSelector:@selector(initWithScheme:params:) withObject:schema withObject:params];
}
//need host
else if (class_respondsToSelector(desVCClass, @selector(initWithScheme:))) {
desViewController = [[desVCClass alloc] performSelector:@selector(initWithScheme:) withObject:params];
}
}
#pragma clang diagnostic pop
if (!desViewController) {
NSLog(@"FFSchemaManager Log:desViewController int failed,it's nil");
return;
}
if(isPresent == 0){
if (isRootTabController) {
desViewController.hidesBottomBarWhenPushed = YES;
}
[currentNavViewController pushViewController:desViewController animated:_vcPushAnimation];
}else{
[currentNavViewController presentViewController:desViewController animated:YES completion:nil];
}
}
- (void)addTabBarControllerClassName:(NSString *)className
{
if (!_customTabbarControllerClassNameArray) {
self.customTabbarControllerClassNameArray = [NSMutableArray arrayWithCapacity:2];
}
[_customTabbarControllerClassNameArray addObject:className];
}
@end