RxRouting
RxRouting 提供了一种反应式处理深层链接的方式。
感谢 devxoul,他的 URLNavigator 库(部分代码)
安装
CocoaPods
pod 'RxRouting'
Carthage
github 'e-sites/RxRouting'
入门
// Handles an URL that is openened from an external application (e.g. Safari).
func handle(url: URL) -> Bool
// Registers a url pattern
func register(_ url: String) -> Observable<RouteMatchResult>
使用方法
AppDelegate.swift
为了处理打开您应用的链接,将URL
传递给RxRouting
,以便它可以分发给订阅的观察者。
import RxRouting
func application(_ app: UIApplication,
open url: URL,
options: [UIApplication.OpenURLOptionsKey: Any] = [:]) -> Bool {
if RxRouting.instance.handle(url: url) {
return true
}
return false
}
注册
监听传入的URL,注册您的模式
RxRouting.instance
.register("rxroutingexample://user/<userid:int>"
.subscribe(onNext: { result in
// result is a RouteMatchResult
}.disposed(by: disposeBag)
可用的模式
模式 | Swift类型 | 模式 | 示例 |
---|---|---|---|
<value> |
字符串 |
scheme://users/gender/<gender> |
scheme://users/gender/male |
<value:int> |
整型 |
scheme://user/<userid:int> |
scheme://user/5123 |
<value:float> |
浮点型 |
scheme://temperatures/<temp:float> |
scheme://temperatures/31.5 |
<value:bool> |
布尔型 |
scheme://users/active/<available:bool> |
scheme://users/active/true |
<value:uuid> |
UUID |
scheme://users/<userid:uuid> |
scheme://users/f3383db8-9a6d-494c-b1af-2438148204c0 |
<value:date> |
日期 |
scheme://news/<newsdate:date> |
scheme://news/2019-06-22 |