Git Product home page Git Product logo

routes's Introduction

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.

routes's People

Contributors

min avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.