プロジェクト作成したらしれっと現れる SceneDelegate ぱいせん。
デフォルトで実装されるようになったんですね
たいして気にしてなかったんですけど、地味に弊害があったので、ちょっと調べてみようと思いまして。
忘れてまたウェブを横断しないように、ここにまとめておきます。
開発環境(前提条件)
・Xcode11.4
・Swift5
SceneDelgate とは
ざっくり概要
iPadOS では SplitView という1画面で同じアプリを2つ並べることができるようになり、それをサポートするのが SceneDelgate です。
これ関連で Apple さんがよく言う「1つのアプリに対して複数の UI のインスタンスを作れるように」ってやつです。
一応 SceneDelegate は必須ではありません。
ライフサイクルについて
AppDelegate か SceneDelegate に同じライフサイクルイベントに関するメソッドを実装しても、片方のメソッドしか呼ばれません。
iOS13 以上で SceneDelegate が実装されている場合は SceneDelegate が動き、それ以外では AppDelegate が動きます。
ちなみにライフサイクルイベントとは、Resign/BecomeActive や openURL などで、NotificatonCenter はどちらでも動きます。
気になる方は公式ドキュメント読んでください。
不要な SceneDelegate を削除する方法
AppDelegate.swift での作業
AppDelegate.swift に以下のメソッドが作られてると思いますが、コメントにあるように UISceneSession のメソッドなので、全部削除します。
// 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) {
// 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.
}
そのまま AppDelegate.swift で window を追加します。
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow? // これを追加
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
return true
}
}
Info.plist での作業
UIApplicationSceneManifest という UIScene 関連の項目があるので、これを削除します。
Info.plist を右クリックして opne As → Source Code を選択するとコードで表示されるので、以下の部分をまるっと削除してください。
UIApplicationSceneManifest UIApplicationSupportsMultipleScenes UISceneConfigurations UIWindowSceneSessionRoleApplication UISceneConfigurationName Default Configuration UISceneDelegateClassName $(PRODUCT_MODULE_NAME).SceneDelegate UISceneStoryboardFile Splash
最後に SceneDelegate.swift を削除してください。Move to Trash でございます。
これで UIApplication を中心に動きます。
さいごに
Swift2 → Swift3 のときの破壊的変更を思うと、まだ救いがあります (˘ਊ˘)
まぁ削除とかせずに慣れていかないといけないですよね。笑



コメント