Hello. Recently, our app has been experiencing crashes with the message 'Attempting to attach window to an invalidated scene' when creating a UIWindow.
Our code stores the UIWindowScene provided in the scene(:willConnectTo:options:) function in a global variable and does not change the set scene until the scene(:willConnectTo:options:) function is called again. Additionally, we do not perform any actions related to the disconnect event.
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
guard let windowScene = (scene as? UIWindowScene) else { return }
hudManager.setup(windowScene: windowScene)
// ...
}
func sceneDidDisconnect(_ scene: UIScene) {
// do nothing
}
In all crash logs, the activationState of the WindowScene is "unattached", so I initially thought that creating a UIWindow with a scene in the 'unattached' state should be avoided.
However, in the scene(_:willConnectTo:options:) function, the scene's state is also 'unattached', yet the UIWindow is created successfully here, which makes me think that deciding whether to create a window based on the activationState is incorrect.
I did find that trying to create a UIWindow with a scene after it has been disconnected causes a crash.
func sceneDidDisconnect(_ scene: UIScene) {
// Crash occur here and scene's state is `unattached`
let window = UIWindow(windowScene: scene as! UIWindowScene)
}
If the activationState alone cannot be used to determine the validity of a scene, is there another way to check the validity of a Scene? Thank you