Redacted
modifier was introduced in SwiftUI with iOS 14 release. Redacted
is a modifier that replaces content with a placeholder. It takes reason as a parameter which is an instance of RedactionReasons
structure and it specifies the reasons to apply a redaction to data displayed on screen.
Possible values are:
placeholder:
Displayed data should appear as generic placeholders.
privacy:
Displayed data should be obscured to protect private information.
Let’s look at an example.
struct DevTechieRedactedExample: View {
var body: some View {
VStack {
Text("DevTechie")
.font(.largeTitle)
Text("Learn iOS Development")
.font(.subheadline)
.foregroundColor(.secondary)
HStack {
Text("iOS")
Text("SwiftUI")
Text("UIKit")
}
.redacted(reason: .placeholder)
}
}
}
Redaction reason is passes to child view via environment variable.
struct DevTechieRedactedExample: View {
var body: some View {
VStack {
Text("DevTechie")
.font(.largeTitle)
Text("Learn iOS Development")
.font(.subheadline)
.foregroundColor(.secondary)
HStack {
RedactedChildView(text: "iOS")
.redacted(reason: .placeholder)
RedactedChildView(text: "SwiftUI")
.redacted(reason: .placeholder)
RedactedChildView(text: "UIKit")
}
}
}
}struct RedactedChildView: View {
@Environment(\.redactionReasons) var redactionReason
var text: String
var body: some View {
if redactionReason.contains(.placeholder) {
Text("** Just a placeholder **")
} else if redactionReason.contains(.privacy) {
Text("*** This is private ***")
} else {
Text("Nothing to hide. Its \(text)")
}
}
}
Unredacted modifier is used to remove all redaction reasons from a view. If we modify our child view to include unredacted modifier for all the Text views, we will find that all the text behind redaction is now visible.
We can apply unredaction on the view directly or to the Group. For our example, let’s apply it to the Group.
struct RedactedChildView: View {
@Environment(\.redactionReasons) var redactionReason
var text: String
var body: some View {
Group {
if redactionReason.contains(.placeholder) {
Text("** Just a placeholder **")
} else if redactionReason.contains(.privacy) {
Text("*** This is private ***")
} else {
Text("Nothing to hide. Its \(text)")
}
}.unredacted()
}
}