【2024年4月】弊社では、基本リモートワークで一緒に成長してくださるメンバーを広く募集させていただいております。 詳細はこちら

【Swift】iOS13以降追加されたSceneDelegateについて徹底調査!

【Swift】iOS13以降追加されたSceneDelegateについて徹底調査!
ふくしま

こんにちは!株式会社メモリアインクのふくしまです!

iOS 13のリリースと共に、Appleはアプリケーションのライフサイクル管理に「SceneDelegate」を導入しました。
この記事では、そんな「SceneDelegate」について詳しく解説したいと思います。

この記事を読んで分かること…
・SceneDelegateとは?
・windowとviewの違い
・SceneDelegateの各ライフサイクルイベントとそれに対するアクション

目次

SceneDelegateとは?

SceneDelegateは、iOS 13以降で導入された新しい概念で、アプリケーションのウィンドウ(シーン)のライフサイクルを管理する役割を持ちます。これにより、アプリは複数のシーンを同時に扱い、ユーザーによりリッチな体験を提供できるようになりました。

ライフサイクルの管理にはAppDelegateを活用することもあります。
AppDelegateについてや、SceneDelegateとの違いについては以下の記事て解説していますので併せてご確認ください。

windowとviewの違い

ウィンドウ(Window)とビュー(View)は、グラフィカルユーザーインターフェイス(GUI)の構成要素であり、iOSやmacOSなどのAppleのプラットフォームにおいて重要な役割を果たしますが、用途には明確な違いがあります。

ウィンドウ(Window)

ウィンドウは、アプリケーションの画面上のコンテナとして機能し、一つまたは複数のビューを含むことができます。ウィンドウ自体は直接的にコンテンツを表示しませんが、ビューやビューコントローラーがそのコンテンツを描画するための基盤を提供します。

ビュー(View)

ビューは、ウィンドウ内でユーザーに具体的なコンテンツを表示するためのオブジェクトです。テキスト、画像、図形など、さまざまな情報を描画する役割を持ち、ユーザーの入力に応じてインタラクションを提供します。

結論

ウィンドウはアプリケーションのビジュアルコンテンツの基盤となり、ビューはそのウィンドウ内でユーザーに具体的な情報を表示する役割を持ちます。つまり、ウィンドウはビューを格納するためのコンテナであり、ビューはウィンドウ内でユーザーとのインタラクションを実現するための具体的な要素です。

各ライフサイクルイベントとそれに対するアクション

SceneDelegateには複数のライフサイクルイベントがあり、アプリケーションが特定の状態になると呼び出されます。これらのイベントを適切に管理することで、アプリのパフォーマンスを最適化し、ユーザー体験を向上させることができます。

SceneDelegate.swift

以下、プロジェクト生成時に作成される「SceneDelegate」ファイルの中身です。

import UIKit

class SceneDelegate: UIResponder, UIWindowSceneDelegate {

    var window: UIWindow?


    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
        // Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
        // If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
        // This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).
        guard let _ = (scene as? UIWindowScene) else { return }
    }

    func sceneDidDisconnect(_ scene: UIScene) {
        // Called as the scene is being released by the system.
        // This occurs shortly after the scene enters the background, or when its session is discarded.
        // Release any resources associated with this scene that can be re-created the next time the scene connects.
        // The scene may re-connect later, as its session was not necessarily discarded (see `application:didDiscardSceneSessions` instead).
    }

    func sceneDidBecomeActive(_ scene: UIScene) {
        // Called when the scene has moved from an inactive state to an active state.
        // Use this method to restart any tasks that were paused (or not yet started) when the scene was inactive.
    }

    func sceneWillResignActive(_ scene: UIScene) {
        // Called when the scene will move from an active state to an inactive state.
        // This may occur due to temporary interruptions (ex. an incoming phone call).
    }

    func sceneWillEnterForeground(_ scene: UIScene) {
        // Called as the scene transitions from the background to the foreground.
        // Use this method to undo the changes made on entering the background.
    }

    func sceneDidEnterBackground(_ scene: UIScene) {
        // Called as the scene transitions from the foreground to the background.
        // Use this method to save data, release shared resources, and store enough scene-specific state information
        // to restore the scene back to its current state.
    }
}

ライフサイクルイベントごとの説明

行 5: ウィンドウオブジェクトのプロパティを宣言しています。
このウィンドウはアプリケーションのビューコンテンツを表示するために使用されます。

willConnectTo

willConnectToイベントは、アプリケーションのシーンが初めて接続される時に呼び出されます。ここで、UIの初期設定やデータの準備を行います。(7行目)

sceneDidDisconnect

sceneDidDisconnectイベントは、シーンが切断されるときに発生します。この時点で、不要なリソースの解放や保存が必要なデータの保存を行います。(14行目)

sceneDidBecomeActive

sceneDidBecomeActiveイベントは、シーンがアクティブになった時に呼び出されます。ここで、アプリケーションの更新やリフレッシュを行います。(21行目)

sceneWillResignActive

sceneWillResignActiveイベントは、シーンが非アクティブになる直前に発生します。ユーザーが別のアプリに切り替える場合などに、現在の状態の保存や一時停止するアクションをここで行います。(26行目)

sceneWillEnterForeground

sceneWillEnterForegroundイベントは、シーンがバックグラウンドからフォアグラウンドに移行する時に呼び出されます。アプリが再びユーザーに見える準備をします。(31行目)

sceneDidEnterBackground

sceneDidEnterBackgroundイベントは、シーンがバックグラウンドに移行した時に発生します。ここで、ユーザーデータの保存やセッションの終了処理を行います。(36行目)

まとめ

いかがでしたか?
SceneDelegateを利用することで、iOS 13以降のアプリケーションはより柔軟にシーン管理ができるようになります。この記事を通じて、SceneDelegateのライフサイクルイベントの活用方法を学ぶことができました。
この記事が、皆様の開発ライフの一助となれることを切に願っております。

ふくしま

この記事があなたのスキルアップに役立ったなら、次のキャリアステップを踏み出す絶好の機会かもしれません。エンジニアとしてのさらなる成長と挑戦を求めるなら、
未経験からIT・Webエンジニアを目指すなら【ユニゾンキャリア】
をオススメします!

よかったらシェアしてね!
  • URLをコピーしました!
  • URLをコピーしました!

この記事を書いた人

コメント

コメント一覧 (2件)

コメントする

目次