When writing code manually, we could inject dependencies to rootViewController by constructor injection in AppDelegate:
UIViewController *vc = [[SomeViewController alloc] initWithDependency: dependency];
self.window.rootViewController = vc;
However, I can not find a way to inject dependencies when using Storyboard. It seems inappropriate to inject them in awakeFromNib
or viewDidLoad
etc.
Is that possible to inject them ?
In your AppDelegate test the following code:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
let dependency = MyDependency()
if let firstVC = window?.rootViewController as? MyViewControllerClass {
firstVC.dependency = dependency
}
return true
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With