• Stars
    star
    275
  • Rank 144,314 (Top 3 %)
  • Language
    Swift
  • License
    MIT License
  • Created over 8 years ago
  • Updated over 4 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A URL Router for iOS, written in Swift

SwiftRouter

License MIT Travis-CI Carthage compatible

A URL Router for iOS, written in Swift, inspired by HHRouter and 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. 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 {
    @objc var userId:String?
    @objc var username:String?
    @objc var password:String?
}

Map URL to ViewController:

import SwiftRouter

let router = Router.shared
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

More Repositories

1

what-happens-when-zh_CN

What-happens-when 的中文翻译,原仓库 https://github.com/alex/what-happens-when
7,700
star
2

Heart-First-JavaWeb

一个走心的 Java Web 入门开发教程
Shell
857
star
3

SimpleDNS

A lightweight yet useful proxy DNS server powered by Python Twisted
Python
190
star
4

pyfm

[DEPRECATED]使用Python编写的豆瓣FM(douban.fm)命令行播放器
Python
152
star
5

baidu-tieba-auto-sign

[DEPRECATED]模拟客户端进行百度贴吧批量签到的Python脚本
Python
74
star
6

learnrx-zh-cn

ReactiveX 官方互动式教程中文翻译,原仓库 https://github.com/ReactiveX/learnrx
JavaScript
54
star
7

Deck

A tiny Github client powered by React Native
JavaScript
45
star
8

Xcode-Search

A Xcode Source Editor Extension that searches external sources(Google, StackOverflow, etc)
Swift
41
star
9

MVVM-DataController-Demo

Objective-C
29
star
10

bebusy.py

Python Terminal log generator for work spoofing (inspired by https://github.com/ondrek/bebusy.js)
Python
19
star
11

Genie

The Simplest Static Blog Generator You'll Ever Seen
CSS
12
star
12

Hackintosh-MSI-GE63-Raider-RGB-8RE

12
star
13

AMA

抖音来的朋友们,这里冒个泡吧
11
star
14

Sunshine-React-Native

Sunshine app powered by React Native
JavaScript
7
star
15

Take-A-Rest

A tiny reminder that will notify you when you need a rest.
Python
5
star
16

leetcode

C++
4
star
17

Pad

A sample Markdown live editor
Swift
4
star
18

JLAlertView

An UIAlertView replacement with a modern chainable API, written in Swift 4.2
Swift
3
star
19

hitlib2

Tiny Python library servering as a set of APIs for HIT Online Library
Python
3
star
20

Annie

A sample Markdown parser written in Swift
HTML
2
star
21

unpv13e

Unix Network Programming Source Code
C
2
star
22

MyCloud

A mini personal cloud storage and sharing solution
JavaScript
2
star
23

noterm

Batchfile
2
star
24

Windows-Loading-View-iOS

A Simple View mimicking the Loading Animation You see on Windows 8 +
Objective-C
2
star
25

Honeymoon

A tiny Swift web framework, written in Swift 2.0
Swift
1
star
26

FDTemplateLayoutCell-Frame-Example

Objective-C
1
star
27

my-linux-toy

1
star
28

Md5-in-CSharp

MD5 Implementation in C#
C#
1
star
29

the-little-mongodb-book-pdf-epub

The Little Mongodb Book PDF and epub
1
star
30

DDP_Benchmark

Python
1
star
31

dg

dg -- directory generator
Python
1
star
32

Feeder

A Qt-Based Feedly RSS client using The Feedly Cloud API (developer.feedly.com)
C++
1
star
33

react-highlighter

A sample react app for online syntax highlighting
JavaScript
1
star
34

mydotfiles

Some of my config files
Vim Script
1
star