SwiftUI: Observing Changes Using System Notifications

DevTechie
DevTechie
Published in
3 min readApr 30, 2024

--

SwiftUI: Observing Changes Using System Notifications

The Apple ecosystem leverages NotificationCenter to report changes at the system level. There are several types of notification events, such as when an app enters the background, when a user takes a screenshot on the device, or when there is a change in battery level. We can leverage these notifications to make important changes to our app.

Refer to the page below for full list of notifications:

Today, we will see how to use these system notifications in our own app. We will continue to build upon the example we saw in the previous article, where we built a view to pass data using NotificationCenter. If you build and run the example in the simulator and change the device orientation, you will notice that the VStack shrinks the content but keeps the layout same. What if we want to change the layout from vertical to horizontal based on the device’s orientation? There are a number of ways to do this, but this is a perfect example to show the usage of observing notifications produced at the system level.

--

--