目的
Xcode11で新規プロジェクトを立ち上げたところ。
SceneDelegate.swift
なるものがデフォルトでつくられた。ナンジャこれは。
ということで、SceneDelegateについてのメモなのですが。
環境
Xcode 11.0
iOS13を試せる端末がないので、Simulatorでのテスト
参照ページ
[iOS13] UIScene APIを使用する [Xcode11]
↑のページがSceneDelegateを網羅しているなと。
この記事ではこれ以上のことは書けないなと。
そもそもSceneってなんだ
SceneDelegate
を調べる前にそもそもSceneってなんだということで。
WWDC 2019 videoより引用
UIScreenとUIWindowの間は設置されるオブジェクトのことで、
おそらくXcode11から対応になったマルチウィンドウ
を実現するためのものじゃないかと思います
マルチウィンドウを試してみる
ということで、マルチウィンドウを試してみます。
新規プロジェクトを作り、以下のように、Storyboardに少し手を加えただけのアプリがあります。
これだけではマルチウィンドウ化はできません。(ちなみにiOS9からある、Split View
などは可能です。)
TARGETS設定より
Supports multiple windows
にチェックをいれます。
これだけで、マルチウィンドウ機能が使えます。
delegateについて
以下の関数が標準で作られていまして、その役割ぐらいは記載しようと思います。
以下のようにコメントを付けてみました。
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).
print("scene_willConnectTo")
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 neccessarily discarded (see `application:didDiscardSceneSessions` instead).
print("sceneDidDisconnect")
}
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.
print("sceneDidBecomeActive")
}
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).
print("sceneWillResignActive")
}
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.
print("sceneWillEnterForeground")
}
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.
print("sceneDidEnterBackground")
}
上記のプログラムでアプリを実行してみるとログは以下のようになりました。
アプリ起動時
起動時のログは以下。
scene_willConnectTo
sceneWillEnterForeground
sceneDidBecomeActive
アプリをバックグラウンドに
バックグラウンド時のログは以下。
正確にはsceneWillResignActive
はAPPスイッチャーの時点で発火
sceneWillResignActive
sceneDidEnterBackground
アプリをバックグラウンドからフォアグラウンドに
フォアグラウンドになった時のログは以下。
sceneWillEnterForeground
sceneDidBecomeActive
アプリを終了したとき
何も呼ばれなかった。
sceneDidDisconnect
が呼ばれると思うんだけど。
シミュレータの不具合とか?自分のやり方が問題!?
マルチウィンドウで2個目を追加した時
以下の状態のこと
マルチウィンドウを追加した時のログは以下。
sceneWillResignActive
sceneDidBecomeActive
scene_willConnectTo
sceneWillEnterForeground
sceneDidBecomeActive
2個目をマルチウィンドウからsplitViewにした時
以下の状態のこと
splitViewにした時のログは以下。
sceneWillResignActive
sceneWillResignActive
sceneDidBecomeActive
sceneDidBecomeActive
2個目を消した時
2つのsceneWillResignActive
はAPPスイッチャーの時点で発火
2個目を消した時のログは以下。
sceneWillResignActive
sceneWillResignActive
sceneDidBecomeActive
sceneDidEnterBackground
もう一度マルチウィンドウで2個目を追加した時
今度はscene_willConnectTo
が呼ばれないよう。。
2個目をもう一度追加した時のログは以下。
sceneWillResignActive
sceneDidBecomeActive
sceneWillEnterForeground
sceneDidBecomeActive
正直まだアプリのtargetをiOS13に設定しようとは思わず。
いざ使う時になったら、また仕様変わってそう
メモとしては以上です。