How to code, in SwiftUI, for Notifications

I need to code differently when Voice Over is on.

I found this:

Code Block
UIAccessibility.voiceOverStatusDidChangeNotification

Notification coding is unknown to me. I need the Notification to change my variable "accessibilityIsOn: Bool" as Voice Over changes. I've been reading google and learned that I do not need Notifications; What I need is the way to detect if the user is using Voice Over.

More reading revels this:
Code Block
isVoiceOverRunning

So I will read how to use it.

Accepted Reply

Generally, SwiftUI is designed to work well with Combine.

You can try something like this:
Code Block
struct ContentView: View {
@State var accessibilityIsOn: Bool = false
var body: some View {
Text("This can be some complex View")
.padding()
.onReceive(NotificationCenter.default.publisher(for: UIAccessibility.voiceOverStatusDidChangeNotification)) { _ in
self.accessibilityIsOn = UIAccessibility.isVoiceOverRunning
}
}
}


Replies

Generally, SwiftUI is designed to work well with Combine.

You can try something like this:
Code Block
struct ContentView: View {
@State var accessibilityIsOn: Bool = false
var body: some View {
Text("This can be some complex View")
.padding()
.onReceive(NotificationCenter.default.publisher(for: UIAccessibility.voiceOverStatusDidChangeNotification)) { _ in
self.accessibilityIsOn = UIAccessibility.isVoiceOverRunning
}
}
}


Whoa! that is simply amazing to me!!. I am searching but would have much difficulty finding your response. Big thanks. I will study it. Your response needs to be in the Quick Help guide built in to Xcode. But Quick Help offers some little help instead.

I also found in Swift UIkit the variable
static var isVoiceOverRunning: Bool {get}

but first try at coding it failed so more study is in order.