// Podfile:
pod 'GetRouter', '1.0.5'
// 终端中:
pod install --repo-update
提供一个无侵入的路由解析组件,本组件不涉及跳转操作,只用来解析路由,匹配路由,权限拦截. 文件查看顺序: GetRouter->GetRouterName->GetRouterHandler->GetRouterMiddleware
// 注册
func prepareRouter() {
GetRouter.config(nativeScheme: "native", routeNameKey: "ios_router")
GetRouter.registerHandler(GetRouterCommonHandler())
GetRouter.registerHandler(GetRouterTestHandler())
}
// 路由名
extension GetRouterName {
static let tabSelect: GetRouterName = "/common/tabSelect"
static let test: GetRouterName = "/common/test"
static let home1: GetRouterName = "/common/home1"
static let home2: GetRouterName = "/common/home2"
static let home3: GetRouterName = "/common/home3"
static let home4: GetRouterName = "/common/home4"
}
/// 公共
struct GetRouterCommonHandler: GetRouterHandlerSource {
var pages: [GetPage] = [
GetPage(name: .test,
middlewares: [
GetRouterMiddlewareVip(priority: 1),
],
action: { params in
TestHomePage().push()
}
),
]
}
// 同步
class GetRouterMiddlewareLogin: GetRouterMiddleware {
override func handler(routeName: GetRouterName, params: GetDict?) -> Bool {
let isLogin = Bool.random()
if !isLogin {
print("未登录")
}
return isLogin
}
}
// 异步
class GetRouterMiddlewareVip: GetRouterMiddleware {
override func handlerAsync(routeName: GetRouterName, params: GetDict?) async throws -> Bool {
try await withCheckedThrowingContinuation({ continuation in
DispatchQueue.global().asyncAfter(deadline: .now() + 2, execute: {
continuation.resume(returning: true)
})
})
}
}
GetRouter.to(name: .home1, params: ["userId": 2222])
GetRouter.to(url: "native://?ios_router=/common/home1&userId=2222")
suyikun, [email protected]
GetRouter is available under the MIT license. See the LICENSE file for more info.