Keeping User Data Private with SwiftUI’s redacted Modifier

Durul Dalkanat
2 min readJun 6, 2024

In iOS our apps handle sensitive user information, so it’s crucial to take steps to protect that data from being inadvertently exposed.

Apple’s SwiftUI framework provides the redacted modifier allows developers to obscure private content when it shouldn't be displayed, such as on-device lock screen notifications or screenshots.

The redacted modifier works in tandem with the privacySensitive modifier to control what content should be treated as private. Here's an example of how you might use these modifiers to redact a private string like a Card Number

struct ContentView: View {
// \.redactionReasons: This is a key path that points to a specific environment variable called redactionReasons. This variable holds a set of reasons for why content might be redacted (e.g., privacy, security).

// @Environment: This is a property wrapper that allows you to access the redactionReasons environment variable.
@Environment(\.redactionReasons) var redactionReasons

var body: some View {
VStack(spacing: 20) {
Text("Card Number")

// "[HIDDEN]" is a visual indicator used in place of the actual sensitive data when privacy redaction is active.
// This helps to protect sensitive information from unauthorized access.

Text(redactionReasons.contains(.privacy) ? "[HIDDEN]" : "1234 5678 9012 3456")