Commit 8e52ca96 authored by jz's avatar jz

modify router

parent 2ee41ac2
This diff is collapsed.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<!--
gm_router.plist
Pods
Created by Q14 on 2019/12/10.
Copyright (c) 2019 ___ORGANIZATIONNAME___. All rights reserved.
-->
<plist version="1.0">
<dict>
</dict>
</plist>
......@@ -7,6 +7,7 @@
#import "GMRouter+gm.h"
#import <objc/message.h>
#import <objc/runtime.h>
//nsstring
static inline BOOL verifiedString(id strlike) {
......@@ -21,20 +22,69 @@ NSString *const GMRouterActionPrefix = @"Action_";
NSString *const GMRouterActionSuffix = @":";
NSString *const GMRouterTargetPrefix = @"Target_";
/**
增加一个魔块 需要在路由做映射
*/
NSString *const GMRouterTargetAI = @"Target_AI";
NSString *const GMRouterTargetBanking = @"Target_Banking";
NSString *const GMRouterTargetCommunity = @"Target_Community";
NSString *const GMRouterTargetWeb = @"Target_Web";
static NSMutableDictionary *routeMap = nil;
@implementation GMRouter (gm)
+ (void)initialize {
//私有库中只能这样写
NSBundle *bundle = [NSBundle bundleForClass:[GMRouter class]];
NSURL *bundleURL = [bundle URLForResource:@"QJRouter" withExtension:@"bundle"];
NSBundle *plistBundle = [NSBundle bundleWithURL:bundleURL];
NSURL *plistUrl = [plistBundle URLForResource:@"gm_router" withExtension:@"plist"];
if (!routeMap) {
routeMap = [[NSMutableDictionary alloc] initWithContentsOfURL:plistUrl];
routeMap = [[NSMutableDictionary alloc] initWithCapacity:50];
NSArray *arr = @[GMRouterTargetAI, GMRouterTargetBanking, GMRouterTargetCommunity, GMRouterTargetWeb];
for (NSString *clsStr in arr) {
NSDictionary *dict = [self getMethods:clsStr];
[routeMap addEntriesFromDictionary:dict];
}
}
}
#pragma mark - 获取类的所有方法
// 获取所有的方法
+ (NSDictionary *)getMethods:(NSString *)clsStr {
Class cls = NSClassFromString(clsStr);
NSRange range = [clsStr rangeOfString:@"Target_"];
NSString *targetValue = [clsStr substringFromIndex:range.length];
if (!targetValue.length) {
return @{};
}
NSObject *target = [[cls alloc] init];
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];
}
// 获取方法的参数列表
int arguments = method_getNumberOfArguments(methods[i]);
//因为消息发送的时候会有两个默认的参数(消息接受者和方法名),所以需要减去2
dict[name] = targetValue;
}
free(methods);
return dict;
}
- (id)performAction:(NSString *)actionName params:(NSDictionary *)params shouldCacheTarget:(BOOL)shouldCacheTarget{
NSString *selName = @"createVC:";
......@@ -81,12 +131,10 @@ static NSMutableDictionary *routeMap = nil;
// debugLog(@"协议出错了!");
}
NSString *host = url.host;
NSDictionary *dict = [routeMap objectForKey:host];
NSString *sel = dict[@"sel"];
NSString *targetName = dict[@"target"];
NSString *targetName = [routeMap objectForKey:host];
NSDictionary *params = [self urlQueryToDictionary:encodeUrlScheme];
return [self performTarget:targetName action:sel params:params shouldCacheTarget:NO];
return [self performTarget:targetName action:host params:params shouldCacheTarget:NO];
}
- (id)pushScheme:(NSString *)urlScheme params:(NSDictionary *)params {
......@@ -95,10 +143,9 @@ static NSMutableDictionary *routeMap = nil;
if (!url) {
}
NSString *host = url.host;
NSDictionary *dict = [routeMap objectForKey:host];
NSString *sel = dict[@"sel"];
NSString *targetName = dict[@"target"];
return [self performTarget:targetName action:sel params:params shouldCacheTarget:NO];
NSString *targetName = [routeMap objectForKey:host];
NSDictionary *paramsDict = [self urlQueryToDictionary:encodeUrlScheme];
return [self performTarget:targetName action:host params:paramsDict shouldCacheTarget:NO];
}
#pragma mark - string to dict
......
//
// NSObject+gmKey.h
// MJExtension
//
// Created by Q14 on 2019/12/4.
//
#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
@interface NSObject (gmKey)
@end
NS_ASSUME_NONNULL_END
//
// NSObject+gmKey.m
// MJExtension
//
// Created by Q14 on 2019/12/4.
//
#import "NSObject+gmKey.h"
#import <MJExtension/MJExtension.h>
@implementation NSObject (gmKey)
+ (id)mj_replacedKeyFromPropertyName121:(NSString *)propertyName {
return [propertyName mj_underlineFromCamel];
}
@end
//
// Target_commons.h
// GMRouter
//
// Created by Q14 on 2019/11/28.
//
#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
extern NSString * const GMRouterTargetCommons;
@interface Target_commons : NSObject
// 自定义push方法
- (UIViewController *)push_CommonViewController:(NSString *)stringVCName params:(NSDictionary *)params;
@end
NS_ASSUME_NONNULL_END
//
// Target_commons.m
// GMRouter
//
// Created by Q14 on 2019/11/28.
//
#import "Target_commons.h"
NSString * const GMRouterTargetCommons = @"commons";
@implementation Target_commons
// 自定义push方法
- (UIViewController *)push_CommonViewController:(NSString *)stringVCName params:(NSDictionary *)params {
// 因为action是从属于ModuleA的,所以action直接可以使用ModuleA里的所有声明
Class class = NSClassFromString(stringVCName);
UIViewController *controller = [[class alloc] init];
return controller;
}
@end
//
// UIViewController+Router.h
// GMRouter
//
// Created by Q14 on 2019/11/28.
//
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
@interface UIViewController (Router)
- (id)createVC:(NSDictionary *)dict;
@end
NS_ASSUME_NONNULL_END
//
// UIViewController+Router.m
// GMRouter
//
// Created by Q14 on 2019/11/28.
//
#import "UIViewController+Router.h"
#import "GMRouter+gm.h"
#import <MJExtension/MJExtension.h>
@implementation UIViewController (Router)
- (id)createVC:(NSDictionary *)dict{
Class class = getClassFromAtcion(_cmd);
if (class) {
UIViewController *doc = self;
doc = [[class alloc]init];
doc = [doc mj_setKeyValues:dict];
return doc;
}
return nil;
}
@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