JLRoutes 是一种基于块回调的先进 URL 解析技术。它旨在简化您的应用程序中对复杂 URL 方案的处理,而无需进行任何类型的 URL 或字符串解析。
有关如何在您的应用程序 Info.plist 中注册自定义 URL 方案的更多信息。
JLRoutes 可以通过 CocoaPods 进行安装。
// in your app delegate:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// ...
[JLRoutes addRoute:@"/user/view/:userID" handler:^BOOL(NSDictionary *parameters) {
NSString *userID = parameters[@"userID"]; // defined in the route by specifying ":userID"
// present UI for viewing user with ID 'userID'
return YES; // return YES to say we have handled the route
}];
// ...
return YES;
}
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
return [JLRoutes routeURL:url];
}
设置好路由之后,任何时刻(包括另一个应用程序)都可以调用此内容来触发处理程序块
NSURL *viewUserURL = [NSURL URLWithString:@"myapp://user/view/joeldev"];
[[UIApplication sharedApplication] openURL:viewUserURL];
在这个例子中,传递到块参数字典中的 userID 对象将具有键值对 "userID": "joeldev"
,然后可以使用该值呈现 UI 或执行其他任何需要的操作。
参数字典始终至少包含以下三个键
{
"JLRouteURL": "(the NSURL that caused this block to be fired)",
"JLRoutePattern": "(the actual route pattern string)",
"JLRouteNamespace": "(the route namespace, defaults to JLRoutesGlobalNamespace)"
}
JLRouteNamespace 键指的是匹配路由所在的命名空间。 有关命名空间了解更多信息。
这些键在 JLRoutes.h 中定义为常量,以便于使用。
static NSString *const kJLRoutePatternKey = @"JLRoutePattern";
static NSString *const kJLRouteURLKey = @"JLRouteURL";
static NSString *const kJLRouteNamespaceKey = @"JLRouteNamespace";
如您可能已注意到,处理器块应该返回一个布尔值以表示是否处理了路由。如果块返回 NO
,JLRoutes 将会假定该路由不匹配,并继续寻找匹配项。只有当模式字符串匹配 并且 块返回 YES
时,才认为该路由是匹配的。
请注意,如果您为处理器块传递nil,系统将创建一个内部处理器块,该块仅返回 YES
。
[JLRoutes addRoute:@"/:object/:action/:primaryKey" handler:^BOOL(NSDictionary *parameters) {
NSString *object = parameters[@"object"];
NSString *action = parameters[@"action"];
NSString *primaryKey = parameters[@"primaryKey"];
// stuff
return YES;
}];
此路由将会匹配如 /user/view/joeldev
或 /post/edit/123
之类的URL。假设您使用一些URL参数调用了 /post/edit/123
NSURL *editPost = [NSURL URLWithString:@"myapp://post/edit/123?debug=true&foo=bar"];
[[UIApplication sharedApplication] openURL:editPost];
处理器块接收的参数字典将包含以下键值对
{
"object": "post",
"action": "edit",
"primaryKey": "123",
"debug": "true",
"foo": "bar",
"JLRouteURL": "myapp://post/edit/123?debug=true&foo=bar",
"JLRoutePattern": "/:object/:action/:primaryKey",
"JLRouteNamespace": "JLRoutesGlobalNamespace"
}
JLRoutes 支持在给定的URL方案命名空间内设置路由。在单个方案命名空间内设置的路由只能由使用相同方案的URL匹配。默认情况下,所有路由都进入全局方案。当前的 +addRoute 方法将使用此方案,且没有任何不同的功能。
但是,如果您决定确实需要处理具有不同功能集的多个方案,以下是如何操作的示例
[JLRoutes addRoute:@"/foo" handler:^BOOL(NSDictionary *parameters) {
// This block is called if the scheme is not 'thing' or 'stuff' (see below)
return YES;
}];
[[JLRoutes routesForScheme:@"thing"] addRoute:@"/foo" handler:^BOOL(NSDictionary *parameters) {
// This block is called for thing://foo
return YES;
}];
[[JLRoutes routesForScheme:@"stuff"] addRoute:@"/foo" handler:^BOOL(NSDictionary *parameters) {
// This block is called for stuff://foo
return YES;
}];
此示例显示您可以在不同的方案中声明相同的路由,并且可以根据每个方案的不同回调函数来处理它们。
继续这个例子,如果您向上面集合中添加以下路由
[JLRoutes addRoute:@"/global" handler:^BOOL(NSDictionary *parameters) {
return YES;
}];
然后尝试路由URL thing://global
,它不会匹配,因为该路由没有在 thing
命名空间中声明,而是在全局命名空间中声明(我们假设这是开发者希望的方式)。但是,您可以轻松地通过设置以下属性为 YES
来改变这种行为
[JLRoutes routesForScheme:@"thing"].shouldFallbackToGlobalRoutes = YES;
这告诉JLRoutes,如果无法在 thing
命名空间(即,以 thing:
开头但找不到适当路由)中进行路由,则尝试在全局路由命名空间中查找匹配的路由。设置此属性为 YES
后,URL 'thing://global' 会被路由到 /global 块。
JLRoutes 支持设置路由以匹配路由URL末尾的任意数量的路径组件。包含附加路径组件的数组将以 kJLRouteWildcardComponentsKey
键添加到参数字典中。
例如,以下路由将触发放以 /wildcard/
开头的任何URL,但如果下一个组件不是 joker
,则处理器会拒绝该路由。
[JLRoutes addRoute:@"/wildcard/*" handler:^BOOL(NSDictionary *parameters) {
NSArray *pathComponents = parameters[kJLRouteWildcardComponentsKey];
if ([pathComponents count] > 0 && [pathComponents[0] isEqualToString:@"joker"]) {
// the route matched; do stuff
return YES;
}
// not interested unless the joker's in it
return NO;
}];
JLRoutes 支持设置带可选参数的路由。在路由注册时,JLRoute 将注册带所有组合的带有可选参数和不带可选参数的路由。例如,对于路由 /user/:userId(/post/:postId)(/reply/:replyId)
,它将注册以下路由
/user/:userId/post/:postId/reply/:replyId
/user/:userId/post/:postId/
/user/:userId
BSD 3-Clause License
版权所有 © 2015, Joel Levin。保留所有权利。
在以下条件下,允许以源代码或二进制形式重新分发和使用,是否修改,包括但不限于以下条件
- 源代码的重新分发必须保留上述版权声明、本许可列表和以下免责声明。
- 以二进制形式重新分发时,必须在随附的文档或其他材料中 reproduction the above copyright notice、this list of conditions and the following disclaimer with the distribution.
- 未经事先书面许可,不得使用 JLRoutes 或其贡献者的名称来推广或证明从本软件派生出的产品。
本软件按“原样”提供,版权所有者和贡献者放弃所有明示或暗示的保证,包括但不限于适销性保证和针对特定目的的适用性保证。在任何情况下,无论因何种原因(包括但不限于直接损失、间接损失、偶发损失、特别损失、示范性损失或后果性损失)或在任何责任理论下(包括合同责任、严格责任或侵权责任),包括过失或非过失),无论是否事先警告过此类损害的可能性,都不应对所有此类损失或损害负任何责任。