All Projects → meili → Mgjrouter

meili / Mgjrouter

Licence: mit
一个高效/灵活的 iOS URL Router

Labels

Projects that are alternatives of or similar to Mgjrouter

Andromeda
Andromeda simplifies local/remote communication for Android modularization
Stars: ✭ 2,203 (-4.8%)
Mutual labels:  router
Brouter
Stars: ✭ 198 (-91.44%)
Mutual labels:  router
Next Routes
Universal dynamic routes for Next.js
Stars: ✭ 2,354 (+1.73%)
Mutual labels:  router
Arouter
💪 A framework for assisting in the renovation of Android componentization (帮助 Android App 进行组件化改造的路由框架)
Stars: ✭ 13,587 (+487.17%)
Mutual labels:  router
Angular Router Loader
A Webpack loader that enables string-based module loading with the Angular Router
Stars: ✭ 194 (-91.62%)
Mutual labels:  router
Luci Wrtbwmon
Bandwidth tracker for OpenWRT that uses wrtbwmon
Stars: ✭ 201 (-91.31%)
Mutual labels:  router
Routerify
A lightweight, idiomatic, composable and modular router implementation with middleware support for the Rust HTTP library hyper.rs
Stars: ✭ 173 (-92.52%)
Mutual labels:  router
Rocon
Router Library with Ultimate Type Safety
Stars: ✭ 206 (-91.1%)
Mutual labels:  router
Bilihp App
BiliBili助手-哔哩哔哩助手苹果/安卓/IOS/PC/C2C/Mac/路由器/单用户/多用户/手机版全平台支持挂机软件库(2020-BiliHP)
Stars: ✭ 196 (-91.53%)
Mutual labels:  router
Packet Journey
DEPRECATED - Packet-journey, userland router which uses DPDK for its fastpath switching.
Stars: ✭ 200 (-91.36%)
Mutual labels:  router
Router Store
Bindings to connect the Angular Router to @ngrx/store
Stars: ✭ 187 (-91.92%)
Mutual labels:  router
Component
🔥🔥🔥A powerful componentized framework.一个强大、100% 兼容、支持 AndroidX、支持 Kotlin并且灵活的组件化框架
Stars: ✭ 2,434 (+5.19%)
Mutual labels:  router
Navigo
A simple vanilla JavaScript router.
Stars: ✭ 2,435 (+5.23%)
Mutual labels:  router
Mithril.js
A JavaScript Framework for Building Brilliant Applications
Stars: ✭ 13,062 (+464.48%)
Mutual labels:  router
Ui Router
The de-facto solution to flexible routing with nested views in AngularJS
Stars: ✭ 13,738 (+493.69%)
Mutual labels:  router
Localize Router
An implementation of routes localisation for Angular
Stars: ✭ 177 (-92.35%)
Mutual labels:  router
Route
Simple isomorphic router
Stars: ✭ 199 (-91.4%)
Mutual labels:  router
React Live Route
📌 An enhanced react-router-v4/5 Route that keeps route alive.
Stars: ✭ 207 (-91.05%)
Mutual labels:  router
Visualizer
UI-Router state visualizer and transition visualizer
Stars: ✭ 205 (-91.14%)
Mutual labels:  router
Rt N56u
Padavan
Stars: ✭ 2,776 (+19.97%)
Mutual labels:  router

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 文件来获得更多信息。

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].