Commit cc32b85d authored by jz's avatar jz

modify router

parent c0f1d04b
...@@ -41,7 +41,7 @@ extern NSString *const GMRouterProtocolPrefix; ...@@ -41,7 +41,7 @@ extern NSString *const GMRouterProtocolPrefix;
* {@"service_id": @"5930",@"is_new_special": @0} * {@"service_id": @"5930",@"is_new_special": @0}
* @return vc的实例 * @return vc的实例
*/ */
- (id)pushScheme:(NSString *)urlScheme params:(NSDictionary *)params; - (id)pushScheme:(NSString *)urlScheme params:(NSDictionary *)params completeBlock:(GMRouterBlock)routerBlock;
/** /**
* 初始化Map * 初始化Map
......
...@@ -35,46 +35,8 @@ static NSMutableDictionary *routeMap = nil; ...@@ -35,46 +35,8 @@ static NSMutableDictionary *routeMap = nil;
- (void)initializeRouteMap { - (void)initializeRouteMap {
routeMap = [[NSMutableDictionary alloc] initWithCapacity:50]; routeMap = [[NSMutableDictionary alloc] initWithCapacity:50];
self.targets = [NSMutableArray arrayWithObjects:GMRouterTargetAI, GMRouterTargetBanking, GMRouterTargetCommunity, GMRouterTargetWeb, nil]; self.targets = [NSMutableArray arrayWithObjects:GMRouterTargetAI, GMRouterTargetBanking, GMRouterTargetCommunity, GMRouterTargetWeb, nil];
} }
#pragma mark - 获取类的所有方法
// 获取所有的方法
- (NSDictionary *)getMethods:(NSString *)clsStr {
Class cls = NSClassFromString(clsStr);
NSRange range = [clsStr rangeOfString:@"Target_"];
NSString *targetValue = [clsStr substringFromIndex:range.length];
NSAssert(targetValue.length != 0, @"Target_后不能为空!请注意Target");
unsigned int count = 0;
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
// 获取类的所有 Method
Method *methods = class_copyMethodList([cls class], &count);
for (NSUInteger i = 0; i < count; i ++) {
// 获取方法 Name
SEL methodSEL = method_getName(methods[i]);
const char *methodName = sel_getName(methodSEL);
NSString *name = [NSString stringWithUTF8String:methodName];
//获取到的是这样的 pushToHospitalDetail: 因此要去掉:
NSString *rangeStr = @":";
if ([name containsString:rangeStr]) {
NSRange range = [name rangeOfString:rangeStr];
name = [name substringToIndex:range.location];
}
NSString *promoteStr = [NSString stringWithFormat:@"%@-内有重复的方法名-%@", clsStr, name];
NSAssert(![dict.allKeys containsObject:name], promoteStr);
//因为消息发送的时候会有两个默认的参数(消息接收者和方法名),所以需要减去2
dict[name] = targetValue;
}
free(methods);
return dict;
}
- (id)pushScheme:(NSString *)urlScheme { - (id)pushScheme:(NSString *)urlScheme {
return [self pushScheme:urlScheme completeBlock:NULL]; return [self pushScheme:urlScheme completeBlock:NULL];
...@@ -87,16 +49,24 @@ static NSMutableDictionary *routeMap = nil; ...@@ -87,16 +49,24 @@ static NSMutableDictionary *routeMap = nil;
} else { } else {
encodeUrlScheme = [NSString stringWithFormat:@"%@%@", GMRouterProtocolPrefix ,urlScheme]; encodeUrlScheme = [NSString stringWithFormat:@"%@%@", GMRouterProtocolPrefix ,urlScheme];
} }
NSURL *url = [NSURL URLWithString:encodeUrlScheme];
if (!url) {
// debugLog(@"协议出错了!");
}
NSString *host = url.host;
NSString *targetName = [routeMap objectForKey:host];
NSDictionary *params = [self getParams:encodeUrlScheme withHost:host];
host = [self getHostWithEncodeUrlScheme:encodeUrlScheme host:host]; NSDictionary *params = [self getParams:encodeUrlScheme];
return [self pushScheme:urlScheme params:params completeBlock:routerBlock];
}
- (id)pushScheme:(NSString *)urlScheme params:(NSDictionary *)params completeBlock:(GMRouterBlock)routerBlock {
NSMutableDictionary *allParams = [NSMutableDictionary dictionaryWithDictionary:params];
NSString *encodeUrlScheme = [self URLEncodeString:urlScheme];
NSString *host = [self getHost:urlScheme];
NSString *targetName = [routeMap objectForKey:host];
NSDictionary *paramsDict = [self getParams:encodeUrlScheme];
[allParams addEntriesFromDictionary:paramsDict];
host = [self safeHostWithEncodeUrlScheme:encodeUrlScheme host:host];
id vc = [self performTarget:targetName action:host params:params shouldCacheTarget:NO]; id vc = [self performTarget:targetName action:host params:params shouldCacheTarget:NO];
//如果是push 过来的 或者需要回调的在此赋值
if ([vc isKindOfClass:[UIViewController class]]) { if ([vc isKindOfClass:[UIViewController class]]) {
UIViewController *VC = (UIViewController *)vc; UIViewController *VC = (UIViewController *)vc;
[self vcAutoIsPushWith:vc params:params]; [self vcAutoIsPushWith:vc params:params];
...@@ -105,9 +75,19 @@ static NSMutableDictionary *routeMap = nil; ...@@ -105,9 +75,19 @@ static NSMutableDictionary *routeMap = nil;
return vc; return vc;
} }
// 获取host
- (NSString *)getHost:(NSString *)encodeUrlScheme {
NSURL *url = [NSURL URLWithString:encodeUrlScheme];
if (!url) {
// debugLog(@"协议出错了!");
}
return url.host;
}
#pragma mark - 处理协议后面待的参数 #pragma mark - 处理协议后面待的参数
- (NSDictionary *)getParams:(NSString *)encodeUrlScheme withHost:(NSString *)host { - (NSDictionary *)getParams:(NSString *)encodeUrlScheme {
NSString *host = [self getHost:encodeUrlScheme];
NSDictionary *params; NSDictionary *params;
NSArray *array = [encodeUrlScheme componentsSeparatedByString:@"url="]; NSArray *array = [encodeUrlScheme componentsSeparatedByString:@"url="];
if (([host isEqualToString:@"third_webview"] || [host isEqualToString:@"common_webview"]) && array.count > 1) { if (([host isEqualToString:@"third_webview"] || [host isEqualToString:@"common_webview"]) && array.count > 1) {
...@@ -125,7 +105,8 @@ static NSMutableDictionary *routeMap = nil; ...@@ -125,7 +105,8 @@ static NSMutableDictionary *routeMap = nil;
return params; return params;
} }
- (NSString *)getHostWithEncodeUrlScheme:(NSString *)encodeUrlScheme host:(NSString *)host { // 如果不在白名单的三方url 禁止获取我们APP内重要信息
- (NSString *)safeHostWithEncodeUrlScheme:(NSString *)encodeUrlScheme host:(NSString *)host {
NSArray *array = [encodeUrlScheme componentsSeparatedByString:@"url="]; NSArray *array = [encodeUrlScheme componentsSeparatedByString:@"url="];
if (([host isEqualToString:@"third_webview"] || [host isEqualToString:@"common_webview"]) && array.count > 1) { if (([host isEqualToString:@"third_webview"] || [host isEqualToString:@"common_webview"]) && array.count > 1) {
NSString *value = array[1]; NSString *value = array[1];
...@@ -146,21 +127,6 @@ static NSMutableDictionary *routeMap = nil; ...@@ -146,21 +127,6 @@ static NSMutableDictionary *routeMap = nil;
return host; return host;
} }
- (id)pushScheme:(NSString *)urlScheme params:(NSDictionary *)params {
NSMutableDictionary *allParams = [NSMutableDictionary dictionaryWithDictionary:params];
NSString *encodeUrlScheme = [self URLEncodeString:urlScheme];
NSURL *url = [NSURL URLWithString:encodeUrlScheme];
if (!url) {
// debugLog(@"协议出错了!");
}
NSString *host = url.host;
NSString *targetName = [routeMap objectForKey:host];
NSDictionary *paramsDict = [self getParams:encodeUrlScheme withHost:host];
[allParams addEntriesFromDictionary:paramsDict];
host = [self getHostWithEncodeUrlScheme:encodeUrlScheme host:host];
return [self performTarget:targetName action:host params:allParams shouldCacheTarget:NO];
}
#pragma mark - autoIsPush #pragma mark - autoIsPush
- (void)vcAutoIsPushWith:(UIViewController *)vc params:(NSDictionary *)params { - (void)vcAutoIsPushWith:(UIViewController *)vc params:(NSDictionary *)params {
if (vc != nil && ([params.allKeys containsObject:@"ispush"])) { if (vc != nil && ([params.allKeys containsObject:@"ispush"])) {
...@@ -236,4 +202,42 @@ static NSMutableDictionary *routeMap = nil; ...@@ -236,4 +202,42 @@ static NSMutableDictionary *routeMap = nil;
[routeMap addEntriesFromDictionary:dict]; [routeMap addEntriesFromDictionary:dict];
} }
} }
#pragma mark - 获取类的所有方法
// 获取所有的方法
- (NSDictionary *)getMethods:(NSString *)clsStr {
Class cls = NSClassFromString(clsStr);
NSRange range = [clsStr rangeOfString:@"Target_"];
NSString *targetValue = [clsStr substringFromIndex:range.length];
NSAssert(targetValue.length != 0, @"Target_后不能为空!请注意Target");
unsigned int count = 0;
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
// 获取类的所有 Method
Method *methods = class_copyMethodList([cls class], &count);
for (NSUInteger i = 0; i < count; i ++) {
// 获取方法 Name
SEL methodSEL = method_getName(methods[i]);
const char *methodName = sel_getName(methodSEL);
NSString *name = [NSString stringWithUTF8String:methodName];
//获取到的是这样的 pushToHospitalDetail: 因此要去掉:
NSString *rangeStr = @":";
if ([name containsString:rangeStr]) {
NSRange range = [name rangeOfString:rangeStr];
name = [name substringToIndex:range.location];
}
NSString *promoteStr = [NSString stringWithFormat:@"%@-内有重复的方法名-%@", clsStr, name];
NSAssert(![dict.allKeys containsObject:name], promoteStr);
//因为消息发送的时候会有两个默认的参数(消息接收者和方法名),所以需要减去2
dict[name] = targetValue;
}
free(methods);
return dict;
}
@end @end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment