Latest 0.2.2
Homepage https://github.com/takecian/SwiftRoutes
License MIT
Platforms ios 8.0
Authors

CI Status
Version
License
Platform
Carthage compatible

A URL Router for iOS, written in Swift 3, inspired by JLRoutes.

Requirements

iOS 8.0 or later.

Installation

Carthage

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

Swift 3

github "takecian/SwiftRoutes" "0.2.2"

Swift 2.2

github "takecian/SwiftRoutes" "0.1.3"

Cocoapods

SwiftRoutes is available through CocoaPods. To install
it, simply add the following line to your Podfile:

Swift 3

pod "SwiftRoutes", "0.2.2"

Swift 2.2

pod "SwiftRoutes", "0.1.3"

Usage

  1. Register route(URL) and handler.
  2. Call routeUrl with URL.
  3. SwiftRoutes fire a handler that matched.
SwiftRoutes.addRoute(URL(string: "http://yourdomain.com/users/")!) { (params) -> Bool in
    let viewController = SomeViewController()
    navigationController.pushViewController(viewController, animated: true)
    return true
}

SwiftRoutes.routeUrl(URL(string: "http://yourdomain.com/users/")!))

The Parameters Dictionary

SwiftRoutes provides parameters as Dictionary from following data.

  • a part of url (Developer can specify name wit prefix :)
  • Query String
SwiftRoutes.addRoute(URL(string: "http://yourdomain.com/users/:userid")!) { (params) -> Bool in
    let viewController = SomeViewController()
    let userId = params["userid"]   <- userId is "1234"
    let name = params["name"]   <- name is "testname"
    viewController.userid = userId
    navigationController.pushViewController(viewController, animated: true)
    return true
}

SwiftRoutes.routeUrl(URL(string: "http://yourdomain.com/users/1234?name=testname")!))

Practical example – Use SwiftRoutes for UIApplicationShortcutItem

This example shows how to perform UIApplicationShortcutItem using SwiftRoutes.

1. Define UIApplicationShortcutItem in info.plist

Here, two UIApplicationShortcutItems are defined. ShortcutItemType has url to be handled.

    ...
    <key>UIApplicationShortcutItems</key>
    <array>
        <dict>
            <key>UIApplicationShortcutItemIconFile</key>
            <string>Home</string>
            <key>UIApplicationShortcutItemTitle</key>
            <string>Home</string>
            <key>UIApplicationShortcutItemType</key>
            <string>/home</string>
            <key>UIApplicationShortcutItemUserInfo</key>
            <dict>
                <key>key1</key>
                <string>value1</string>
            </dict>
        </dict>
        <dict>
            <key>UIApplicationShortcutItemIconFile</key>
            <string>Setting</string>
            <key>UIApplicationShortcutItemTitle</key>
            <string>Setting</string>
            <key>UIApplicationShortcutItemType</key>
            <string>/setting</string>
            <key>UIApplicationShortcutItemUserInfo</key>
            <dict>
                <key>key1</key>
                <string>value1</string>
            </dict>
        </dict>
    </array>
    ...

2. Define Route Handler

Define route and handler at app launches.

“`AppDelegate.swift
class AppDelegate: UIResponder, UIApplicationDelegate {
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

    // Define routing
    SwiftRoutes.addRoute(URL(string: "/home")!) { (params) -> Bool in
        self.window.rootViewController.moveToHome()  // pseudo code
        return true
    }

    SwiftRoutes.addRoute(URL(string: "/setting")!) { (params) -> Bool in
        self.window.rootViewController.moveSetting() // pseudo code
        return true
    }
}

}


## 3. Handle routes

Just put `SwiftRoutes.routeUrl(_)` in `application(application:performActionForShortcutItem:completionHandler)` and pass return value of `SwiftRoutes.routeUrl(_)` into `completionHandler(_)`.

@available(iOS 9.0, *)
func application(application: UIApplication, performActionForShortcutItem shortcutItem: UIApplicationShortcutItem, completionHandler: (Bool) -> Void) {
let val = SwiftRoutes.routeUrl(URL(string: shortcutItem.type)!)
completionHandler(val)
}



## Author

takecian

## License

SwiftRoutes is available under the MIT license. See the LICENSE file for more info.

Latest podspec

{
    "name": "SwiftRoutes",
    "version": "0.2.2",
    "summary": "Simple routing library writtein in Swift.",
    "description": "SwiftRoutes is a simple routing library. SwiftRoutes fires handler according to NSURL.",
    "homepage": "https://github.com/takecian/SwiftRoutes",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    },
    "authors": {
        "takecian": "[email protected]"
    },
    "source": {
        "git": "https://github.com/takecian/SwiftRoutes.git",
        "tag": "0.2.2"
    },
    "platforms": {
        "ios": "8.0"
    },
    "source_files": "Sources/*",
    "pushed_with_swift_version": "3.0"
}

Pin It on Pinterest

Share This