-1

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)
}
anySwift
  • 87
  • 1
  • 8
  • Your view doesn't "appear" when your apps is suspended and then returns to the foreground. You will need to use the `didBecomeActive` notification in this case. `viewWillAppear` is called when the view controller appears - is presented in your app – Paulw11 Oct 06 '22 at 06:20
  • This should provide you with an explanation: https://stackoverflow.com/questions/5277940/why-does-viewwillappear-not-get-called-when-an-app-comes-back-from-the-backgroun – thedp Oct 06 '22 at 06:22
  • @Paulw11, thank you.. `didBecomeActive` notification will work when i come back from web page but My Question is: "this notification call will not effect when i come to that viewcontroller normally? i mean when i come from another viewcontroller to this viewcontroller not from web page.. in this case? " – anySwift Oct 06 '22 at 06:36
  • @anySwift When you travel inside the application, between VCs, `viewWillAppear` will be called. It is for coming back into the app that this notification is required. – thedp Oct 06 '22 at 07:13

1 Answers1

1

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.

thedp
  • 8,350
  • 16
  • 53
  • 95