0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

SceneDelegate対応

Posted at

はじめに

WWDC2025で発表された通り、次のメジャーアップデートでSceneDelegateへの対応が必須要件となりました。本記事では、従来のAppDelegateベースのアプリからマルチシーン対応アプリへの移行方法について解説します。

SceneDelegateとは

SceneDelegateは、iOS 13から導入されたマルチウィンドウサポートに対応するための仕組みです。従来のAppDelegateがアプリ全体のライフサイクルを管理していたのに対し、SceneDelegateは個々のシーン(ウィンドウ)のライフサイクルを管理します。

移行手順

1. SceneDelegateファイルの作成

新規ファイルとしてSceneDelegate.swiftを作成します。

2. Info.plistの設定

Application Scene Manifestの設定を追加します。
46d3f884-a94d-4cd8-98d2-4c3ea90d96b4.png

3. AppDelegateの更新

シーン設定用のメソッドを追加します。

AppDelegate.swift
func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
    return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
}

移行が必要な主な変更点

ライフサイクルメソッドの移行

シーン単位のライフサイクルメソッドは、AppDelegateからSceneDelegateへ移行する必要があります。

移行が必要なメソッド例

AppDelegate.swift
func applicationDidBecomeActive(_ application: UIApplication) {
    // 前面化時の処理
}
SceneDelegate.swift
func sceneDidBecomeActive(_ scene: UIScene) {
    // 前面化時の処理
}

AppDelegateに残すメソッド

アプリ全体のライフサイクルに関するメソッドは、引き続きAppDelegateに残ります。

AppDelegate.swift
func application(
    _ application: UIApplication,
    didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil ) -> Bool {
    // アプリ起動時の処理
}

func applicationWillTerminate(_ application: UIApplication) {
    // アプリ終了前の処理
}

注意点

その1. URLスキーマ遷移

他アプリから呼ばれURLスキーマ連携される場合にSceneDelegateで呼ばれる関数がアプリの状態によって変わります。

AppDelegate.swift
func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([UIUserActivityRestoring]?) -> Void) -> Bool {
    guard let url = userActivity.webpageURL else { return false }
    handle(url: url)
}
SceneDelegate.swift
/// アプリ未起動時
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
    if let url = connectionOptions.urlContexts.first?.url {
        handle(url: url)
    }
}

/// アプリ起動中やバックグラウンド時
func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {
    guard let url = URLContexts.first?.url else { return }
    handle(url: url)
}

その2. Windowの取得方法の変更

従来のUIApplication.shared.delegate?.windowによる取得方法は使えなくなります。

推奨されない方法

UIApplication.shared.connectedScenesから取得する方法は、以下の理由から推奨されません:

  • マルチウィンドウ環境に対応していない
  • 外部ディスプレイが接続されている場合、それが別のウィンドウとして判定される

推奨される方法

UIKitの場合表示画面から取得するのが一番安全かと思います

let windowScene = view.window?.windowScene
0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?