iosswiftdependency-injectionswinject

Swinject container registration fails


While trying to bind some protocols to various classes I have encountered a strange behaviour of getting nil in the protocols. This is the dependency injector class:

class DependencyInjector: NSObject {
    
    let container: Container
    
    override init() {
        container = Container()
        super.init()
        register()
    }
}

extension DependencyInjector {
    
    func register() {
        container.register(NetworkProtocols.self) { _ in ApiManager() }.inObjectScope(.container)
        container.register(ConnectivityManager.self) { r in
            let controller = ConnectivityManager()
            controller.network = r.resolve(NetworkProtocols.self)
            return controller
        }
        
        container.storyboardInitCompleted(ChooseCameraViewController.self) {r,c in
            c.network = r.resolve(NetworkProtocols.self)
        }
        
        container.storyboardInitCompleted(InstallationViewController.self) {r,c in
            c.network = r.resolve(NetworkProtocols.self)
        }
        
        container.storyboardInitCompleted(CameraSetupViewController.self) {r,c in
            c.network = r.resolve(NetworkProtocols.self)
        }
        
        container.storyboardInitCompleted(LoginViewController.self) { r,c in
            c.network = r.resolve(NetworkProtocols.self)
        }
    }
} 

This is the AppDelegate:

var dependencyInjector: DependencyInjector!

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.        
    dependencyInjector = DependencyInjector()
    
    let window = UIWindow(frame: UIScreen.main.bounds)
    self.window = window
    let bundle = Bundle(for: ViewController.self)
    let storyboard = SwinjectStoryboard.create(name: "Main", bundle: bundle, container: dependencyInjector.container)
    window.rootViewController = storyboard.instantiateInitialViewController()
    
    return true
}

In the class below I get nil in the protocol property:

class ConnectivityManager: NSObject {
    
    var network: NetworkProtocols!
    
    func connectHotSpot() {
        self.network.whoAmI(success: {
            print("api succses")
        }, failure: { (error) in
            print(error.localizedDescription)
        })
    }
}

The network var is always nil while in the other classes that use storyboardInitCompleted the network var works.

What am I doing wrong?


Solution

  • As @JakubVano said I had to obtain an instance of ConnectivityManager:

        container.register(ConnectivityManager.self) { r in
            let controller = ConnectivityManager()
            controller.network = r.resolve(NetworkProtocols.self)
            return controller
        }
    
       container.storyboardInitCompleted(ChooseCameraViewController.self) {r,c in
        c.connectivityManager = r.resolve(ConnectivityManager.self)!
    
        }