LoginSignup
9
4

More than 3 years have passed since last update.

【SwiftUI】【Xcode12】AppDelegate.swift と SceneDelegate.swift が無いんだが

Posted at

なぜ

Xcode12で新しくプロジェクトを作る際に『Interface』の選択肢で「SwiftUIでコードを書くから」ということで UIKit App Delegate ではなく SwiftUI Appを選択すると掲題の2ファイルが作られない。
で、何が困るのかというとスーパー初心者がテキストなどを参考にした際に掲題のファイルにアレコレ書いてくれとあるが、そんなファイルが無いから困ってしまったのだ。

なんとかする

Testという名前のプロジェクトを作ったと仮定する。

TestApp.swift
//
//  TestApp.swift
//  Test
//
//  Created by 名前 on 日付.
//

import SwiftUI

@main
struct TestApp: App {
    @UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
    var body: some Scene {
        WindowGroup {

            // SceneDelegate.swift の代わりとして仮定
            // 最初に表示したい画面のファイル名を書く。例えば Index.swift の場合
            Index()
        }
    }
}

class AppDelegate: UIResponder, UIApplicationDelegate {
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {

        // この間に AppDelegate.swift の処理を書く。今回は書いてない

        return true
    }
}

struct TestApp_Previews: PreviewProvider {
    static var previews: some View {
        /*@START_MENU_TOKEN@*/Text("Hello, World!")/*@END_MENU_TOKEN@*/
    }
}


現時点でぼくがわかるのはこれくらいです。
以下のサイトを参考にしました。

[Xcode 12] アプリの起動について変更になった部分まとめ
https://dev.classmethod.jp/articles/xcode12_change_appdelegate/

9
4
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
9
4