【Swift UIKit】SceneDelegate.swiftとは?役割と使い方まとめ

この記事からわかること
- SwiftのUIKitのSceneDelegate.swiftとは?
- 役割や使い方
- sceneメソッドの使用方法
index
[open]
\ アプリをリリースしました /
SceneDelegate.swiftとは?
「SceneDelegate.swift」ファイルはUIKitフレームワークで新規プロジェクトを作成した際にiOS13/Xcode11から自動で作られるようになったファイルの1つです。
作成されるファイル群
├── Swiftプロジェクト
│ ├── プロジェクト名
│ ├── AppDelegate.swift
│ ├── SceneDelegate.swift
│ ├── ViewController.swift
│ ├── Main.storyboard
│ ├── Assets.xcassets
│ ├── LaunchScreen.storyboard
│ └── info.plist
│ └── プロジェクト名.xcodeproj
似たようなファイルにAppDelegate.swiftがありますが、このファイルで担っていたライフサイクルに関する機能がSceneDelegate.swiftでも実装できるようになっています。
役割
SceneDelegate.swiftの役割は複数のUIインスタンスを作成とライフサイクルの管理ができることです。これによりiPadに備わっているSplit Viewなどの機能では同じアプリを別画面として表示させることができるようになっています。
ライフサイクルに関してはAppDelegate.swiftからも管理しているアプリ起動時などに実行できる処理などが提供されています。
まとめ
- iOS13/Xcode11から自動生成されるファイル
- 複数のUIインスタンスを生成できる
- アプリのライフサイクルを管理
- AppDelegate.swiftと似たような役割を持つ
概要
生成されたファイルの中身は以下のようにUIWindowSceneDelegate
プロトコルに準拠したクラスになっています。あらかじめライフサイクルイベント発生時に呼び出されるメソッドが複数用意されています。
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.
}
}
scene(_:willConnectTo:options:)
アプリが起動した時に呼ばれるメソッド。UIWindowのインスタンスの生成と最初に接続するView Controllerを設定しています。プロジェクト生成時はStoryboardと紐付いているので自動でインスタンス化とView Controllerの設定 が行われています。
sceneDidDisconnect(_:)
アプリが削除されたときに呼ばれるメソッド。
sceneDidBecomeActive(_:)
アプリが非アクティブ状態からアクティブ状態になったときに呼ばれるメソッド。
sceneWillResignActive(_:)
アプリがアクティブ状態から非アクティブ状態になったときに呼ばれるメソッド。
sceneWillEnterForeground(_:)
アプリがバックグラウンドからフォアグラウンドに送られたときに呼ばれるメソッド。
sceneDidEnterBackground(_:)
アプリがフォアグラウンドからバックグラウンドに送られたときに呼ばれるメソッド。
使用例
import UIKit
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
var window: UIWindow?
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
print("初期化!")
guard let _ = (scene as? UIWindowScene) else { return }
}
func sceneDidDisconnect(_ scene: UIScene) {
print("削除されました")
}
func sceneDidBecomeActive(_ scene: UIScene) {
print("アクティブになりました")
}
func sceneWillResignActive(_ scene: UIScene) {
print("非アクティブになりました")
}
func sceneWillEnterForeground(_ scene: UIScene) {
print("フォアグラウンドになりました") }
func sceneDidEnterBackground(_ scene: UIScene) {
print("バックグラウンドになりました")
}
}
まだまだ勉強中ですので間違っている点や至らぬ点がありましたら教えていただけると助かります。
ご覧いただきありがとうございました。
個人開発に限界を感じたらiOSに特化したプログラミングスクール「iOSアカデミア」も検討してみてください!無料相談可能で「最短・最速」でiOSエンジニアになれるように手助けしてくれます。