Reputation: 1133
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
Reputation: 917
You can add an AppDelegate
class to your SwiftUI project as follows:
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!")
}
}
App.swift
file before the body
property:...
struct SampleApp: App {
@UIApplicationDelegateAdaptor private var appDelegate: AppDelegate
var body: some Scene {
...
Upvotes: 2
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.
Upvotes: 6
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.
Upvotes: 6