Why screen viewWillAppear is not calling when i back from browser in swift

335 Views Asked by At

I am calling my servicecall in viewWillAppear, and in same screen when i click on button webpage is opening in safari browser here when i go back to app from safari then my servicecall is not calling why?

like this i am going back from safari to app

enter image description here

code: here when i come back from webpage my sellServiceCall is not calling why?.. how to make it call when i back from safari web page.. guide me please

override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
sellServiceCall()
}

@IBAction func connectStripeBtn(_ sender: Any) {

 stripeUrl = "someurl"
 UIApplication.shared.open(url)
}
1

There are 1 best solutions below

0
thedp On

When you travel inside the application, viewDidAppear and viewWillAppear will be called each time the VC comes into view.

But, when you leave the app and it goes into "background", and then come back, it will never trigger viewDidAppear and viewWillAppear. Because from the VC's point of view, you never left.

This is how you can listen to coming to foreground event, which also covers app launch:

NotificationCenter.default.addObserver(
    forName: UIApplication.willEnterForegroundNotification,
    object: nil,
    queue: .main,
    using: { _ in
        // do your logic here
    }
)

If you need to update the VC when user comes back into foreground, you will need to listen to this event on that VC and execute your logic there.

Note: A better practice is not to listen to events directly on the VC, and instead find a way to propagate the event to the VC. Depends on your app's architecture.