Topics

#62: JLRoutes 🔗

Topics

JLRoutes is a library from Joel Levin that makes it very easy to manage "routes" for URL schemes in an app. It's common to need to "deep link" in to an app. Other apps can use these to open your app to a specific spot, or we can use them within our own app such as when the user opens a push notification. Let's take a look.

First we'll add a new URL Type for our app. In Xcode, we'll go to our project, then target settings, then to the Info tab, and finally to the URL Types section.

Neat, now URLs in the form of spaceships://something will open our app! Now let's add our first route.

These can added anywhere, but it's best to set them up early, when the app has first launched.

JLRoutes.addRoute("/spaceships/:id") { (params: [NSObject : AnyObject]!) -> Bool in
  guard let spaceshipID = params["id"] as? String else { return false }

  let vc = SpaceshipViewController()      
  vc.spaceshipID = spaceshipID

  self.currentNavigationController().pushViewController(vc, animated: true)

  return true
}

A few things to note here. First notice the :id portion of the route path. This lets us easily extract out portions of the route from the params dictionary.

Also note how we use guard and return false if we can't handle the URL for some reason.

In addition, JLRoutes has support for wildcard matching as well as route namespaces.

More info about JLRoutes can be found at git.io/routes