|
| 1 | +--- |
| 2 | +title: Using Custom Masking for Session Replay |
| 3 | +sidebar_order: 5501 |
| 4 | +notSupported: |
| 5 | +description: "Learn how to mask parts of your app's data in Session Replay." |
| 6 | +--- |
| 7 | + |
| 8 | +<Alert> |
| 9 | +Before publising an App with Session Replay enabled, make sure to test it thoroughly to ensure that no sensitive data is exposed. |
| 10 | + |
| 11 | +If you choose to use custom masking in your Session Replays, you may accidentally expose sensitive customer data. Be sure to double-check what you choose to expose. |
| 12 | + |
| 13 | +</Alert> |
| 14 | + |
| 15 | +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. |
| 16 | + |
| 17 | + |
| 18 | +## Mask by View Class |
| 19 | + |
| 20 | +You can choose which type of view you want to mask or unmask by using the `maskedViewClasses` or `unmaskedViewClasses` options. |
| 21 | + |
| 22 | +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: |
| 23 | + |
| 24 | +```swift |
| 25 | + options.experimental.sessionReplay.maskedViewClasses = [MyCustomView.self] |
| 26 | + options.experimental.sessionReplay.unmaskedViewClasses = [MyCustomLabel.self] |
| 27 | +``` |
| 28 | + |
| 29 | +## Mask by View Instance |
| 30 | + |
| 31 | +You can also choose to mask or unmask a specific view instance by using the replay API (`SentrySDK.replay`) or view extensions like this: |
| 32 | + |
| 33 | +```swift |
| 34 | + SentrySDK.replay.maskView(view: view) |
| 35 | + SentrySDK.replay.unmaskView(view: label) |
| 36 | +``` |
| 37 | +or |
| 38 | + |
| 39 | +```swift |
| 40 | + view.sentryReplayMask() |
| 41 | + label.sentryReplayUnmask() |
| 42 | +``` |
| 43 | + |
| 44 | +## SwiftUI |
| 45 | + |
| 46 | +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`. |
| 47 | +In order to control the SwiftUI masking process, you need to use the `sentryReplayUnmask` and/or `sentryReplayMask` modifiers. |
| 48 | + |
| 49 | +In this example we want to show the message, but not the user name. |
| 50 | +```swift |
| 51 | + @Binding var user: String |
| 52 | + |
| 53 | + var body: some View { |
| 54 | + VStack { |
| 55 | + Text("Hello") |
| 56 | + .sentryReplayUnmask() |
| 57 | + Text("\(user)") |
| 58 | + } |
| 59 | + } |
| 60 | +``` |
| 61 | + |
| 62 | +In this example, we need to unmask the VStack because its background element will be masked by default. |
| 63 | +To hide the username, we need to mask it. |
| 64 | + |
| 65 | +```swift |
| 66 | + @Binding var user: String |
| 67 | + |
| 68 | + var body: some View { |
| 69 | + VStack { |
| 70 | + Text("Hello") |
| 71 | + Text("\(user)") |
| 72 | + .sentrtReplayMask() |
| 73 | + } |
| 74 | + .background(.blue) |
| 75 | + .sentryReplayUnmask() |
| 76 | + } |
| 77 | +``` |
0 commit comments