In the newest SwiftUI template project there is no AppDelegate (like in the oldern-days :^) so where do I put the code to work with UserDefaults?
Any pointers? Thanks!
In the newest SwiftUI template project there is no AppDelegate (like in the oldern-days :^) so where do I put the code to work with UserDefaults?
Any pointers? Thanks!
 
    
    Now why didn't I think to do this... (because I'm a newbie). And coming from a Java world... where the one Class <-> one File rule has some werid influence upon code structure thinking. Got give up the Java.
import SwiftUI
import UIKit
// no changes in your AppDelegate class
class AppDelegate: NSObject, UIApplicationDelegate {
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
        print(">> your code here !!")
        return true
    }
}
@main
struct Testing_SwiftUI2App: App {
    // inject into SwiftUI life-cycle via adaptor !!!
    @UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
    var body: some Scene {
        WindowGroup {
            ContentView()
        }
    }
}
 
    
    Of course if you want to use "pure" swiftUI, you could put your code in the init() of @main...
Like:
@main
struct Testing_SwiftUI2App: App {
    init() {
    // your userdefaults code here...
    }
    var body: some Scene {
        WindowGroup {
            ContentView()
        }
    }
}
