Hello Readers, CoolMonkTechie heartily welcomes you in A Short Note Series.
In this note series, we will learn about how to prepare UI to run in the foreground in iOS. We will discuss how to configure our app to appear onscreen.
So Let’s begin.
We use foreground transitions to prepare our app’s UI to appear onscreen. An app’s transition to the foreground is usually in response to a user action. For example, when the user taps the app’s icon, the system launches the app and brings it to the foreground. We use a foreground transition to update our app’s UI, acquire resources, and start the services, we need to handle user requests.
All state transitions result in UIKit sending notifications to the appropriate delegate object:
- In iOS 13 and later—A
- In iOS 12 and earlier—The
We can support both types of delegate objects, but UIKit always uses scene delegate objects when they are available. UIKit notifies only the scene delegate associated with the specific scene that is entering the foreground.
Update Our App’s Data Model when Entering the Foreground
At launch time, the system starts our app in the inactive state before transitioning it to the foreground. We use our app’s launch-time methods to perform any work needed at that time. For an app that is in the background, UIKit moves our app to the inactive state by calling one of the following methods:
- For apps that support scenes—The
sceneWillEnterForeground(_:)method of the appropriate scene delegate object.
- For all other apps—The
When transitioning from the background to the foreground, we use these methods to load resources from disk and fetch data from the network.
Configure User Interface and Initial Tasks at Activation
The system moves our app to the active state immediately before displaying the app’s UI. Activation is a good time to configure our app’s UI and runtime behavior; specifically:
- Show your app’s windows, if needed.
- Change the currently visible view controller, if needed.
- Update the data values and state of views and controls.
- Display controls to resume a paused game.
- Start or resume any dispatch queues that you use to execute tasks.
- Update data source objects.
- Start timers for periodic tasks.
We can put our configuration code in one of the following methods:
- For a scene-based UI—The
sceneDidBecomeActive(_:)method of the appropriate scene delegate object.
- For all other apps—The
applicationDidBecomeActive(_:)method of your app delegate object.
Activation is also the time to put finishing touches on our UI before displaying it to the user. Don’t run any code that might block our activation method. Instead, make sure we have everything, we need in advance. For example, if our data changes frequently outside of the app, we use background tasks to fetch updates from the network before our app returns to the foreground. Otherwise, be prepared to display existing data while we fetch changes asynchronously.
Start UI-Specific Tasks when View Appears
When our activation method returns, UIKit shows any windows that we made visible. It also notifies any relevant view controllers that their views are about to appear. We use our view controller’s
viewWillAppear(_:) method to perform any final updates to our interface. For example:
- Start user interface animations, as appropriate.
- Begin playing media files, if auto-play is enabled.
- Begin displaying graphics for games and immersive content at their full frame rates.
Don’t try to show a different view controller or make major changes to our user interface. By the time our view controller appears onscreen, our interface should be ready to display.
In this note series, We understood how to prepare UI to run in the foreground in iOS. We also discussed how to configure our app to appear onscreen.
Thanks for reading ! I hope you enjoyed and learned about UI Preparation concepts during foreground in iOS. Reading is one thing, but the only way to master it is to do it yourself.
Please follow and subscribe us on this blog and and support us in any way possible. Also like and share the article with others for spread valuable knowledge.
If you have any comments, questions, or think I missed something, feel free to leave them below in the comment box.
Thanks again Reading. HAPPY READING !!😊😊😊