squimer / DatePickerDialog-iOS-Swift

Date picker dialog for iOS
MIT License
580 stars 177 forks source link

fails at appDelegate.window when window is in scene delegate #100

Open sisoje opened 4 years ago

Xfcmy commented 4 years ago

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

thelastsummer commented 4 years ago

not working :(

thelastsummer commented 4 years ago
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

rajuashok commented 4 years ago
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.

Dubey-Shivani commented 4 years ago

not working in my case

boazjames commented 4 years ago

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