Skip to content

Latest commit

 

History

History
139 lines (94 loc) · 5.4 KB

README.md

File metadata and controls

139 lines (94 loc) · 5.4 KB

Routes 🗺

Build Status CocoaPods Compatible Carthage Compatible

This library is a Swift port/fork of the popular Objective-C library JLRoutes. Much ❤️ and credit goes to joeldev for creating such a delightful routing library.

Routes is a pure-Swift 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

Carthage (recommended)

Add Routes to your Cartfile:

github "min/Routes"

CocoaPods

Add Routes to your Podfile:

pod 'Routes'

Requirements

  • iOS 9.0+ / tvOS 9.0+ / watchOS 2.0+
  • Swift 4.2

Getting Started

Configure your URL schemes in Info.plist.

class AppDelegate: UIResponder, UIApplicationDelegate {
    let router: Router = Router()
    
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        router.default.add(pattern: "/user/view/:user_id") { parameters in
            let userId = parameters["user_id"]
            
            // present UI for viewing user with userId
        }
        return true
    }
    
    func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
        return router.route(to: url)
    }
}

After adding a route for /user/view/:user_id, the following call will cause the handler block to be called with a dictionary containing {"user_id": "min"}:

router.route(to: "myapp://user/view/min")

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 false, Routes 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 true.

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 true.

Schemes

Routes supports setting up routes within a specific URL scheme. Routes that are set up within a scheme can only be matched by URLs that use a matching URL scheme. By default, all routes go into the global scheme.

let router = Router()

router.default["home"] = { parameters in
    // This block is called if the scheme is not 'alpha' or 'beta' (see below)	
    return true
}

router.alpha["home"] = { parameters in
    // This block is called for alpha://home
    return true
}

router.beta["home"] = { parameters in
    // This block is called for beta://home
    return true
}

This example shows that you can declare the same routes in different schemes and handle them with different callbacks on a per-scheme basis.

Continuing with this example, if you were to add the following route:

router.default["/global"] = { parameters in
    return true
}

and then try to route the URL alpha://global, it would not match because that route has not been declared within the alpha scheme but has instead been declared within the global scheme (which we'll assume is how the developer wants it). However, you can easily change this behavior by setting the following property to true:

router.alpha.shouldFallback = true

This tells Routes that if a URL cannot be routed within the alpha scheme (aka, it starts with alpha: but no appropriate route can be found), try to recover by looking for a matching route in the global routes scheme as well. After setting that property to true, the URL alpha://global would be routed to the /global handler block.

Wildcards

Routes supports setting up routes that will match an arbitrary number of path components at the end of the routed URL. An array containing the additional path components will be added to the parameters dictionary with the key Definition.Keys.wildcard.

For example, the following route would be triggered for any URL that started with /wildcard/, but would be rejected by the handler if the next component wasn't joker.

router.default.add(pattern: "/wildcard/*") { parameters in
    let components = parameters[Definition.Keys.wildcard]

    guard let component = components.first, component == "joker" else {
        return false
    }

    return true
}

Optional Routes

Routes supports setting up routes with optional parameters. At the route registration moment, Routes will register multiple routes with all combinations of the route with the optional parameters and without the optional parameters. For example, for the route /the(/foo/:a)(/bar/:b), it will register the following routes:

  • /the/foo/:a/bar/:b
  • /the/foo/:a
  • /the/bar/:b
  • /the

License

MIT. See the LICENSE file for details.