Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

}
like image 260
Ben77006 Avatar asked Nov 14 '15 23:11

Ben77006


People also ask

What is a AppDelegate class?

A set of methods to manage shared behaviors for your app.

Why did Apple introduce the scene delegate?

Use your UISceneDelegate object to manage life-cycle events in one instance of your app's user interface. This interface defines methods for responding to state transitions that affect the scene, including when the scene enters the foreground and becomes active, and when it enters the background.


2 Answers

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()
    }
}
like image 150
Kostarev Kirill Avatar answered Sep 17 '22 15:09

Kostarev Kirill


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

like image 38
Amitha Avatar answered Sep 18 '22 15:09

Amitha