If you're trying to run something after the app launches and has nothing to do with a specific view you can add code in two different places...
In AppDelegate.swift, the first function is called after the App launches...
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { // Override point for customization after application launch. // ******** Run your function here ********** return true }
Or in, SceneDelegate.swift, the first function actually sets the root view to the original ContentView...
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) { // Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`. // If using a storyboard, the `window` property will automatically be initialized and attached to the scene. // This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead). // Use a UIHostingController as window root view controller if let windowScene = scene as? UIWindowScene { let window = UIWindow(windowScene: windowScene) window.rootViewController = UIHostingController(rootView: ContentView()) self.window = window // ******** Add code here before root view is shown ********** window.makeKeyAndVisible() // ******** Add code here after root view is shown ********** } }
viewDidLoadis part ofUIKitand not part ofSwiftUI. (You could tryonAppearandonDisappearbut I think that's not the issue.) What's your model? Are you using@Statevariables? How can you do *what - specifically? Sorry, but for now I actually do have to downvote this question. PLEASE, be more specific. What you posted can be done, but apprearantly not how you are trying to do. And try to understand...SwiftUIis reactive by nature, not the source or much.onAppearbecause that's all you get. (Just noted, I didn't downvote and won't.) Details.... what are you used to do inviewDidLoad? And remember, that isn't "reactive", it's 'after-the-fact". I'm guessing you need to understand "reactive" - I'm barely grasping some details - along with explaining what your model is (or maybe just the model state). What function dod you run 8after*viewDidLoad, and what does it do?viewDidLoadwas never the appropriate place for it.