LoginSignup
3
7

More than 1 year has passed since last update.

【SwiftUI】AppDelegate.swift がない! 対処法

Last updated at Posted at 2021-08-28

参考

この記事は以下の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 でアプリを作成する際、デフォルトで作成される。

↓参考にした記事

3
7
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
3
7