Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/skyline75489/SwiftRouter
A URL Router for iOS, written in Swift
https://github.com/skyline75489/SwiftRouter
Last synced: about 2 months ago
JSON representation
A URL Router for iOS, written in Swift
- Host: GitHub
- URL: https://github.com/skyline75489/SwiftRouter
- Owner: skyline75489
- License: mit
- Created: 2015-09-24T10:29:06.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2019-09-21T02:14:27.000Z (over 5 years ago)
- Last Synced: 2024-12-03T10:44:49.908Z (about 2 months ago)
- Language: Swift
- Homepage:
- Size: 74.2 KB
- Stars: 278
- Watchers: 11
- Forks: 27
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-ios - SwiftRouter - A URL Router for iOS. (App Routing)
- awesome-swift - SwiftRouter - A URL Router for iOS. (Libs / App Routing)
- awesome-swift - SwiftRouter - A URL Router for iOS. (Libs / App Routing)
- fucking-awesome-swift - SwiftRouter - A URL Router for iOS. (Libs / App Routing)
- awesome-ios-star - SwiftRouter - A URL Router for iOS. (App Routing)
- awesome-swift-cn - SwiftRouter - A URL Router for iOS written in Swift 2.0 (Libs / Utility)
- fucking-awesome-ios - SwiftRouter - A URL Router for iOS. (App Routing)
- awesome-swift - SwiftRouter - A URL Router for iOS, written in Swift ` 📝 2 years ago ` (App Routing [🔝](#readme))
README
SwiftRouter
===========[![License MIT](https://img.shields.io/badge/License-MIT-blue.svg?style=flat)](https://github.com/skyline75489/SwiftRouter/blob/master/LICENSE)
[![Travis-CI](https://travis-ci.org/skyline75489/SwiftRouter.svg?branch=master)](https://travis-ci.org/skyline75489/SwiftRouter)
[![Carthage compatible](https://img.shields.io/badge/Carthage-compatible-4BC51D.svg?style=flat)](https://github.com/Carthage/Carthage)A URL Router for iOS, written in Swift, inspired by [HHRouter](https://github.com/Huohua/HHRouter) and [JLRoutes](https://github.com/joeldev/JLRoutes).
## Installation
| SwiftRouter Version | Swift Version | Note |
|:------------------:|:--------------------:|:-----|
| Before 1.0.7 | 2.0 | n/a |
| 1.0.7 | 2.2 | n/a |
| 2.0.0 | 3.0 | n/a |
| 2.1.0 | 3.0 | Breaking changes by adopting exception |
| 3.0.0 | 4.0 | @objc should be used with properties |### Carthage
SwiftRouter is compatible with [Carthage](https://github.com/Carthage/Carthage). Add it to your `Cartfile`:
github "skyline75489/SwiftRouter"
### CocoaPods
```ruby
pod 'JLSwiftRouter'use_frameworks!
```### Manually
Add `SwiftRouter.swift` in your project.
## Usage
### Routing ViewControllerDefine properties in your custom ViewController:
```swift
class UserViewController: UIViewController {
@objc var userId:String?
@objc var username:String?
@objc var password:String?
}
```Map URL to ViewController:
```swift
import SwiftRouterlet router = Router.shared
router.map("/user/:userId", controllerClass: UserViewController.self)
```Get instance of ViewController directly from the URL. Parameters will be parsed automatically:
```swift
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:
```swift
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:
```swift
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:
```swift
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:
```swift
router.routeURL("/user/add?username=hello&password=123")
// The handler function will be called with parameters.
```## License
[MIT License](https://github.com/skyline75489/SwiftRouter/blob/master/LICENSE)