joeldev /JLRoutes
URL routing library for iOS with a simple block-based API
★ 5,702⑂ 686👁 132⑃ 54● 73▤ 20
+0
Daily
5,702
stars
686
forks
132
watchers
54
prs
73
issues
20
releases
README
JLRoutes
========
What is it?
JLRoutes is a URL routing library with a simple block-based API. It is designed to make it very easy to handle complex URL schemes in your application with minimal code.
Installation
JLRoutes is available for installation using CocoaPods or Carthage (add github "joeldev/JLRoutes" to your Cartfile).
Requirements
JLRoutes 2.x require iOS 8.0+ or macOS 10.10+. If you need to support iOS 7 or macOS 10.9, please use version 1.6.4 (which is the last 1.x release).
Documentation
Documentation is available here.
Getting Started
Configure your URL schemes in Info.plist.
objc
- (BOOL)application:(UIApplication )application didFinishLaunchingWithOptions:(NSDictionary )launchOptions
{
JLRoutes routes = [JLRoutes globalRoutes];
[routes 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 )app openURL:(NSURL )url options:(NSDictionary )options
{
return [JLRoutes routeURL:url];
}
Routes can also be registered with subscripting syntax:
objc
JLRoutes.globalRoutes[@"/user/view/:userID"] = ^BOOL(NSDictionary parameters) {
// ...
};
After adding a route for /user/view/:userID, the following call will cause the handler block to be called with a dictionary containing @"userID": @"joeldev":
objc
NSURL viewUserURL = [NSURL URLWithString:@"myapp://user/view/joeldev"];
[JLRoutes routeURL:viewUserURL];
The Parameters Dictionary
The parameters dictionary always contains at least the following three keys:
json
{
"JLRouteURL": "(the NSURL that caused this block to be fired)",
"JLRoutePattern": "(the actual route pattern string)",
"JLRouteScheme": "(the route scheme, defaults to JLRoutesGlobalRoutesScheme)"
}
The JLRouteScheme key refers to the scheme that the matched route lives in. Read more about schemes.
See JLRoutes.h for the list of constants.
Handler Block Chaining
The handler block is expected to return a boolean for if it has handled the route or not. If the block returns NO, JLRoutes will behave as if that route is not a match and it will continue looking for a match. A route is considered to be a match if the pattern string matches and the block returns YES.
It is also important to note that if you pass nil for the handler block, an internal handler block will be created that simply returns YES.
Global Configuration
There are multiple global configuration options available to help customize JLRoutes behavior for a particular use-case. All options only take affect for the next operation.
objc
/// Configures verbose logging. Defaults to NO.
+ (void)setVerboseLoggingEnabled:(BOOL)loggingEnabled;
/// Configures if '+' should be replaced with spaces in parsed values. Defaults to YES.
+ (void)setShouTop issues
#125Apple Privacy Manifest3 comments · 2023-11-27#118Thanks to the author, I have translated this repo to Swift 51 comments · 2019-07-14#117How do I call back between two controllers?1 comments · 2019-05-10#128CocoaPods0 comments · 2024-12-24#122Ambivalent behaviour of wildcard pattern0 comments · 2020-05-16#121Version 2.1 is no longer compatible with Carthage0 comments · 2020-04-09#114How do I jump to the deeper level controller0 comments · 2018-12-07#112pass object0 comments · 2018-09-27
Latest release
JLRoutes 2.1.1 2.1.1 2021-08-10Support for Swift Package Manager