简体   繁体   中英

The app delegate must implement the window property if it wants to use a main storyboard file swift

I have just developed an app, but when running in the simulator the debugger console says:

The app delegate must implement the window property if it wants to use a main storyboard file.

I have an app delegate file. What does the message mean, and how can I get my app working?

确保您的 AppDelegate 类中有以下属性声明:

var window: UIWindow?

If you run your project on earlier than iOS 13.0, in that case you will face the problem. Because of iOS 13 and later, app launch differently than earlier versions.

  • In iOS 13 and later, use UISceneDelegate objects to respond to life-cycle events in a scene-based app

  • In iOS 12 and earlier, use the UIApplicationDelegate object to respond to life-cycle events.

When you launch the app in iOS 12 and earlier then UIApplicationMain class expect a window property in your AppDelegate class as like SceneDelegate has. So your problem will be solved if you add the following line in your AppDelegate class.

var window: UIWindow?

For Objective-C

@property (strong, nonatomic) UIWindow *window;

You can find more here App's Life Cycle .

以防万一有人再次遇到这个问题并且正在使用 Objective-C 进行编程,请确保您的AppDelegate.h文件中有这行代码:

@property (strong, nonatomic) UIWindow *window;

I have received this error, when I created new project in XCode 11. I have not used SwiftUI . Here are the steps, I have considered to fix this.

  1. Deleted Application Scene Manifest entry from Info.plist
  2. Deleted SceneDelegate.swift file
  3. Deleted all scene related methods in AppDelegate.swift class
  4. added var window: UIWindow? property in AppDelegate.swift class

After these steps, I am able to run the app on version prior to iOS 13.

[EDIT]
Finally, your AppDelegate.swift file should look something like the following.

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.
        return true
    }

}

Add the following window declaration in Appdelegate file

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
    
    var window:UIWindow?
    ...

Implementation of this property is required if your app's Info.plist file contains the UIMainStoryboardFile key. The default value of this synthesized property is nil, which causes the app to create a generic UIWindow object and assign it to the property. If you want to provide a custom window for your app, you must implement the getter method of this property and use it to create and return your custom window.

I had the same issue, just add var window: UIWindow? as the debug error says.

@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
    }

You can check your app delegate class:

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.
        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.
        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.
    }
}

Swift 5 & Xcode 11

Make sure that SceneDelegate contains UIWindow property

class SceneDelegate: UIResponder, UIWindowSceneDelegate {

    var window: UIWindow?

    //...
}

Setting in Info.plist Application Scene Manifest > Enable Mutliple Windows > false. This solved the problem for me.

No, Xcode uses automatic assign if you've defined Main Interface file, in Application target > General tab > Deployment info > Main Interface field.

Just drag, textfield, for example on Main.storyboard.

In Xcode it's created and assigned in "Single View Application", by default.

Long ago answered, but to help understand the questions above about why simply adding the window property solves the problem, note that the app delegate conforms to the UIApplicationDelegate protocol which defines a property, @property (nullable, nonatomic, strong) UIWindow *window; that classes need to provide to specify the window to use when presenting a storyboard . Failure to provide that is causing the Xcode log warnings.

For Swift:

var window: UIWindow?

For Objective-C:

@property (strong, nonatomic) UIWindow *window;

In addition, you might have an Application Scene Manifest entry in your Info.plist file - if you do not use scenes, but only a window and view controllers (eg because you want to test something in the old UI setup), you should remove that entry to be able to see your views.

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