🐥

SceneDelegateを使用せずにiOSでアプリをビルドするようにする

2020/12/23に公開

SceneDelegateとは

Xcode11以降、iOSアプリのプロジェクトを生成するとAppDelegateの他にSceneDelegateというクラスもプロジェクトフォルダ内に自動生成されるようになったそうです。

このSceneDelegateを活用することで、1つのアプリに対して複数のUIインスタンスを生成することができるようです。例えば、iPadOS上で動くアプリの場合、Slide OverSplit Viewの機能が登場したことにより、アプリのViewを構成するときに単純にバックグラウンド・フォアグラウンドの2状態以外の状態も有り得るようになったので、その場合にSceneDelegateを用いて実装するシーンがあるそうです。そしてその流れでiOSアプリ向けのプロジェクトであってもSceneDelegateが自動生成される状態になっています。

しかし、SceneDelegate自体は実装が必須ではなく、iOSの場合、iOS13.0以降でのみ対応しているため、iOS12系以前ではビルドすることができません。プロジェクトでiOS12系以前でもサポートが対応が必要な場合は手動でSceneDelegateを削除する必要があります。

久々に新規でプロジェクトを立ち上げてハマったので、備忘がてらSceneDelegateをプロジェクト内から削除する手順を残します。

Xcode 12.0 (12A7209)
Swift 5.3

手順

  1. AppDelegate.swiftから以下のメソッドを削除する

        // MARK: UISceneSession Lifecycle
    
        func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
            // Called when a new scene session is being created.
            // Use this method to select a configuration to create the new scene with.
            return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
        }
    
        func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) {
            // Called when the user discards a scene session.
            // If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions.
            // Use this method to release any resources that were specific to the discarded scenes, as they will not return.
        }
    
  2. AppDelegate.swiftにvar window: UIWindow?を追加する

  3. Info.plistの中でUIApplicationSceneManifestのkey-valueの組み合わせをまとめて削除する

  4. SceneDelegate.swiftのファイルを削除する (Move To Trash)

参考

https://tech.yappli.io/entry/scenedelegate

https://qiita.com/u5_03/items/ff005e7cab7ff509f140

Discussion