DatePickerDialog-iOS-Swift
DatePickerDialog-iOS-Swift copied to clipboard
fails at appDelegate.window when window is in scene delegate
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() }
not working :(
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
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.
not working in my case
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