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

后端 未结 10 1692
臣服心动
臣服心动 2020-12-07 17:27

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 w

相关标签:
10条回答
  • 2020-12-07 18:06

    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.

    0 讨论(0)
  • 2020-12-07 18:12

    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
        }
    
    0 讨论(0)
  • 2020-12-07 18:17

    Just in case anyone comes across this again and is programming in Objective-C make sure you have this line of code in your AppDelegate.h file:

    @property (strong, nonatomic) UIWindow *window;
    
    0 讨论(0)
  • 2020-12-07 18:18

    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.
        }
    }
    
    0 讨论(0)
  • 2020-12-07 18:22

    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
        }
    
    }
    
    0 讨论(0)
  • 2020-12-07 18:22

    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.

    0 讨论(0)
提交回复
热议问题