Coder Social home page Coder Social logo

swiftroutes's Introduction

SwiftRoutes

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.

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.

swiftroutes's People

Contributors

szweier avatar takecian avatar

Watchers

 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.