Latest 1.0.8
Homepage https://github.com/skyline75489/SwiftRouter
License MIT
Platforms ios 8.0
Authors

SwiftRouter

Swift 2.2
License MIT
Travis-CI
Carthage compatible

A URL Router for iOS, written in Swift 2.2, inspired by HHRouter and JLRoutes.

Installation

Carthage

SwiftRouter is compatible with Carthage. Add it to your Cartfile:

github "skyline75489/SwiftRouter"

CocoaPods

pod 'JLSwiftRouter'

use_frameworks!

Manually

Add SwiftRouter.swift in your project.

Usage

Routing ViewController

Define properties in your custom ViewController:

class UserViewController: UIViewController {
    var userId:String?
    var username:String?
    var password:String?
}

Map URL to ViewController:

import SwiftRouter

let router = Router.sharedInstance
router.map("/user/:userId", controllerClass: UserViewController.self)

Get instance of ViewController directly from the URL. Parameters will be parsed automatically:

let vc = router.matchController("/user/1?username=hello&password=123")!
XCTAssertEqual(vc.userId, "1")
XCTAssertEqual(vc.username, "hello")
XCTAssertEqual(vc.password, "123")

This will load controller using init() method. If you want to load view controller from storyboard – use:

let vc = router.matchControllerFromStoryboard("/user/1?username=hello&password=123", 
                                              storyboardName: "MyStoryboard")!

This code will load controller from storyboard named MyStoryboard.storyboard. Just don’t forget to set that controller identifier in storyboard to its class name. In this case UserViewController.

Push custom ViewController:

router.routeURL("/user/123", navigationController: self.navigationController!)
// The custom ViewController will be pushed with parameters.

Routing handler

Define your custom handler function and map it to URL:

router.map("/user/add", handler: { (params:[String: String]?) -> (Bool) in
    XCTAssertNotNil(params)
    if let params = params {
        XCTAssertEqual(params["username"], "hello")
        XCTAssertEqual(params["password"], "123")
    }
    return true
})

Call the handler from router:

router.routeURL("/user/add?username=hello&password=123") 
// The handler function will be called with parameters.

License

MIT License

Latest podspec

{
    "name": "JLSwiftRouter",
    "version": "1.0.8",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    },
    "summary": "A URL Router for iOS, written in Swift 2.2",
    "homepage": "https://github.com/skyline75489/SwiftRouter",
    "authors": {
        "Chester Liu": "[email protected]"
    },
    "source": {
        "git": "https://github.com/skyline75489/SwiftRouter.git",
        "tag": "1.0.8"
    },
    "source_files": "Source/*",
    "platforms": {
        "ios": "8.0"
    }
}

Pin It on Pinterest

Share This