Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dependency injection #9

Closed
luca-i opened this issue Feb 18, 2017 · 4 comments
Closed

Dependency injection #9

luca-i opened this issue Feb 18, 2017 · 4 comments

Comments

@luca-i
Copy link

luca-i commented Feb 18, 2017

How should I do dependency injection?
Suppose I need to access an external service inside my login viewcontroller (the initial viewcontroller for loginStoryboard). The service is defined as a protocol.
I could use an implicitly unwrapped optional, but how could I set it?
Thank you, Luca-

@initFabian
Copy link
Collaborator

Hey @luca-i,

Been giving your issue some thought and am ready some more information. How are you currently instantiating your login viewcontroller? Should help us find a solution for your issue.

@luca-i
Copy link
Author

luca-i commented Feb 23, 2017

Thank you @initFabian,
since the initial view controller is instantiated with Cely.setup static method, I can't inject any dependencies in it (i'm talking about something like https://www.natashatherobot.com/ios-view-controller-data-injection-with-storyboards-and-segues-in-swift/).

@initFabian
Copy link
Collaborator

Hey @luca-i,

Since Cely simply references the rootViewController, you should still have access to the loginVC. For example, you can still run code like this inside of your AppDelegate.

class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        Cely.setup(with: window, forModel: User(), requiredProperties: [User.Property.token], withOptions: [
            .loginStoryboard: UIStoryboard(name: "Login", bundle: nil)
        ])

        if let loginVC = window?.rootViewController as? MyCustomLoginViewController {
            loginVC.dependency = ...
        }
...
}

@initFabian
Copy link
Collaborator

Going to close this issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants