前言
隨著用戶的需求越來越多问畅,對App的用戶體驗(yàn)也變的要求越來越高六荒。為了更好的應(yīng)對各種需求,開發(fā)人員從軟件工程的角度卵皂,將App架構(gòu)由原來簡單的MVC變成MVVM灯变,VIPER等復(fù)雜架構(gòu)添祸。更換適合業(yè)務(wù)的架構(gòu),是為了后期能更好的維護(hù)項(xiàng)目刃泌。除此之外解耦也成為重中之重耙替,HZURLManager是HZExtend框架中的一個路由組件,借鑒天貓的統(tǒng)跳協(xié)議而實(shí)現(xiàn)硝烂。它能有效對整個應(yīng)用的各個組件進(jìn)行解耦滞谢。
HZURLManager的作用
- 通過URL就能打開指定的頁面
- 解耦
- 支持URL重寫除抛,解決跨平臺的URL不一致
- 通過URL重寫镶殷,進(jìn)行頁面降級
Demo
安裝
添加 pod 'HZURLManager' 到Podfile文件里微酬,然后運(yùn)行pod install
使用
一. URL Config
所有的URL配置都存放在plist文件里颗管,并且URL需要符合W3C的URL標(biāo)準(zhǔn)垦江,如下圖所示
1.加載URL配置
[[HZURLManagerConfig sharedConfig] loadURLCtrlConfig:[[NSBundle mainBundle] pathForResource:@"URL-Controller-Config" ofType:@"plist"] urlMethodConfig:[[NSBundle mainBundle] pathForResource:@"URL-Method-Config" ofType:@"plist"]];
URL配置分為2類绽族,一類為進(jìn)行頁面跳轉(zhuǎn)吧慢,另一類為進(jìn)行方法調(diào)用赏表。即通過指定的URL來跳轉(zhuǎn)到對應(yīng)的頁面或者調(diào)用對應(yīng)的方法瓢剿。
2.添加重寫規(guī)則
[[HZURLManagerConfig sharedConfig] addRewriteRules:@[@{@"match":@"(?:https://)?www.hz.com/articles/(\\d)\\?(.*)",@"target":@"hz://page.hz/article?$query&id=$1"}]];
重寫規(guī)則可以從服務(wù)端獲取间狂,一個字典代表一個重寫規(guī)則,必須提供match和target2個字段坠韩,其中match所對應(yīng)的值是正則表達(dá)式用來匹配源跳轉(zhuǎn)URL只搁,target對應(yīng)的值是用來生成新URL的規(guī)則氢惋。
在target中$表示變量洞翩,變量名1-n表示正則表達(dá)式中元組的值,query表示標(biāo)準(zhǔn)URL中對應(yīng)的部分焰望。
添加了上述的重寫規(guī)則之后骚亿,當(dāng)準(zhǔn)備跳轉(zhuǎn)到https://ww.hz.com/articles/3?title=cool
所對應(yīng)的控制器時,URL會被重寫成hz://page.hz/article?title=cool&id=3
從而跳轉(zhuǎn)到URLItemViewController中熊赖。
3.設(shè)置http(s)URL默認(rèn)對應(yīng)的Ctrl
[HZURLManagerConfig sharedConfig].classOfWebViewCtrl = @"WebViewController";
進(jìn)行http(s)URL跳轉(zhuǎn)時如果沒有在plist文件里配置對應(yīng)的控制器来屠,則會默認(rèn)跳轉(zhuǎn)到該控制器中。
二. 跳轉(zhuǎn)
//進(jìn)行push跳轉(zhuǎn)
[URL_MANAGERN redirectToURL:@"https://www.hz.com/articles/3?title=push" animated:YES];
//進(jìn)行present跳轉(zhuǎn)
[URL_MANAGERN redirectToURL:@"hz://page.hz/article?title=present" animated:YES parmas:nil options:@{HZRedirectPresentMode:@(YES)} completion:nil];
三. 執(zhí)行方法
當(dāng)通過URL調(diào)用方法時,同樣的每個URL都唯一對應(yīng)一個URLHandler震鹉,URLHandler需要實(shí)現(xiàn)HZURLHandler協(xié)議
@interface ShowAlertURLHandler ()<HZURLHandler>
@end
@implementation ShowAlertURLHandler
/**
hz://urlmanger.kit/doAlert
@param title
@param message
*/
- (id)handleURL:(NSURL *)url withParams:(id)params
{
NSDictionary *queryParam = url.queryDic;
NSString *title = [queryParam objectForKey:@"title"];
NSString *message = [queryParam objectForKey:@"message"];
UIAlertController *alerController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *confirmAtion = [UIAlertAction actionWithTitle:@"Confirm" style:UIAlertActionStyleDefault handler:nil];
[alerController addAction:confirmAtion];
UIAlertAction *cancleAction = [UIAlertAction actionWithTitle:@"Cancle" style:UIAlertActionStyleCancel handler:nil];
[alerController addAction:cancleAction];
[[HZURLNavigation currentViewController] presentViewController:alerController animated:YES completion:nil];
return nil;
}
@end
然后通過HZURLManager就能調(diào)用上述方法
[URL_MANAGERN handleURL:@"hz://urlmanger.kit/doAlert?title=alert&message=URL-showAlert" withParams:nil];
四. URL參數(shù)
在控制器中可以獲取到查詢字符串參數(shù)以及最終跳轉(zhuǎn)URL俱笛。
@interface UIViewController (HZURLManager)
/**
The URL corresponding to the Controller
*/
@property(nonatomic, strong, readonly) NSString *originURL;
/**
Consists of a query string and additional parameters passed by user.
*/
@property(nonatomic, strong, readonly) NSDictionary *params;
@end
五. 導(dǎo)航
使用HZURLManager中底層的HZURLNavigation可以獲取當(dāng)前的控制器并進(jìn)行跳轉(zhuǎn)
//跳轉(zhuǎn)
UIViewController *controller = [UIViewController viewControllerForURL:[NSURL URLWithString:@"hz://page.hz/article"]];
[HZURLNavigation pushViewController:controller animated:YES];
//獲取當(dāng)前控制器
UIViewController *currentViewCtrl = [HZURLNavigation currentViewController];
//獲取當(dāng)前導(dǎo)航控制器
UIViewController *currentNavViewCtrl = [HZURLNavigation currentNavigationViewController];
//Dismiss(Pop or dissmiss) 控制器
[HZURLNavigation dismissCurrentAnimated:YES];