简体   繁体   English

“AppDelegate”类型的值没有成员“window”

[英]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.我正在尝试使用 3D 触摸快速操作,我正在设置它复制 VEA 软件代码。 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? ):这是我收到错误的地方( 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

}

In iOS 13, Xcode 11, the sceneDelegate handles the functionality of UIScene and window.在 iOS 13、Xcode 11 中,sceneDelegate 处理 UIScene 和 window 的功能。 The window performs properly when used in the sceneDelegate.窗口在sceneDelegate 中使用时正常执行。

Xcode 12.0 Swift 5.0 Xcode 12.0 斯威夫特 5.0

At the moment you need to:目前你需要:

  1. Remove the “Application Scene Manifest” from info.plist file;从 info.plist 文件中删除“应用场景清单”;
  2. Remove scene delegate class;移除场景委托类;
  3. Remove scene related methods in AppDelegate class;移除 AppDelegate 类中的场景相关方法;
  4. If missing, add the property var window: UIWindow?如果丢失,添加属性var window: UIWindow? to your AppDelegate class.到您的 AppDelegate 类。

Add some logic in func application(_: UIApplication, didFinishLaunchingWithOptions _: [UIApplication.LaunchOptionsKey: Any]?) .func application(_: UIApplication, didFinishLaunchingWithOptions _: [UIApplication.LaunchOptionsKey: Any]?)添加一些逻辑。

Example of implementation when you need to support iOS 12 and 13:需要支持 iOS 12 和 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()
    }
}

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.复制窗口对象并将其放置在应用程序委托中,它将起作用。

if your project already has SceneDelegate file ,then you need to use it insead of AppDelegate , like this way :如果您的项目已经有SceneDelegate文件,那么您需要在AppDelegate使用它,就像这样:

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
}

Sometimes when you have an issue in AppDelegate it can be solved with a quick Product -> Clean.有时,当您在 AppDelegate 中遇到问题时,可以通过快速的 Product -> Clean 来解决。 Hope this helps.希望这可以帮助。

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..}在 SceneDelegate 的第一个函数func scene(...) 、 if 语句、 if let windowScene = scene as? UIWindowScene {..Add Below Code Here..}添加以下代码if let windowScene = scene as? UIWindowScene {..Add Below Code Here..} . if let windowScene = scene as? UIWindowScene {..Add Below Code Here..} .

Make sure to add import MediaPlayer at the top of the file.确保在文件顶部添加import MediaPlayer

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

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 :这对你来说很好用,但首先你必须确定 rootViewController 我们可以这样说:

        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
                }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM