DEV Community

loading...

Day 87 of 100 Days Of SwiftUI

Oluwasanmi Aderibigbe
Android developer
・1 min read

I just completed day 87 of 100 days of SwiftUI. Today I learnt how to perform an action repeatedly using the Timer class. I also learnt how to be notified when your app goes to the background.

In order to know when your app goes to the background, you have to Apple's Notification Center API. Notification Center is internal message system. It allows the system notify us of events. It also let us send events between different parts of our application.
For example, to know when your app in the background, you can do this

Text("Hello, World!")
    .onReceive(NotificationCenter.default.publisher(for: UIApplication.willResignActiveNotification)) { _ in
        print("Moving to the background!")
    }
Enter fullscreen mode Exit fullscreen mode

Discussion (0)