macOS Catalyst configurationForConnecting UISceneSession delegate function not always called on app launch

642 views Asked by At

I'm building a macOS Catalyst app with support for multiple windows, which is implemented with the new UISceneDelegate set of APIs introduced in iOS 13.

According to Apple's documentation, as a new window is created, a scene delegate needs to connect to UISceneSession, which allows passing information to this session via its userInfo property. One way to set userInfo before the scene delegate is connected is in this function of UISceneDelegate:

  func application(
    _ application: UIApplication,
    configurationForConnecting connectingSceneSession: UISceneSession,
    options: UIScene.ConnectionOptions
  ) -> UISceneConfiguration {
    // Called when a new scene session is being created.
    // Use this method to select a configuration to create the new scene with.
    connectingSceneSession.userInfo?["foo"] = "bar"

    return UISceneConfiguration(
      name: "Default Configuration", 
      sessionRole: connectingSceneSession.role
    )
  }

Looking at the Developer Reference page for this function, it seems it should be always called on app launch before the main app's scene is set up. Unfortunately, it seems with macOS Catalyst this is not true, but I'm not able to find any documentation or logic for why this function is not always called. In my application, which reuses trivial sample code for UISceneDelegate, this function is called randomly about 50% of the time on app launch. This is not great, as it means that randomly 50% of the time app's windows are not properly set up, as required information is not passed in the userInfo property of UISceneSession.

What is the exact logic for calls to application(_:configurationForConnecting:options:) or how to enforce that it is called deterministically on app launch for the first app's scene with macOS Catalyst?

2

There are 2 answers

0
Adrian On

This isn't specific to Catalyst - it happens in iOS too. It's to do with window restoration versus creation. To understand that, see my answer to this more generic question: Why is UIApplicationDelegate method `application(_:configurationForConnecting:options:)` not called reliably

0
Koshub On

It could not be called even in iOS, not only Mac Catalyst

Once you have configured UISceneSession with particular persistentIdentifier UIKit will not call application(_:configurationForConnecting:options:) for the session with this persistentIdentifier again.

UIKit calls this method shortly before creating a new scene