Interfaces of Swift extension not called if put in separate target
Asked Answered
O

0

0

I have a SceneDelegate class which is part of Target 1 code. Target 1 is compiled as a static library.

public class SceneDelegate: UIResponder, UIWindowSceneDelegate {

    public var window: UIWindow?

    // SceneDidBecomeActive
    // other lifecycle events
}

The SceneDelegate is programmatically assigned in the 'configurationForConnecting' of the AppDelegate in Target 1. i.e.

config = UISceneConfiguration(name: nil, sessionRole: connectingSceneSession.role)
                                    
// Set the scene delegate
config.delegateClass = SceneDelegate.self

Now, I do 'extension SceneDelegate' in Target 2. In this extension, I have put the 'willConnectTo' function definition. Target 2 is also compiled as a static library.

extension SceneDelegate {

    public func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {

        // ...

    }

    // Also has sceneDidDisconnect
}

The App Target has Target 1 and Target 2 added under 'Link Binary with Libraries'.

Now when the application runs, willConnectTo is never invoked, which was in Target 2. The other code in Target 2 is invoked properly. However the other lifecycle states like SceneDidBecomeActive, sceneDidResignActive etc from Target 1 gets invoked.

Is there any limitation as such when using extensions in this manner ?

Otisotitis answered 10/11, 2022 at 13:15 Comment(2)
If you said config.delegateClass = SceneDelegate.self for target 1, then that is where the scene delegate lifecycle events will be sent. The willConnectTo is one of those so it needs to be in target 1.Newmodel
@Newmodel : Thanks ! But, Target 2 has the extension of the class. I agree that willConnectTo is on of the lifecycle events but was trying to keep it in a different target for some specific usecases. It is the same class and should the remaining interfaces in the extension not get called no matter which target they are in ? Not completely sure how it works hence the doubt.Otisotitis

© 2022 - 2024 — McMap. All rights reserved.