11

I had created a few projects in SwiftUI that were perfectly fine, before I went on holiday around 2 weeks ago. When I got back, I updated Xcode and my iPhone.

The code for SwiftUI is not relevant as it was all working perfectly fine before. All I am getting on multiple projects tested is just a plain black screen.

What could be causing all my projects just to show a black screen, when they worked before updating Xcode and my device?

Versions:

My device - 13.0 beta 4

Simulators don't work - not sure on versions

Xcode-beta - 11.0 beta 4 (11M374r)

Community
  • 1
  • 1
George
  • 25,988
  • 10
  • 79
  • 133

5 Answers5

25

In SceneDelegate.swift replace

let window = UIWindow(frame: UIScreen.main.bounds)
window.rootViewController = UIHostingController(rootView: ContentView())
self.window = window
window.makeKeyAndVisible()

with

if let windowScene = scene as? UIWindowScene {
    let window = UIWindow(windowScene: windowScene)
    window.rootViewController = UIHostingController(rootView: ContentView())
    self.window = window
    window.makeKeyAndVisible()
}

If you had a .environtmentObject attached to ContentView() originally, don't forget to add that onto the ContentView() in the above code.

When you create a new project in Xcode beta 4, the second code block I posted is what is automatically generated in SceneDelegate.swift. The first code block I posted is what was automatically generated in all versions prior to beta 4. As you can see in the second block, the window is now initialized with the scene provided by the SceneDelegate function scene(scene:, session:, connectionOptions:) instead of a CGRect (UIScreen.main.bounds).

RPatel99
  • 7,448
  • 2
  • 37
  • 45
  • “The first code block I posted is what was automatically generated in all versions prior to beta 4” That would explain the black screen, but I’ve never seen that code in a SwiftUI app template in any version of Xcode. – matt Jul 25 '19 at 20:18
  • Accidentally had a test environment object in the first block and just now edited it out, but that first block of code is what was automatically generated in all my projects from pre-beta 4, and all of them result in a black screen until I replace it with the second block. @matt – RPatel99 Jul 25 '19 at 20:20
3

Also if the advice above didn't work, make sure that you have the correct pointer to SceneDelegate in your Info.plist.

Info.plist

  • This one solves my problem. I was coming from creating a new project on the XCode 13 so I had to manually add the SceneDelegate myself. – Seto Oct 20 '21 at 20:18
0

Try re-installing Xcode and see this link to migrate from Xcode beta 3 to Xcode beta 4

Christophe Prat
  • 188
  • 1
  • 2
  • 18
0

I had this problem too after going from beta 3 to 4. I created a new project in beta 4 and copied everything over from the old project and it worked.

dcvonb
  • 315
  • 3
  • 10
0

In your info plist replace this key value -

<key>UIApplicationSceneManifest</key>
<dict>
    <key>UIApplicationSupportsMultipleScenes</key>
    <false/>
    <key>UISceneConfigurations</key>
    <dict>
        <key>UIWindowSceneSessionRoleApplication</key>
        <array>
            <dict>
                <key>UISceneConfigurationName</key>
                <string>Default Configuration</string>
                <key>UISceneDelegateClassName</key>
                <string>$(PRODUCT_MODULE_NAME).SceneDelegate</string>
            </dict>
        </array>
    </dict>
</dict>

The SceneDelegate will looks like below --

import UIKit
import SwiftUI

class SceneDelegate: UIResponder, UIWindowSceneDelegate {

    var window: UIWindow?

    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
        if let windowScene = scene as? UIWindowScene {
            let window = UIWindow(windowScene: windowScene)
            window.rootViewController = UIHostingController(rootView: ContentView())
            self.window = window
            window.makeKeyAndVisible()
        }
    }
}

The AppDelegate will be -

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {}
Jack
  • 13,571
  • 6
  • 76
  • 98