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
@UIApplicationMainclassAppDelegate:UIResponder,UIApplicationDelegate{letserviceLocator=ServiceLocator()varnavigator: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()returntrue}privatefunc registerModules(){
serviceLocator.register(MyModule.self, routes:[MyRoute.self]){MyModule()}}}Create modules conforming to MRModule and define their supported routes.
import ModuleRoute
structMyRoute:MRRoute{staticvarname:String{"myRoute"}varparams:[String:Any]=[:]varcallback:((Any?)->Void)?=nil}classMyModule:MRModule{staticvarsupportedRoutes:[MRRoute.Type]=[MyRoute.self]func handle(route:MRRoute)->RouteResult{switch route {case is MyRoute:letviewController=MyViewController()return.navigator(viewController)default:return.none
}}}Use MRNavigator to navigate to a specific route.
letroute=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 inguardlet route =MRRoute.from(url: url)else{returnnil}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
classAuthenticationMiddleware:MRMiddleware{func process(route:MRRoute, navigator:MRNavigator, next:@escaping(MRRoute)->RouteResult)->RouteResult{if !isAuthenticated {
// Handle unauthorized access
return.handler {
// Show login screen or alert
}}returnnext(route)}}
// Interceptor example
classLoggingInterceptor:MRInterceptor{func shouldIntercept(route:MRRoute)->Bool{
// Define when to intercept
returntrue}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.