Running a View Controller in background of another View Controller


I am trying to build an app that fetches information from the web in background and updates the fields in current View Controller.

For fetching information I need to log into the website, redirect to pages within and then fetch the required information. I am using WebView (and its evaluateJavaScript function) for doing this task. This information then should update the current View Controller which until then should show the previous information.

I wish to make the WebView functionality to execute in background while the user sees the previous information until the task is completed and then sees the updated information while (user) all the time being on the same View Controller.

I was able to make the WebView View Controller not show anything by not assigning the view of the View Controller to the webView.

How do I execute the WebView (currently a View Controller) functionality in the background of another View Controller?

take a look at DispatchQueue.main.async
this will run your code inside the closure in your main thread asynchronously

A ViewController to be displayed over (the WebViewController in case above) has to made with its superview background clear. Then it is instantiated and its modalPresentationStyle set to overCurrentContext (among many other options) in the view it is to be displayed over (MainViewController). I learnt this in one of the Chris’s lessons!

1 Like