美文网首页HZExtend
HZExtend之路由(HZURLManager)

HZExtend之路由(HZURLManager)

作者: GeniusBrother | 来源:发表于2017-09-21 17:49 被阅读12次

    前言

    随着用户的需求越来越多,对App的用户体验也变的要求越来越高。为了更好的应对各种需求,开发人员从软件工程的角度,将App架构由原来简单的MVC变成MVVM,VIPER等复杂架构。更换适合业务的架构,是为了后期能更好的维护项目。除此之外解耦也成为重中之重,HZURLManagerHZExtend框架中的一个路由组件,借鉴天猫的统跳协议而实现。它能有效对整个应用的各个组件进行解耦。

    HZURLManager的作用

    • 通过URL就能打开指定的页面
    • 解耦
    • 支持URL重写,解决跨平台的URL不一致
    • 通过URL重写,进行页面降级

    Demo

    demo.gif

    安装

    添加 pod 'HZURLManager' 到Podfile文件里,然后运行pod install

    使用

    一. URL Config

    所有的URL配置都存放在plist文件里,并且URL需要符合W3C的URL标准,如下图所示


    URL-Controller-Config.plist URL-Method-Config.plist

    1.加载URL配置

    [[HZURLManagerConfig sharedConfig] loadURLCtrlConfig:[[NSBundle mainBundle] pathForResource:@"URL-Controller-Config" ofType:@"plist"] urlMethodConfig:[[NSBundle mainBundle] pathForResource:@"URL-Method-Config" ofType:@"plist"]];
    

    URL配置分为2类,一类为进行页面跳转,另一类为进行方法调用。即通过指定的URL来跳转到对应的页面或者调用对应的方法。

    2.添加重写规则

    [[HZURLManagerConfig sharedConfig] addRewriteRules:@[@{@"match":@"(?:https://)?www.hz.com/articles/(\\d)\\?(.*)",@"target":@"hz://page.hz/article?$query&id=$1"}]];
    

    重写规则可以从服务端获取,一个字典代表一个重写规则,必须提供match和target2个字段,其中match所对应的值是正则表达式用来匹配源跳转URL,target对应的值是用来生成新URL的规则。

    在target中$表示变量,变量名1-n表示正则表达式中元组的值,query表示标准URL中对应的部分。

    添加了上述的重写规则之后,当准备跳转到https://ww.hz.com/articles/3?title=cool所对应的控制器时,URL会被重写成hz://page.hz/article?title=cool&id=3从而跳转到URLItemViewController中。

    3.设置http(s)URL默认对应的Ctrl

    [HZURLManagerConfig sharedConfig].classOfWebViewCtrl = @"WebViewController"; 
    

    进行http(s)URL跳转时如果没有在plist文件里配置对应的控制器,则会默认跳转到该控制器中。

    二. 跳转

    //进行push跳转
    [URL_MANAGERN redirectToURL:@"https://www.hz.com/articles/3?title=push" animated:YES];
    
    //进行present跳转
    [URL_MANAGERN redirectToURL:@"hz://page.hz/article?title=present" animated:YES parmas:nil options:@{HZRedirectPresentMode:@(YES)} completion:nil];
    

    三. 执行方法

    当通过URL调用方法时,同样的每个URL都唯一对应一个URLHandler,URLHandler需要实现HZURLHandler协议

    @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就能调用上述方法

    [URL_MANAGERN handleURL:@"hz://urlmanger.kit/doAlert?title=alert&message=URL-showAlert" withParams:nil];
    

    四. URL参数

    在控制器中可以获取到查询字符串参数以及最终跳转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
    

    五. 导航

    使用HZURLManager中底层的HZURLNavigation可以获取当前的控制器并进行跳转

    //跳转
    UIViewController *controller = [UIViewController viewControllerForURL:[NSURL URLWithString:@"hz://page.hz/article"]];
    [HZURLNavigation pushViewController:controller animated:YES];
    
    //获取当前控制器
    UIViewController *currentViewCtrl = [HZURLNavigation currentViewController];
    
    //获取当前导航控制器
    UIViewController *currentNavViewCtrl = [HZURLNavigation currentNavigationViewController];
    
    //Dismiss(Pop or dissmiss) 控制器
    [HZURLNavigation dismissCurrentAnimated:YES];
    

    相关文章

      网友评论

        本文标题:HZExtend之路由(HZURLManager)

        本文链接:https://www.haomeiwen.com/subject/yedtextx.html