A URL Router for iOS, written in Swift 3, inspired by JLRoutes.
iOS 8.0 or later.
SwiftRoutes is compatible with Carthage. Add it to your Cartfile
:
github "takecian/SwiftRoutes" "0.2.2"
github "takecian/SwiftRoutes" "0.1.3"
SwiftRoutes is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod "SwiftRoutes", "0.2.2"
pod "SwiftRoutes", "0.1.3"
- Register route(URL) and handler.
- Call
routeUrl
with URL. - 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/")!))
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")!))
This example shows how to perform UIApplicationShortcutItem
using SwiftRoutes.
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>
...
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
}
}
}
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)
}
takecian
SwiftRoutes is available under the MIT license. See the LICENSE file for more info.