1 Star 0 Fork 3

杨码云 / MGJRouter

forked from iFlyCai / MGJRouter 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

MGJRouter

一个高效/灵活的 iOS URL Router

2015-08-22 更新

添加了同步获取 Object 的方法

有些场景我们可能需要根据 URL 获取某个 Object,所以就添加了这个方法

UIView *searchTopBar = [MGJRouter objectForURL:@"search_top_bar"];

为什么要再造一个轮子?

已经有几款不错的 Router 了,如 JLRoutes, HHRouter, 但细看了下之后发现,还是不太满足需求。

JLRoutes 的问题主要在于查找 URL 的实现不够高效,通过遍历而不是匹配。还有就是功能偏多。

HHRouter 的 URL 查找是基于匹配,所以会更高效,MGJRouter 也是采用的这种方法,但它跟 ViewController 绑定地过于紧密,一定程度上降低了灵活性。

于是就有了 MGJRouter。

安装

pod 'MGJRouter', '~>0.9.0'

使用姿势

最基本的使用

[MGJRouter registerURLPattern:@"mgj://foo/bar" toHandler:^(NSDictionary *routerParameters) {
    NSLog(@"routerParameterUserInfo:%@", routerParameters[MGJRouterParameterUserInfo]);
}];

[MGJRouter openURL:@"mgj://foo/bar"];

当匹配到 URL 后,routerParameters 会自带几个 key

extern NSString *const MGJRouterParameterURL;
extern NSString *const MGJRouterParameterCompletion;
extern NSString *const MGJRouterParameterUserInfo;

处理中文也没有问题

[MGJRouter registerURLPattern:@"mgj://category/家居" toHandler:^(NSDictionary *routerParameters) {
    NSLog(@"routerParameters:%@", routerParameters);
}];

[MGJRouter openURL:@"mgj://category/家居"];

Open 时,可以传一些 userinfo 过去

[MGJRouter registerURLPattern:@"mgj://category/travel" toHandler:^(NSDictionary *routerParameters) {
    NSLog(@"routerParameters[MGJRouterParameterUserInfo]:%@", routerParameters[MGJRouterParameterUserInfo]);
    // @{@"user_id": @1900}
}];

[MGJRouter openURL:@"mgj://category/travel" withUserInfo:@{@"user_id": @1900} completion:nil];

如果有可变参数(包括 URL Query Parameter)会被自动解析

[MGJRouter registerURLPattern:@"mgj://search/:query" toHandler:^(NSDictionary *routerParameters) {
    NSLog(@"routerParameters[query]:%@", routerParameters[@"query"]); // bicycle
    NSLog(@"routerParameters[color]:%@", routerParameters[@"color"]); // red
}];

[MGJRouter openURL:@"mgj://search/bicycle?color=red"];

定义一个全局的 URL Pattern 作为 Fallback

[MGJRouter registerURLPattern:@"mgj://" toHandler:^(NSDictionary *routerParameters) {
    NSLog(@"没有人处理该 URL,就只能 fallback 到这里了");
}];

[MGJRouter openURL:@"mgj://search/travel/china?has_travelled=0"];

当 Open 结束时,执行 Completion Block

[MGJRouter registerURLPattern:@"mgj://detail" toHandler:^(NSDictionary *routerParameters) {
    NSLog(@"匹配到了 url, 一会会执行 Completion Block");

    // 模拟 push 一个 VC
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.25 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        void (^completion)() = routerParameters[MGJRouterParameterCompletion];
        if (completion) {
            completion();
        }
    });
}];

[MGJRouter openURL:@"mgj://detail" withUserInfo:nil completion:^{
    [self appendLog:@"Open 结束,我是 Completion Block"];
}];

生成 URL

URL 的处理一不小心,就容易散落在项目的各个角落,不容易管理。比如注册时的 pattern 是 mgj://beauty/:id,然后 open 时就是 mgj://beauty/123,这样到时候 url 有改动,处理起来就会很麻烦,不好统一管理。

所以 MGJRouter 提供了一个类方法来处理这个问题。

+ (NSString *)generateURLWithPattern:(NSString *)pattern parameters:(NSArray *)parameters;

使用方式

#define TEMPLATE_URL @"mgj://search/:keyword"

[MGJRouter registerURLPattern:TEMPLATE_URL  toHandler:^(NSDictionary *routerParameters) {
    NSLog(@"routerParameters[keyword]:%@", routerParameters[@"keyword"]); // Hangzhou
}];

[MGJRouter openURL:[MGJRouter generateURLWithPattern:TEMPLATE_URL parameters:@[@"Hangzhou"]]];
}

这样就可以在一个地方定义所有的 URL Pattern,使用时,用这个方法生成 URL 就行了。

协议

MGJRouter 被许可在 MIT 协议下使用。查阅 LICENSE 文件来获得更多信息。

The MIT License (MIT) Copyright (c) 2015 mogujie Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

一个高效/灵活的 iOS URL Router 展开 收起
Objective-C
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
Objective-C
1
https://gitee.com/yangyuefanKEN/MGJRouter.git
git@gitee.com:yangyuefanKEN/MGJRouter.git
yangyuefanKEN
MGJRouter
MGJRouter
master

搜索帮助