55![ Bitbucket open issues] ( https://img.shields.io/bitbucket/issues/wxlpp/UIRouter )
66
77UIRouter 是一个用Swift实现的路由解耦框架.
8-
8+ [ API文档 ] ( https://wxlpp.github.io/UIRouter/ )
99## 安装
1010
1111### CocoaPods
@@ -18,7 +18,79 @@ pod 'UIRouter', '~> 0.1.0.alpha'
1818
1919``` swift
2020dependencies: [
21- .package (url : " https://github.com/Alamofire/Alamofire.git" , .upToNextMajor (from : " 5.2 .0" ))
21+ .package (url : " https://github.com/Alamofire/Alamofire.git" , .upToNextMajor (from : " 0.1 .0" ))
2222]
2323```
2424## 使用
25+
26+ ### 页面注册
27+
28+ ``` swift
29+ import UIKit
30+ import UIRouter
31+
32+ @main
33+ class AppDelegate : UIResponder , UIApplicationDelegate {
34+
35+ func application (_ application : UIApplication, didFinishLaunchingWithOptions launchOptions : [UIApplication.LaunchOptionsKey: Any ]? ) -> Bool {
36+ // 可以在此调用 autoRegisterIfNeed 进行页面预注册,否则会在第一次页面路由发生时进行注册
37+ application.router .autoRegisterIfNeed ()
38+ // 注册拦截器,WebInterceptor 是一个默认实现的 htttp 协议拦截器
39+ application.router .register (interceptors : [WebInterceptor ()])
40+ // 注册错误处理器进行错误处理
41+ application.router .registerErrorHandler (RouteErrorHandler ())
42+ return true
43+ }
44+ }
45+
46+ ```
47+
48+ ``` swift
49+ import UIKit
50+ import UIRouter
51+
52+ public final class UserProfileViewController : UIViewController {}
53+
54+ // MARK: 路由
55+
56+ extension UserProfileViewController : Routable {
57+ public static func route (parameters : RouterParameters, completion : @escaping RouteCompletionHandler<UserProfileViewController>) {
58+ // 对参数进行验证,成功返回 ViewController,失败则返回错误,
59+ // 这里返回的是参数验证错误,也可以返回业务自定义错误。
60+ if let userID: String = parameters.get (" name" ) {
61+ completion (.success (UserProfileViewController ()))
62+ } else {
63+ completion (.failure (RouteError.parameterValidationFailed (url : path, name : " name" )))
64+ }
65+ }
66+
67+ public static var path: String {
68+ " user/profile/:id"
69+ }
70+ }
71+ ```
72+ ### 页面路由
73+ ``` swift
74+ UIApplication.shared .route (url : " user/profile/123456?name=wxlpp" ).push ()
75+ UIApplication.shared .route (url : " https://github.com/wxlpp/UIRouter" ).present ()
76+ UIApplication.shared .route (url : " flutter://shop.com/home" ).presentWithNavigationController (UINavigationController.self )
77+ ```
78+ ### 错误拦截
79+ ``` swift
80+ import UIRouter
81+
82+ class RouteErrorHandler : RouteErrorHandling {
83+ func handleRouteError (_ error : RouteError) {
84+ #if DEBUG
85+ let vc = ErrorDetailsViewController (error : error)
86+ UIApplication.shared .route (viewcontroller : vc).presentWithNavigationController (UINavigationController.self )
87+ #else
88+ debugPrint (error.errorDescription )
89+ #endif
90+ }
91+
92+ func handleCustomError (_ error : Error ) {
93+ // 这里对业务错误信息进行处理,比如用户鉴权失败弹出登录页面
94+ }
95+ }
96+ ```
0 commit comments