2
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

SceneDelegateのシングルトンオブジェクトを取得する

Posted at

UIApplicationが接続されたSceneを保持しています。
外部ディスプレイへの接続などを行わない一般的なiOSアプリの場合は基本的にconnectedScenesは1つとなると思います。
以下のようなコードでオブジェクトを取得できます。

UIApplication.shared.connectedScenes.first?.delegate as! SceneDelegate

また、以下のようなユーティリティを定義しておくと使用が楽です。

extension SceneDelegate {
    static var shared: SceneDelegate {
        UIApplication.shared.connectedScenes.first?.delegate as! SceneDelegate
    }
}

上記関数は以下のように使用ができ、簡単にUIViewControllerをSwiftUIから表示することができます。

let controller = UIViewController()
SceneDelegate.shared.window?.rootViewController.present(controller, animated: true)
2
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?