20

I'm trying to run a function when the app finish loading up, like viewDidLoad, but I'm using SwiftUI now and I have no viewDidLoad. How can I do this now?

var body: some View { NavigationView { Form { Section { self.exampleFunction() Text(" ...... ") } } } } 

I want to take some information from that function and present it in the text. But the way I'm doing it is wrong. It's not building.

4
  • You really need to show more code. We can't duplicate your issue - but worse, your issue doesn't make sense. viewDidLoad is part of UIKit and not part of SwiftUI. (You could try onAppear and onDisappear but I think that's not the issue.) What's your model? Are you using @State variables? 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... SwiftUI is reactive by nature, not the source or much. Commented Jul 18, 2019 at 17:45
  • @dfd when I was using UIKit, and I want to run a function that I created when the app finish loading, I was writing it under the viewDidLoad. Now that there is no viewDidLoad, how is it done? thanks! Commented Jul 18, 2019 at 17:48
  • Yes, understood. And part of what I call "paradigm change' involves understanding that you only have onAppear because that's all you get. (Just noted, I didn't downvote and won't.) Details.... what are you used to do in viewDidLoad? 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? Commented Jul 18, 2019 at 17:57
  • "and I want to run a function that I created when the app finish loading" But function? What is it that you do? It may be that viewDidLoad was never the appropriate place for it. Commented Jul 20, 2019 at 3:04

5 Answers 5

27

You can use .onAppear { ... } to execute arbitrary code when a view appears:

var body: some View { NavigationView { Form { Section { Text(" ...... ") }.onAppear { self.exampleFunction() } 
Sign up to request clarification or add additional context in comments.

1 Comment

Good answer. But does this fit for all circumstances like in UIKit? It sounds to me - and I'm probably wrong - that the OP is thinking too much like UIKit coding. For example, let's say you wish to load a NSURL content from onAppear. Will this work? snapper is simply not equatable to viewDidLoad. Two different stacks, two different APIs. EDIT: And do not forget that an - onAppear closure will happen every time that view appears. viewDidLoad happens only every time the UIView... was loaded.
14

Fully updated for Xcode 11.2, Xcode 5.0

I think the viewDidLoad() just equal to implement in the body closure.
SwiftUI gives us equivalents to UIKit’s viewDidAppear() and viewDidDisappear() in the form of onAppear() and onDisappear(). You can attach any code to these two events that you want, and SwiftUI will execute them when they occur.

As an example, this creates two views that use onAppear() and onDisappear() to print messages, with a navigation link to move between the two:

struct ContentView: View { var body: some View { NavigationView { VStack { NavigationLink(destination: DetailView()) { Text("Hello World") } } }.onAppear { print("ContentView appeared!") }.onDisappear { print("ContentView disappeared!") } } } 

ref:https://www.hackingwithswift.com/quick-start/swiftui/how-to-respond-to-view-lifecycle-events-onappear-and-ondisappear

1 Comment

but we can't call api (network request) in body closure.
9

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 ********** } } 

Comments

0

I'm using init() instead. I think onApear() is not an alternative to viewDidLoad(). Because onApear is called when your view is being appeared. Since your view can be appear multiple times it conflicts with viewDidLoad which is called once.

Imagine having a TabView. By swiping through pages onApear() is being called multiple times. However viewDidLoad() is called just once.

struct TeamInfoView: View { var myText:String init() { myText = function() } ..... } 

After having initialized it's time to use the value of the string:

var body: some View { NavigationView { Form { Section { Text(myText) } } } } 

Comments

0

You can use isPresented environemnt value to acheive the same.

1 Comment

This answer would be much more useful to others if you included a little example code demonstrating how the answer solves the issue.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.