はじめに
UIWindowをSwiftUIで使う際に落とし穴があるので記事にしておきます。
悪い例
let scene = UIApplication.shared.connectedScenes.first as? UIWindowScene
良い例
let scene = UIApplication.shared.connectedScenes.first(where: { $0.activationState == .foregroundActive }) as? UIWindowScene
悪い例を使うとどうなるか
iPadでクラッシュする
おわり
何が正解かわかりませんが、
もしかしたら、SceneDelegateから取得する方法が正しい方法かもしれません