参考
この記事は以下の2つのサイトを参考にしました。
これらの記事を見る方が、よく理解できると思います。
目的
SwiftUI で AppDelegate.swift(Swift×UIKit) のような機能を追加する
(おまけ) Swift での AppDelegate.swift って何のためにあるのか解説
対処法
①〜③ を行う。
SampleApp.swift
import SwiftUI
@main
struct SampleApp: App {
// ① : 既存のAppDelegateを利用することができるようになる
@UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
var body: some Scene {
WindowGroup {
// SceneDelegate.swift の代わりとなる
// ② : 最初に表示したい画面のファイル名を書く。例えば FirstViewApp.swift の場合
FirstViewApp()
}
}
}
class AppDelegate: UIResponder, UIApplicationDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
// ③ : ここに AppDelegate.swift の処理を書く。
// 今回は省略
return true
}
}
(おまけ) Swift での AppDelegate.swift って何のためにあるのか解説
「アプリの状態に応じて自動で呼ばれる関数」の集まり。
Swift でアプリを作成する際、デフォルトで作成される。
↓参考にした記事