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

元气小坏坏 提交于 2021-01-26 19:09:56

问题


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

}

回答1:


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




回答2:


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.




回答3:


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()
    }
}



回答4:


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




回答5:


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
}



回答6:


This worked for me.

Add the following code inside SceneDelegate's first function's, func scene(...), if statement, if let windowScene = scene as? UIWindowScene {..Add Below Code Here..}.

Make sure to add import MediaPlayer at the top of the file.

let volumeView = MPVolumeView()
volumeView.alpha = 0.000001
window.addSubview(volumeView)

At the end your code should be:

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
    

    // Create the SwiftUI view that provides the window contents.
    let contentView = ContentView()

    // Use a UIHostingController as window root view controller.
    if let windowScene = scene as? UIWindowScene {
        let window = UIWindow(windowScene: windowScene)
        window.rootViewController = UIHostingController(rootView: contentView)
        self.window = window
        window.makeKeyAndVisible()

        // MARK:    Hide Volume HUD View
        let volumeView = MPVolumeView()
        volumeView.alpha = 0.000001
        window.addSubview(volumeView)
    }
}



回答7:


first you need to specify which shortcut you need to use , cuz there are two types of shortcuts : dynamic and static and let say that you choose the static that can be done and added from the property list info then you have to handle the actions for each shortcut in the app delegate file in your project

func application(_ application: UIApplication, performActionFor shortcutItem: UIApplicationShortcutItem, completionHandler: @escaping (Bool) -> Void) {

    let storyboard = UIStoryboard(name: "Main", bundle: Bundle.main)
    let mainTabController = storyboard.instantiateViewController(withIdentifier: "Tab_Bar") as? Tab_Bar
    let loginViewController = storyboard.instantiateViewController(withIdentifier: "LoginVC") as? LoginVC
    let doctor = storyboard.instantiateViewController(withIdentifier: "DrTabController") as! DrTabController
    getData(key: "token") { (token) in
        getData(key: "type") { (type) in
            if token != "" && type != "" , type == "2" {
                self.window?.rootViewController = mainTabController
                if #available(iOS 13.0, *) {
                    self.window?.overrideUserInterfaceStyle = .light
                } else {
                    self.window?.makeKeyAndVisible()
                }
                if shortcutItem.type == "appointMent" {
                    mainTabController?.selectedIndex = 1
                } else if shortcutItem.type == "Chatting" {
                    mainTabController?.selectedIndex = 2
                }
            } else if token != "" && type != "" , type == "1"{
                self.window?.rootViewController = doctor
                if #available(iOS 13.0, *) {
                    self.window?.overrideUserInterfaceStyle = .light
                } else {
                    self.window?.makeKeyAndVisible()
                }
                if shortcutItem.type == "appointMent" {
                    mainTabController?.selectedIndex = 1
                } else if shortcutItem.type == "Chatting" {
                    mainTabController?.selectedIndex = 2
                }
            } else if token == "" && type == "" {
                self.window?.rootViewController = loginViewController
                if #available(iOS 13.0, *) {
                    self.window?.overrideUserInterfaceStyle = .light
                } else {
                    self.window?.makeKeyAndVisible()
                }
                if shortcutItem.type == "appointMent" {
                    mainTabController?.selectedIndex = 1
                } else if shortcutItem.type == "Chatting" {
                    mainTabController?.selectedIndex = 2
                }
            }
        }
    }



}

this will be work so fine for you but first you have to determine the rootViewController we can say like :

        let storyboard = UIStoryboard(name: "Main", bundle: Bundle.main)

  self.window?.rootViewController = mainTabController
                if #available(iOS 13.0, *) {
                    self.window?.overrideUserInterfaceStyle = .light
                } else {
                    self.window?.makeKeyAndVisible()
                }
                if shortcutItem.type == "appointMent" {
                    mainTabController?.selectedIndex = 1
                } else if shortcutItem.type == "Chatting" {
                    mainTabController?.selectedIndex = 2
                }


来源:https://stackoverflow.com/questions/33714671/value-of-type-appdelegate-has-no-member-window

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!