Using Custom Masking for Session Replay

Learn how to mask parts of your app's data in Session Replay.

By default, our Session Replay SDK masks all text content, images, and user input. This helps ensure that no sensitive data will be exposed. You can also manually choose which parts of your app's data you want to mask by using the different options listed below.

You can choose which type of view you want to mask or unmask by using the maskedViewClasses or unmaskedViewClasses options.

Let's say you have a custom view that you want to mask and a UILabel subclass (which normally would be masked) that you don't want to mask. You can set the options like this:

Copied
  options.sessionReplay.maskedViewClasses = [MyCustomView.self]
  options.sessionReplay.unmaskedViewClasses = [MyCustomLabel.self]

You can also choose to mask or unmask a specific view instance by using the replay API (SentrySDK.replay) or view extensions like this:

Copied
  SentrySDK.replay.maskView(view: view)
  SentrySDK.replay.unmaskView(view: label)

or

Copied
  view.sentryReplayMask()
  label.sentryReplayUnmask()

Because of the way SwiftUI is transformed into UIKit, it will often be over-masked. A modifier like background uses the same element as an Image. In order to control the SwiftUI masking process, you need to use the sentryReplayUnmask and/or sentryReplayMask modifiers.

In this example we want to show the message, but not the user name.

Copied
  @Binding var user: String

  var body: some View {
    VStack {
      Text("Hello")
        .sentryReplayUnmask()
      Text("\(user)")
    }
  }

In this example, we need to unmask the VStack because its background element will be masked by default. To hide the username, we need to mask it.

Copied
  @Binding var user: String

  var body: some View {
    VStack {
      Text("Hello")
      Text("\(user)")
        .sentryReplayMask()
    }
    .background(.blue)
    .sentryReplayUnmask()
  }

To see how elements are being masked, enable the masking preview from anywhere in your app. It will display an overlay on top of the masked elements. This works on the simulator and on device, as well as within Xcode Preview.

Copied
  SentrySDK.replay.showMaskPreview()

By default, the overlay will be opaque. To configure the opacity, pass the desired opacity as a parameter:

Copied
  SentrySDK.replay.showMaskPreview(0.5) // 0.5 opacity to render the preview semi-transparent

Make sure not accidentally include this in your release build by e.g. wrapping it in a #if DEBUG block.

Copied
#if DEBUG
  SentrySDK.replay.showMaskPreview()
#endif

To preview masking during the design phase of your SwiftUI views, use the sentryReplayPreviewMask modifier.

This view modifier works on the simulator and on device, as well as within Xcode Preview. Therefore we recommend to apply the modifier only in your preview code, to ensure proper masking without affecting the final release build.

Note that when you apply this modifier to a view, it will show the masking preview for the entire window containing that view, not just the view itself.

Copied
struct ContentView_Previews: PreviewProvider {
  static var previews: some View {
    ContentView()
      .sentryReplayPreviewMask()
  }
}
Was this helpful?
Help improve this content
Our documentation is open source and available on GitHub. Your contributions are welcome, whether fixing a typo (drat!) or suggesting an update ("yeah, this would be better").