- Notifications
You must be signed in to change notification settings - Fork26
🛣 Simple Navigation for iOS -
License
freshOS/Router-deprecated
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
This has been used extensively and after weighing up pros and cons, we now favour the use of native Flow Controllers or Coordinators as explainedhere.
The native route (pun intended) should always be favoured and we believe less dependencies is something we should always strive for.
Reason -Get Started -Installation
Becauseclassic App Navigation introducestight coupling between ViewControllers.Complex Apps navigation can look like agigantic spider web.
Besides the fact thatNavigation responsibility is split among ViewControllers, modifying a ViewController can cascade recompiles and produceslow compile times.
By using a Navigationenum to navigate we decouple ViewControllers between them. Aka they don't know each other anymore. So modifyingVCA won't triggerVCB to recompile anymore \o/
// navigationController?.pushViewController(AboutViewController(), animated: true)navigate(.about)
Navigation code is now encapsulated in aAppNavigation object.
- Decouples ViewControllers
- Makes navigation Testable
- Faster compile times
enumMyNavigation:Navigation{case aboutcase profile(Person)}
Swift enum can take params!Awesome for us because that's how we will pass data between ViewControllers :)
structMyAppNavigation:AppNavigation{func viewcontrollerForNavigation(navigation:Navigation)->UIViewController{iflet navigation= navigationas?MyNavigation{switch navigation{case.about:returnAboutViewController()case.profile(let p):returnProfileViewController(person: p)}}returnUIViewController()}func navigate(_ navigation:Navigation, from:UIViewController, to:UIViewController){ from.navigationController?.pushViewController(to, animated:true)}}
A cool thing is that the swift compiler will produce an error if a navigationcase is not handled ! Which would'nt be the case with string URLs by the way ;)
InAppDelegate.swift, before everything :
Router.default.setupAppNavigation(appNavigation:MyAppNavigation())
You can now call nagivations from you view controllers :
navigate(MyNavigation.about)
BridgeNavigation with your own enum type, hereMyNavigation so that we don't have to type our own.
extensionUIViewController{func navigate(_ navigation:MyNavigation){navigate(navigationasNavigation)}}
You can now write :
navigate(.about)
Another cool thing about decoupling navigation is that you can now extract traking code from view Controllers as well. You can be notified by the router whenever a navigation happened.
Router.default.didNavigate{ navigationin // Plug Analytics for instanceGoogleAnalitcs.trackPage(navigation)}
There is a nasty bug in Swift 3 compiler where the compiler rebuilds files even though they haven't changed.This is documented here :https://forums.developer.apple.com/thread/62737?tstart=0
Due to this bug, the compilation can go like this :
ChangeViewController1 ->Build
-> CompilesViewController1, referenced inMyAppNavigation soMyAppNavigation gets recompiled.MyAppNavigation is referenced inAppDelegate which gets recompiled which references ...App ->ViewController2 ->ViewController3 ->ViewControllerX you get the point.Before you know it the entire App gets rebuilt :/
A good this is that most of the app coupling usually comes from navigation. which Router decouples.
We can stop this nonsense until this gets fixed in a future release of Xcode.Router can help us manage this issue by injecting our AppNavigation implementation at runtime.
In yourAppDelegate.swift
// Inject your AppNavigation at runtime to avoid recompilation of AppDelegate :)Router.default.setupAppNavigation(appNavigation:appNavigationFromString("YourAppName.MyAppNavigation"))
And make sure yourAppNavigation implementation is now aclass that isRuntimeInjectable
classMyAppNavigation:RuntimeInjectable,AppNavigation{
github "freshOS/Router"Simply Copy and PasteRouter.swift files in your Xcode Project :)
Grab this repository and build the Framework target on the example project. Then Link against this framework.
Like the project? Offer coffee or support us with a monthly donation and help us continue our activities :)
Become a sponsor and get your logo on our README on Github with a link to your site :)
About
🛣 Simple Navigation for iOS -
Topics
Resources
License
Code of conduct
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.

