Maxime Franchot
Maxime Franchot

Reputation: 1133

Why doesn't my Swift app have AppDelegate or SceneDelegate?

So I'm new to Swift. I see it's a fast-moving language.

But so many tutorials I see have AppDelegate and SceneDelegate as standard init files, why don't I have any? Why can't I seem to find an option for them?

The tutorials include one from July 2020 so I'm guessing this is some sort of recent update. How should I approach the two types of apps with different file types? As a "Veteran" programmer, which is more advantageous?

Thanks everyone. Happy Coding.

Upvotes: 10

Views: 4632

Answers (3)

michael
michael

Reputation: 917

You can add an AppDelegate class to your SwiftUI project as follows:

  1. Create a new file called AppDelegate.swift with the following content:
import Foundation
import UIKit

class AppDelegate: NSObject, UIApplicationDelegate {

    // swiftlint: disable line_length
    func application(_ application: UIApplication,
                     didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? = nil) -> Bool {
        setupMyApp()
        return true
    }

    private func setupMyApp() {
        // TODO: Add any intialization steps here.
        print("Application started up!")
    }
}
  1. Add the following line to your App.swift file before the body property:
...
struct SampleApp: App {

    @UIApplicationDelegateAdaptor private var appDelegate: AppDelegate

    var body: some Scene {
...

Source

Upvotes: 2

Casey Perkins
Casey Perkins

Reputation: 1932

The only way I know to get the result you did is to choose "SwiftUI" for Interface and "SwiftUI App" for Life Cycle in the New Project dialog. If you choose "Storyboard" for Interface, or "UIKit App Delegate" for Life Cycle, you will get the files you expected.

enter image description here

Upvotes: 6

Alexander Li
Alexander Li

Reputation: 791

When creating a new iOS app, if you select SwiftUI App for the life cycle, this replaces the use of AppDelegate and SceneDelegate.

If you want a classic lifecycle delegate, you can select UIKit App Delegate instead.

Creating a Swift UI App

Upvotes: 6

Related Questions