简体   繁体   中英

Swift Storyboard app launches to a black screen

Hi everyone,

I'm building a Swift app using Storyboards. I've been working fine for a few months now, but all of a sudden my app won't load properly. Whenever I open it on a Simulator or my physical iPhone, the launch screen is displayed before a black screen appears.

My Mac is on macOS Big Sur Developer Beta 5 with Xcode 12 Beta 6, and my iPhone is on iOS 14 Developer Beta 5.

This happened all of a sudden and I don't recall doing anything to cause it.

Here's what I've tried so far...

  • Renaming the storyboard and updating the target's General tab to the new name, as well as doing the same but manually editing Info.plist
  • Moving the storyboard in and out of "Copy Bundle Resources"
  • Updating to the latest Xcode 12 beta (I'm on macOS Big Sur)
  • Clearing Derived Data with DevCleaner
  • Starting a whole new project and moving all of my code and resources over via drag-and-drop (Interesting observation: when I started a new project, I added a simple label to the default Main.storyboard and ran it on my iPhone. The label wasn't displayed. )
  • Adding a function to my AppDelegate to load the storyboard manually on launch
  • Adding various print statements in AppDelegate and my Home View Controller

AppDelegate

I've added

let storyboard = UIStoryboard(name: "Main", bundle: nil)
let initialViewController = storyboard.instantiateViewController(withIdentifier: "Home")
self.window.rootViewController = initialViewController
self.window.makeKeyAndVisible()
print("App launched")

to my AppDelegate. Now, when I run my app, I get printed. I also added

override func viewDidLoad() {
    super.viewDidLoad()
    print("Home view loaded")

to my Home View Controller. Now, when I run my app, I get this printed in Xcode:

2020-08-28 13:11:20.140963+0100 MY-APP[11077:1951343] libMobileGestalt MobileGestaltCache.c:166: Cache loaded with 4536 pre-cached in CacheData and 53 items in CacheExtra.

2020-08-28 13:11:20.759943+0100 MY-APP[11077:1951162] Metal API Validation Enabled

Home view loaded

App launched

Still, nothing on my iPhone. The launch screen appears, fades to black, and that's it. I'm so confused.

If anyone knows how to fix this, or something I can try, please let me know. Thank you in advance!

Make sure there a window like this present in your sceneDelegate and AppDelegate both.

class AppDelegate: UIResponder, UIApplicationDelegate {
    
// check for this

    var window: UIWindow?

// check for this

    
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

    }

}

If you don't want to switch to the Scene API just yet, you can also just set UIApplicationSupportsMultipleScenes to NO in the UIApplicationSceneManifest section which probably recently appeared in your Info.plist. That's what I just did, and it fixed the issue for me.

In latest version, window property is no more available in AppDelegate. Now, it is moved to SceneDelegate.swift . You can try doing as below in func scene willConnectTo:

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
    guard let _ = (scene as? UIWindowScene) else { return }
    if let windowScene = scene as? UIWindowScene {
        self.window = UIWindow(windowScene: windowScene)
        let storyBoard = UIStoryboard(name: "Main", bundle: nil)
        let initialViewController = storyBoard.instantiateViewController(withIdentifier: "Home")
        self.window?.rootViewController = initialViewController
        self.window!.makeKeyAndVisible()
    }
}

Update: Also make your Main Interface under General menu is empty

在此处输入图片说明

Also you have to remove the <key>UIMainStoryboardFile</key><string>Main</string> and <key>UISceneStoryboardFile</key> <string>Main</string> from your Info.plist of your project.

I also faced the same issue. The original question says,
"This happened all of a sudden and I don't recall doing anything to cause it."
This is exactly what happened to me and following is how I solved the issue.

In Main.storyboard, there is an entry point to the app which is visually depicted as an arrow pointing to the Navigation Controller. Once the issue showed up, I noticed that this arrow is now somehow missing and Is Initial View Controller checkbox in the View Controller section of the Attribute Inspector tab in the Inspectors pane is now unchecked .

The issue was resolved when I checked the Is Initial View Controller checkbox mentioned above(The arrow in the storyboard navigation controller also reappeared upon checking this). Below is an image of xcode after solving the issue to help get a better understanding.
This is an image showing the Navigation Controller and Inspectors pane after the issue was fixed.


Hope someone finds this useful.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM