Value of type 'AppDelegate' has no member 'window'

19,820

Solution 1

Xcode 12.0 Swift 5.0

At the moment you need to:

  1. Remove the “Application Scene Manifest” from info.plist file;
  2. Remove scene delegate class;
  3. Remove scene related methods in AppDelegate class;
  4. If missing, add the property var window: UIWindow? to your AppDelegate class.

Add some logic in func application(_: UIApplication, didFinishLaunchingWithOptions _: [UIApplication.LaunchOptionsKey: Any]?).

Example of implementation when you need to support iOS 12 and 13:

class AppDelegate: UIResponder, UIApplicationDelegate {
    var window: UIWindow?

    func application(_: UIApplication, didFinishLaunchingWithOptions _: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        configureInitialViewController()
        return true
    }

    private func configureInitialViewController() {
        let initialViewController: UIViewController
        let storyboard = UIStoryboard(name: "Main", bundle: nil)

        window = UIWindow()

        if (condition) {
            let mainViewController = storyboard.instantiateViewController(withIdentifier: "mainForm")
            initialViewController = mainViewController
        } else {
            let loginViewController = storyboard.instantiateViewController(withIdentifier: "loginForm")
            initialViewController = loginViewController
        }
        window?.rootViewController = initialViewController
        window?.makeKeyAndVisible()
    }
}

Solution 2

In iOS 13, Xcode 11, the sceneDelegate handles the functionality of UIScene and window. The window performs properly when used in the sceneDelegate.

Solution 3

When you are using Scene view then windows object is in the scene delegate. Copy window object and place it in the app delegate and it will work.

Solution 4

if your project already has SceneDelegate file ,then you need to use it insead of AppDelegate , like this way :

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
    // Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
    // If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
    // This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).
    guard let _ = (scene as? UIWindowScene) else { return }
    
        
    
    let mainStoryBoard = UIStoryboard(name: "Main", bundle: nil)
    let viewController = mainStoryBoard.instantiateViewController(withIdentifier: "setCountry")
    window?.rootViewController = viewController
}

Solution 5

Sometimes when you have an issue in AppDelegate it can be solved with a quick Product -> Clean. Hope this helps.

Share:
19,820
Ben77006
Author by

Ben77006

Updated on July 21, 2022

Comments

  • Ben77006
    Ben77006 almost 2 years

    Im trying to use the 3D touch Quick Actions and I'm setting it up copying VEA Software code. In his sample code it works perfectly but when I try to add it to my app I get some unusual errors. I am new to coding and swift so please explain as much as possible. Thanks. Below I have the code which is in my app delegate.

    This is where I'm getting the error (self.window?):

    @available(iOS 9.0, *)
    func handleShortcutItem(shortcutItem: UIApplicationShortcutItem) -> Bool
    {
    var handled = false
    var window: UIWindow?
    
    guard ShortcutIdentifier(fullType: shortcutItem.type) != nil else { return false }
    guard let shortcutType = shortcutItem.type as String? else { return false }
    
    switch (shortcutType)
    {
    case ShortcutIdentifier.First.type:
        handled = true
        var window = UIWindow?()
    
        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let navVC = storyboard.instantiateViewControllerWithIdentifier("ProjectPage") as! UINavigationController
        // Error on line below
        self.window?.rootViewController?.presentViewController(navVC, animated: true, completion: nil)
    
    
        break
    case ShortcutIdentifier.Second.type:
        handled = true
    
        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let navVC = storyboard.instantiateViewControllerWithIdentifier("ContactPageView") as! UINavigationController
        // Error on line below
        self.window?.rootViewController?.presentViewController(navVC, animated: true, completion: nil)
        break
    
    case ShortcutIdentifier.Third.type:
        handled = true
    
        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let navVC = storyboard.instantiateViewControllerWithIdentifier("ViewController") as! UINavigationController
        // Error on line below
        self.window?.rootViewController?.presentViewController(navVC, animated: true, completion: nil)
        break
    
    
    default:
        break
    }
    
    return handled
    
    }
    
  • Hashim Akhtar
    Hashim Akhtar over 3 years
    I found this to be the best answer.
  • dphans
    dphans about 3 years
    I should to login to stackoverflow to upvote for you, thanks!
  • Jagdish
    Jagdish almost 3 years
    Thank you this helped. This works with SceneDelegate