App delegate must implement the window property if it wants to use a main storyboard file

Viewed 2350

I've already tried all solutions I've been able to find in stackoverflow and none of them work.

This is my current AppDelegate.swift file, I think that I'm implementing the window property, some way or another:

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.
    window = UIWindow()
    window?.makeKeyAndVisible()
    let mainVC = UIViewController()
    window?.rootViewController = mainVC

    return true
}

// MARK: UISceneSession Lifecycle
@available(iOS 13.0, *)
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.
    window = UIWindow()
          window?.makeKeyAndVisible()
          let mainVC = UIViewController()
          window?.rootViewController = mainVC
    return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
}

@available(iOS 13.0, *)
func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) {
    // Called when the user discards a scene session.
    // If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions.
    // Use this method to release any resources that were specific to the discarded scenes, as they will not return.
    window = UIWindow()
          window?.makeKeyAndVisible()
          let mainVC = UIViewController()
          window?.rootViewController = mainVC
}

}

What can be happening so the warning still appears and my app keeps on showing a black screen?

4 Answers

I guide you step by step.

  1. 1st remove SceneDelegate file from the project.
  2. Add var window: UIWindow? to AppDelegate.
  3. Remove below func from AppDelegate.

    // MARK: UISceneSession Lifecycle

    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.
        return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
    }
    
    func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) {
        // Called when the user discards a scene session.
        // If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions.
        // Use this method to release any resources that were specific to the discarded scenes, as they will not return.
    }
    
  4. Remove Application Scene Manifest key from Info.plist file.


Extra

Add below delegate methods in AppDelegate file, below didFinishLaunchingWithOptions

    func applicationWillResignActive(_ application: UIApplication) {
        // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
        // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
    }

    func applicationDidEnterBackground(_ application: UIApplication) {
        // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
        // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    }

    func applicationWillEnterForeground(_ application: UIApplication) {
        // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
    }

    func applicationDidBecomeActive(_ application: UIApplication) {
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    }

    func applicationWillTerminate(_ application: UIApplication) {
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    }

I just added var window: UIWindow? into the AppDelegate class, the issue got fixed.

Incase you are developing an Objective C app and face this error, follow same steps as mentioned above with only change in step 1. Add @property (nonatomic, retain) IBOutlet UIWindow window; to AppDelegate.h

When you create a new project in Xcode 11.4 and above, you create a project directory structure with SceneDelegate files.

if you try to run a project you get an error.

Step 1 : Delete SceneDelegate.h & SceneDelegate.m

Step 2: In AppDelegate.h create window property.

@property (strong, nonatomic) UIWindow *window;

Step 3: In the main Storyboard set the storyboard id for ViewController.

enter image description here

and add a set window in didFinishLaunchingWithOptions method of appdelegate.

UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
UIViewController *vc = [sb instantiateViewControllerWithIdentifier:@"ViewController"];
vc.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;

self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.rootViewController = vc;
[self.window makeKeyAndVisible];

return YES;

For Swift: Use below code

@UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?

func application(_ application: UIApplication, 
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: 
  Any]?) -> Bool {
    // Override point for customization after application launch.
    return true
}

}

Note: Don't add window inside didFinishLaunchingWithOptions method for swift.

Related