Follow along at https://www.hackingwithswift.com/100/swiftui/87.
This day covers Part Two of Project 17
in the 100 Days of SwiftUI Challenge. (Project 17 files can be found in the directory for Part One.)
It focuses on several specific topics:
- Triggering events repeatedly using a timer
- How to be notified when your SwiftUI app moves to the background
- Supporting specific accessibility needs with SwiftUI
Apple is integrating Combine into Foundation
at every level, and Timer
is no exception.
We can compose a publisher out of Timer.publish
and have our view listen to any of its emitted events.
This listening can be done via View.onReceive
, but I usually prefer keeping the Timer in a view-model ObservableObject
, and having the view-model use the timer to update its own state -- which can include @Published
properties that the View is listening to.
Apple is integrating Combine into Foundation
at every level, and NotificationCenter
is no exception.
Using NotificationCenter.publisher(for:object:)
, we can create a publisher that emits the notifications broadcasted for a passed-in notification name. Because this includes the notifications broadcasted by UIApplication
, Combine gives us a new lense into things like willResignActiveNotification
, willEnterForegroundNotification
, userDidTakeScreenshotNotification
, and so much more.