VisionOS app with UIKit : error with window visibility

I am running into an exception whenever I try to open the app's window in visionOS simulator. The exception error message says:

"Thread 1: "Error in UIKit client: window visibility must match its layer visibility!""

This is the same code that runs just fine on iOS where I setup the app's window and tab bar controller in code:

- (BOOL)application:(UIApplication *)application willFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    // Setup the UI - Override point for customization after app launch
    UIWindowScene *firstScene = (UIWindowScene *) [[[[UIApplication sharedApplication] connectedScenes] allObjects] firstObject];
    self.mainWindow = [[UIWindow alloc] initWithWindowScene:firstScene];
    self.mainWindow.backgroundColor = [UIColor systemBackgroundColor];
    
    self.tabController = [[UITabBarController alloc] init];
    [self.tabController definesPresentationContext];
    
    self.mainWindow.rootViewController = self.tabController;
    [self.mainWindow makeKeyAndVisible];
    // setup the tab bar later

The exception comes from the makeKeyAndVisible line. I'm not sure what I need to do differently for a visionOS app to make it work.

I'm using Xcode 15.1 Beta 2.

Are you sure you're getting a UIWindowScene here (that is, firstScene is non-nil)?

This isn't how we would expect you to connect your first window when using scenes – we would instead expect you to create your first window in your scene delegate's -scene:willConnectToSession:options: method instead.

I would suspect that your app is simply getting launched (and getting -application:willFinishLaunchingWithOptions:) before your first scene has been connected.

Thanks. The firstScene object isn't nil, until it's windows or keyWindow properties are both nil. Could that be the problem?

This is what the object looks like in the debugger:

<UIWindowScene: 0x106b0ef40; role: UIWindowSceneSessionRoleApplication; activationState: UISceneActivationStateForegroundActive> {
    session = <UISceneSession: 0x600001793200; persistentIdentifier: com.myapp.name> {
        configuration = <UISceneConfiguration: 0x600001794c40; name: 0x0>;
    };
    delegate = (nil);
    screen = <UIScreen: 0x10711b1a0; bounds: {{0, 0}, {1366, 1024}}; mode: <UIScreenMode: 0x600000268bc0; size = 2732.000000 x 2048.000000>>;
}

I never adopted the UIScenes and UIWindowsScene APIs from iOS13+, and I run my iOS app as a single-window app. Would I have to change that to be able to run my app as a native VisionOS app?

This is still not resolved with Xcode 15.1 Beta3. I created a I created FB13325773 with a sample project, so it's a problem for 'new' projects as well.

The issue still persists with Xcode 15.2 Beta 1. Just to reiterate: I created FB13325773 with a sample project

After much time wastage, it turns out that the problem is caused by setting the window's backgroundColor (for some reason). So it works fine after commenting out this line:

self.mainWindow.backgroundColor = [UIColor systemBackgroundColor];

VisionOS app with UIKit : error with window visibility
 
 
Q