let connectedScenes = UIApplication.shared.connectedScenes let windowScene = connectedScenes.first(where: { $0.activationState == .foregroundActive }) as? UIWindowScene let window = windowScene?.keyWindow
This worked well for me.