Route
路由實現(xiàn)功能
- 使用
- api導(dǎo)航
- 服務(wù)導(dǎo)航
- 總結(jié)
使用
解析參數(shù)
url = XXX://page/detail?goodId=12345
[[Route shared] routeWithUrl:url completion:nil];
//url對應(yīng)參數(shù)
這個方法中主要采用了prengine(我老大寫的一個基于nginx的解析框架)來進行解析
傳入?yún)?shù)調(diào)用
在這里scheme =XXX; server = page; key = detail;
parameter = {goodID = 12345};
[self routeWithScheme:scheme server:server key:key parameter:dic completion:nil];
路由規(guī)則
scheme://routeType/value/parameters
| 類型 | 參數(shù) | 示例|
| :--------: | :--------:| :--: | :--:|
| page | goodId=12345 |xxx://page/detail?goodId=12345|
| api | userId=12345 | xxx://api/GetCouponTf8Model?userId=12345 |
| server | | xxx://server/doLogin |
- 頁面導(dǎo)航、app喚起
Router中一共使用了一張plist表格來做配置文件
RouterPlist.png
-
page跳轉(zhuǎn)最后的解決方案
在父類controller里面增加這一個方法暫定為routejump需要傳入的參數(shù)應(yīng)該是controller 的class诡渴,以及這個controller需要的參數(shù)蚯姆,這里主要還是需要進行一次參數(shù)校驗。
NSMutableDictionary *parameterDic = [NSMutableDictionary dictionary];
for (int i = 0; i < parameterArr.count; i++) {
if (parameter[parameterArr[i]]) {
[parameterDic setValue:parameter[parameterArr[i] forKey:parameterArr[i]];
}
}
可以直接用KVC方式賦值,而需要特殊處理的子類远荠,重寫這個routejump的方法。
- page實現(xiàn)方式
if ([clazz isSubclassOfClass:[UIViewController class]]) {
NSString *actionString = [NSString stringWithFormat:@"pageRouteActionWithClass:parameters:completion:"];
NSArray *parameterArr = hhDic[routeArr[0]];
NSMutableDictionary *parameterKeyDic = [NSMutableDictionary dictionary];
//通過這種方式去取對應(yīng)類的屬性 就不需要手動輸入了
unsigned int count;
objc_property_t *properties = class_copyPropertyList(clazz, &count);
NSMutableArray *clspatArray = [NSMutableArray array];
for (int i = 0; i < count; i++) {
objc_property_t property = properties[i];
const char *cName = property_getName(property);
NSString *name = [NSString stringWithCString:cName encoding:NSUTF8StringEncoding];
[clspatArray addObject:name];
}
[clspatArray enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
if (parameter[obj]) {
[parameterKeyDic setValue:parameter[obj] forKey:obj];
}
}];
SEL action = NSSelectorFromString(actionString);
Method m = class_getClassMethod(clazz, action);
if (m) {
IMP imp = method_getImplementation(m);
BOOL (*imp1)(id,SEL,NSString *,NSMutableDictionary *,void (^)(NSDictionary *)) = (BOOL (*)(id,SEL,NSString *,NSMutableDictionary *,void (^)(NSDictionary *)))imp;
imp1(clazz,action,routeArr[0],parameterKeyDic,completion);
}
- server和api的調(diào)用
這里我創(chuàng)建了Task基類通過類名映射的方式進行調(diào)用
Task *task = [[clazz alloc] init];
task.parameter = parameter;
task.callback = completion;
[task excuteTask];
總結(jié)
基本上用的都是相對容易理解的代碼來完成了這一系列過程里面有使用一個nginx的解析工具prengine,思路比較清晰册招,用runtime里面的一些方法來完成頁面直接的解耦。