ZPRouter
接入路由組件ZPRouter只需2個配置,就可以使用路由組件
// 注冊scheme協(xié)議頭
+ (void)registerScheme:(NSString *)scheme;
// 添加路由規(guī)則表
+ (void)addRouteWithPlistPath:(NSString *)path;
路由協(xié)議
在接入路由之前饱狂,需要先了解一下路由的協(xié)議規(guī)則,ZPRouter與絕大部分路由協(xié)議一樣曹步,使用:
scheme://host/path?k=v&k2=v2&k3=v3
不同點在于,ZPRouter優(yōu)化了復(fù)雜的query數(shù)據(jù)情況休讳,增加用于統(tǒng)一處理業(yè)務(wù)數(shù)據(jù)的
k=v
scheme://host/path?k=v&k2=v2&data={"k":"v","k2":"v2"}
- k=v、k2=v2為協(xié)議本身數(shù)據(jù)酵紫,
- data={"k1":"v1"} 為此url的業(yè)務(wù)數(shù)據(jù)橄唬,因為url跳轉(zhuǎn)需要的業(yè)務(wù)數(shù)據(jù)更加復(fù)雜,
常常伴隨著內(nèi)嵌的url等特殊情況械筛,所以有必要對業(yè)務(wù)數(shù)據(jù)存放一個獨立的k-v。
如下抛计,是一個跳轉(zhuǎn)簡歷詳情的跳轉(zhuǎn)url
bangjob://zcmclient/resumeDetailView?rf=1&data={
"from": 1,
"resumedata": {
"userid": "123",
"isdate": 0,
"phone": "xxx",
"url": "https://www.baidu.com",
"sid": "483209478123",
"resumeid": "4534u253u125retest**",
"phoneProtected": 1
}
}
注意:需要對業(yè)務(wù)數(shù)據(jù)進行encode,因為它是一個json串.
頁面規(guī)則制定
ZPRouter的頁面規(guī)則制定波俄,不是傳統(tǒng)的由代碼進行注冊昏翰,而是交給路由規(guī)則表進行配置。
{
"schemename":{
"main":{
"home":{
"_extend":"擴展參數(shù)",
"_class":"HomeContoller",
"_description":"首頁"
}
},
"module_a":{
"home":{
"_extend":"擴展參數(shù)",
"_class":"ModuleHomeContoller",
"_description":"業(yè)務(wù)線A的home頁面"
},
"detail":{
"_extend":"擴展參數(shù)",
"_description":"業(yè)務(wù)線A的詳情頁頁面",
"_hold":{
"_class":"DetailHold"
}
}
}
}
}
路由表遵循 scheme://host/path 的協(xié)議規(guī)則码邻,此處規(guī)則表對于關(guān)系scheme協(xié)議為:
schemename://main/home 主App的首頁奏甫,頁面=HomeContoller
schemename://module_a/home 業(yè)務(wù)線A的首頁胜蛉,頁面=ModuleHomeContoller
schemename://module_a/detail 業(yè)務(wù)線A的詳情頁,攔截處理類=DetailHold
main 和 module_a 代表不同的業(yè)務(wù)線模塊案怯,如果不需要區(qū)分業(yè)務(wù)線至会,可以統(tǒng)一命名术陶。
接入ZPRouter
了解完上面的路由協(xié)議和規(guī)則配置塘匣,接下來只需要簡單的對組件進行基礎(chǔ)配置即可脓豪。
一、注冊協(xié)議命名規(guī)則
// 注冊scheme命名 (必須)
[ZPRouteConfig registerScheme:@"routerdemo"];
// 注冊host命名, (非必須忌卤,不區(qū)分業(yè)務(wù)線默認(rèn)叫client)
[ZPRouteConfig setModule:@"main"]
// 注冊query中存放業(yè)務(wù)數(shù)據(jù)的key命名 (非必須)
[ZPRouteConfig registerModule:@"data"]
// 此處代碼URI為: routerdemo://main/xxx?data=xxx
二扫夜、添加規(guī)則數(shù)據(jù)
// 添加一個規(guī)則配置表
[ZPRouteConfig addRouteWithPlistPath:@"xxx.plist"];
// or 添加多個規(guī)則配置表
[ZPRouteConfig addRouteWithPlistPaths:@[
@"xxx.plist",
@"xxx.plist",
@"xxx.plist"
]];
// or 添加服務(wù)端下發(fā)的規(guī)則配置數(shù)據(jù)
[ZPRouteConfig addRouteDictionary:dict];
三、回調(diào)函數(shù)
// 未登錄下是否允許跳轉(zhuǎn)驰徊,默認(rèn)YES笤闯, 設(shè)置NO后需要等待登錄成功后自動跳轉(zhuǎn)
+ (BOOL)routeAllowJumpNotLogin;
// URL即將跳轉(zhuǎn)
+ (void)routeWillJump:(NSString *)url scheme:(ZPRouteScheme *)scheme customInfo:(NSDictionary *)customInfo;
// URL跳轉(zhuǎn)失敗
+ (void)routeFailed:(NSString *)url scheme:(ZPRouteScheme *)scheme fromPage:(UIViewController *)controller;
// URL跳轉(zhuǎn)成功
+ (void)routeSuccess:(NSString *)url scheme:(ZPRouteScheme *)scheme fromPage:(UIViewController *)controller;
// 跳轉(zhuǎn)規(guī)則不滿足
+ (void)routeError:(NSString *)url scheme:(ZPRouteScheme *)scheme fromPage:(UIViewController *)controller;
// 外部喚起未登錄情況下,跳轉(zhuǎn)被攔截
+ (void)routeToNotLogin:(NSString *)url fromPage:(UIViewController *)controller from:(URLRouteFromType)from;
使用方法
普通跳轉(zhuǎn)
[self openRouteURLString:@"demo://module_a/home" parameter:nil options:nil];
url含有參數(shù)跳轉(zhuǎn)
// 發(fā)起跳轉(zhuǎn)頁
NSString *url = @"demo://module_a/home?data={\"name\":\"張三\",\"age\":\"20\"}";
[self openRouteURLString:url parameter:nil options:nil];
// 1辣垒、目的頁Controller接收數(shù)據(jù)
- (void)routeWillPushControllerWithResult:(ZPRouteResultModel *)result {
NSDictionary *dataParams = result.data;
NSLog(@"姓名:%@ 年齡:%@",dataParams[@"name"],dataParams[@"age"]);
}
// 2望侈、如果跳轉(zhuǎn)被攔截,則由攔截類接收數(shù)據(jù)
// return yes-業(yè)務(wù)能正常跳轉(zhuǎn)勋桶, no-業(yè)務(wù)不能正常跳轉(zhuǎn)脱衙。 用于收集跳轉(zhuǎn)失敗的數(shù)據(jù)。
- (BOOL)holdWithParameters:(ZPRouteResultModel *)result
{
NSDictionary *data = result.data;
if ([data[@"age"] intValue] < 18) {
// 18歲以下禁入
// push error page
return NO;
}
// 18歲以上 go
// push right page
return YES;
}
跳轉(zhuǎn)含有自定義參數(shù)
[self openRouteURLString:@"demo://module_a/home" parameter:@{@"customInfo":@"自定義數(shù)據(jù)"} options:nil];
跳轉(zhuǎn)完成回調(diào)
[self openRouteURLString:@"demo://module_a/pageTwo" parameter:nil options:nil completion:^{
NSLog(@"跳轉(zhuǎn)完成回調(diào)");
} callParams:nil];
跳轉(zhuǎn)回傳數(shù)據(jù)
// 發(fā)起跳轉(zhuǎn)頁接收回傳數(shù)據(jù)
[self openRouteURLString:@"demo://module_a/pageTwo" parameter:nil options:nil completion:nil callParams:^(NSDictionary * _Nonnull params) {
NSLog(@"------收到回傳參數(shù)");
if (params[@"vip"] && params[@"age"] > 18) {
// 會員18歲了例驹,可以開放功能
}
}];
// 目的頁回傳數(shù)據(jù)
- (void)routeWillPushControllerWithResult:(ZPRouteResultModel *)result {
if (self.callParams) {
self.callParams(@{@"vip":@"yes",@"age":@"20"});
}
}
Present跳轉(zhuǎn)
// kURLRouteOpenAnimatedTransition 跳轉(zhuǎn)類型push還是present
// kURLRouteOpenAnimated 跳轉(zhuǎn)是否需要動畫
NSDictionary *options = @{kURLRouteOpenAnimatedTransition:@(URLRouteOpenAnimatedPresent),
kURLRouteOpenAnimated:@(YES)
};
[self openRouteURLString:@"demo://module_a/pageThree" parameter:nil options:options];
跳轉(zhuǎn)普通的H5頁面
// 需要對https或者h(yuǎn)ttp配置響應(yīng)的scheme跳轉(zhuǎn)規(guī)則,具體參考demo
[self openRouteURLString:@"https://www.baidu.com" parameter:nil options:nil];
復(fù)雜情況跳轉(zhuǎn)到H5頁面
// 跳轉(zhuǎn)到H5頁面捐韩,也可以配置統(tǒng)一的規(guī)則進行跳轉(zhuǎn),這種方式可以添加更多的數(shù)據(jù)進行頁面配置鹃锈。
[self openRouteURLString:@"demo://mainClient/web?data={\"url\":\"https://www.baidu.com\",\"title\":\"web標(biāo)題\"}" parameter:nil options:nil];
未登錄攔截荤胁,登錄后繼續(xù)跳轉(zhuǎn)
// app在登錄成功時調(diào)用此函數(shù),
+ (void)sendLoginStatus:(BOOL)isLogin;
安全性和動態(tài)更新規(guī)則
路由配置表推薦使用本地plist文件進行存儲配置屎债,如果考慮到安全性或需要動態(tài)更新跳轉(zhuǎn)規(guī)則仅政,可以由接口下發(fā)數(shù)據(jù)。
接口獲取的配置數(shù)據(jù)需要在app啟動時去獲取盆驹,避免外部喚起時配置數(shù)據(jù)未更新完成無法跳轉(zhuǎn)圆丹,具體需要接入方去實現(xiàn)請求和跳轉(zhuǎn)的時機。