ModuleRoute is a modular routing framework designed to simplify navigation within your Swift applications. Leveraging the Service Locator pattern, ModuleRoute enables clean, scalable, and maintainable routing architecture for iOS projects.
- Modular Architecture: Organize your app into distinct modules with clear separation of concerns.
- Service Locator Integration: Efficient dependency management using Service Locator.
- Middleware Support: Process routes through customizable middleware.
- Interceptors: Intercept and handle routes based on custom logic.
- Deep Linking: Handle URL schemes and universal links seamlessly.
- Flexible Navigation: Support for various navigation types including push, present, modal, replace, and custom transitions.
- Logging: Built-in logging for route processing.
ModuleRoute is distributed via Swift Package Manager (SPM).
- Open your project in Xcode.
- Go to
File
>Add Packages...
. - Enter the ModuleRoute repository URL:
https://github.com/GIKICoder/ModuleRoute
- Choose the version you want to install and add the package to your project.
First, initialize the ServiceLocator
and MRNavigator
in your AppDelegate or SceneDelegate.
import ModuleRoute
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
let serviceLocator = ServiceLocator()
var navigator: MRNavigator!
func application(_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
navigator = MRNavigator(serviceLocator: serviceLocator)
// Register middleware
navigator.addMiddleware(LoggingMiddleware())
// Register modules and routes
registerModules()
return true
}
private func registerModules() {
serviceLocator.register(MyModule.self, routes: [MyRoute.self]) {
MyModule()
}
}
}
Create modules conforming to MRModule
and define their supported routes.
import ModuleRoute
struct MyRoute: MRRoute {
static var name: String { "myRoute" }
var params: [String : Any] = [:]
var callback: ((Any?) -> Void)? = nil
}
class MyModule: MRModule {
static var supportedRoutes: [MRRoute.Type] = [MyRoute.self]
func handle(route: MRRoute) -> RouteResult {
switch route {
case is MyRoute:
let viewController = MyViewController()
return .navigator(viewController)
default:
return .none
}
}
}
Use MRNavigator
to navigate to a specific route.
let route = MyRoute(params: ["key": "value"])
navigator.navigate(to: route, from: currentViewController, navigationType: .push, animated: true)
Register deep link handlers to handle incoming URLs.
navigator.registerDeepLinkHandler(scheme: "myapp") { url in
guard let route = MRRoute.from(url: url) else { return nil }
return route
}
// Handle incoming URL
func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
return navigator.handleDeepLink(url)
}
Customize route processing with middleware and interceptors.
// Middleware example
class AuthenticationMiddleware: MRMiddleware {
func process(route: MRRoute, navigator: MRNavigator, next: @escaping (MRRoute) -> RouteResult) -> RouteResult {
if !isAuthenticated {
// Handle unauthorized access
return .handler {
// Show login screen or alert
}
}
return next(route)
}
}
// Interceptor example
class LoggingInterceptor: MRInterceptor {
func shouldIntercept(route: MRRoute) -> Bool {
// Define when to intercept
return true
}
func handleInterception(route: MRRoute) -> RouteResult {
// Handle the interception
print("Route \(type(of: route)) intercepted")
return .none
}
}
// Adding Middleware and Interceptors
navigator.addMiddleware(AuthenticationMiddleware())
navigator.addInterceptor(LoggingInterceptor())
Contributions are welcome! Please follow these steps:
- Fork the repository.
- Create your feature branch:
git checkout -b feature/YourFeature
. - Commit your changes:
git commit -m 'Add some feature'
. - Push to the branch:
git push origin feature/YourFeature
. - Open a pull request.
Please ensure your code follows the project's coding standards and includes appropriate tests.
ModuleRoute is released under the MIT License.
Feel free to reach out or open an issue if you have any questions or need assistance.