DatePickerDialog-iOS-Swift icon indicating copy to clipboard operation
DatePickerDialog-iOS-Swift copied to clipboard

fails at appDelegate.window when window is in scene delegate

Open sisoje opened this issue 5 years ago • 6 comments

sisoje avatar Oct 05 '19 22:10 sisoje

crash on

guard let appDelegate = UIApplication.shared.delegate else { fatalError() } guard let window = appDelegate.window else { fatalError() }

can fix by

var windowOption = UIApplication.shared.delegate?.window if windowOption == nil { windowOption = UIApplication.shared.keyWindow } guard let window = windowOption else { fatalError() }

Xfcmy avatar Nov 17 '19 04:11 Xfcmy

not working :(

thelastsummer avatar Feb 13 '20 15:02 thelastsummer

if #available(iOS 13.0, *) {
            for scene in UIApplication.shared.connectedScenes {
                if scene.activationState == .foregroundActive {
                    guard let window = ((scene as? UIWindowScene)!.delegate as! UIWindowSceneDelegate).window else { fatalError() }
                    window?.addSubview(self)
                    window?.bringSubviewToFront(self)
                    window?.endEditing(true)
                    break
                }
            }
        } else {
            guard let appDelegate = UIApplication.shared.delegate else { fatalError() }
            guard let window = appDelegate.window else { fatalError() }
            window?.addSubview(self)
            window?.bringSubviewToFront(self)
            window?.endEditing(true)
        }

I added this code and It is working for me

thelastsummer avatar Feb 13 '20 16:02 thelastsummer

if #available(iOS 13.0, *) {
            for scene in UIApplication.shared.connectedScenes {
                if scene.activationState == .foregroundActive {
                    guard let window = ((scene as? UIWindowScene)!.delegate as! UIWindowSceneDelegate).window else { fatalError() }
                    window?.addSubview(self)
                    window?.bringSubviewToFront(self)
                    window?.endEditing(true)
                    break
                }
            }
        } else {
            guard let appDelegate = UIApplication.shared.delegate else { fatalError() }
            guard let window = appDelegate.window else { fatalError() }
            window?.addSubview(self)
            window?.bringSubviewToFront(self)
            window?.endEditing(true)
        }

I added this code and It is working for me

This worked! Thanks. Would love if the maintainers fix this in the source. I'll try to send a pull request next week when I have some time.

bobbyflowstate avatar Feb 18 '20 20:02 bobbyflowstate

not working in my case

Dubey-Shivani avatar Mar 11 '20 08:03 Dubey-Shivani

This is because in iOS 13, App Delegate does not have the variable "window".

You must create it within AppDelegate, Then, let's go to SceneDelegate, At the end of func scene (_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {

and set the "Window" variable of "AppDelegate":

let appDel = (UIApplication.shared.delegate as! AppDelegate) appDel.window = window

boazjames avatar Apr 07 '20 10:04 boazjames