CocoaPods trunk is moving to be read-only. Read more on the blog, there are 12 months to go.
| TestsTested | ✓ |
| LangLanguage | SwiftSwift |
| License | MIT |
| ReleasedLast Release | Oct 2015 |
| SPMSupports SPM | ✗ |
Maintained by Ian Keen.
Once you have made your UIViewControllers conform to Routable you can register them with the parameters that they represent in your registered url-scheme routes. IKRouter is then able to create an array of UIViewControllers for you to display when a valid url is handled. All you need to do then is display them!
IKRouter can also handle routes in the traditional way by simply registering a route and handling it with a funciton/closure and the two methods can also be used together.
An example route is:
myapp://project/:projectId/item/:itemId
Note that routes must include the scheme (myapp://), url parameters need to be prefixed with a colon (:) and query strings dont need to be included when registering a route as they are included when the route is matched.
To use the Routables to automatically create your UI stack
UIViewController that can be linked to a route parameter conform to Routable.IKRouter instance.UIViewControllers via the routableHandler closure/function.The Routable protocol consists of a single simple method which when give a MatchedRoute returns an instance of the Routable.
protocol Routable {
static func instanceForRoute(route: MatchedRoute) -> Routable?
}MatchedRoute instances provide all the details needed to pass information through to Routables like matched parameters and their values as well as query string.
Once your UIViewControllers are Routable simply do the following:
let navController = UINavigationController()
let router = IKRouter()
router
.registerRoutableWithParameter(ProjectViewController.self, parameter: ":projectId")
.registerRoutableWithParameter(ItemViewController.self, parameter: ":itemId")
.registerRouteHandler("myapp://project/:projectId/item/:itemId")
.registerRouteHandler("myapp://project/:projectId")
.routableHandler = { match, viewControllers in
navController.setViewControllers(viewControllers, animated: true)
}Routable registered for all parametersRoutable the default handler will be used (if provided)handler parameter. This can be omitted when using Routables.If you have routes which might not suit the automatic functionality provided by Routables you can also register individual routes with their own handlers
let router = IKRouter()
router
.registerRouteHandler("myapp://project/:projectId/item/:itemId") { match in
//create view controllers and show here...
return true
}
.registerRouteHandler("myapp://project/:projectId/users/:userId") { match in
//create view controllers and show here...
return true /* return false if we didn't handle the route */
}NOTE: The handler for each route is used here (unlike above)
Every app has a slightly different UI hierarchy/architecture… for this reason IKRouter does not provide and automatic handling of UIViewController presentation but instead allows you to handle that yourself. Instead I have provided a UINavigationController extension that you can use to display the stack in different ways.
Currently there is just a simple method that will take a stack of UIViewControllers; push all but the last and present the last item in the stack like so:
router.routableHandler = { match, viewControllers in
navController.setViewControllersPresentingLast(viewControllers, animatedSet: true, animatedPresent: true)
}If there are other means of displaying a stack you think would be useful here feel free to add an issue or pull request, both are welcome!
or manually by adding the source files from the IKRouter subfolder to your project.
There is an included app so you can see it in action.