-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add postHogNoMask SwiftUI view modifier (#277)
* feat: add postHogNoMask SwiftUI view modifier * chore: update CHANGELOG * fix: improve SwiftUI view tagging * fix: lint * fix: remove swifltint build script
- Loading branch information
Showing
9 changed files
with
527 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// | ||
// PostHogMaskViewModifier.swift | ||
// PostHog | ||
// | ||
// Created by Yiannis Josephides on 09/10/2024. | ||
// | ||
|
||
#if os(iOS) && canImport(SwiftUI) | ||
|
||
import SwiftUI | ||
|
||
public extension View { | ||
/** | ||
Marks a SwiftUI View to be masked in PostHog session replay recordings. | ||
|
||
Because of the nature of how we intercept SwiftUI view hierarchy (and how it maps to UIKit), | ||
we can't always be 100% confident that a view should be masked and may accidentally mark a | ||
sensitive view as non-sensitive instead. | ||
|
||
Use this modifier to explicitly mask sensitive views in session replay recordings. | ||
|
||
For example: | ||
```swift | ||
// This view will be masked in recordings | ||
SensitiveDataView() | ||
.postHogMask() | ||
|
||
// Conditionally mask based on a flag | ||
SensitiveDataView() | ||
.postHogMask(shouldMask) | ||
``` | ||
|
||
- Parameter isEnabled: Whether masking should be enabled. Defaults to true. | ||
- Returns: A modified view that will be masked in session replay recordings when enabled | ||
*/ | ||
func postHogMask(_ isEnabled: Bool = true) -> some View { | ||
modifier( | ||
PostHogTagViewModifier { uiViews in | ||
uiViews.forEach { $0.postHogNoCapture = isEnabled } | ||
} onRemove: { uiViews in | ||
uiViews.forEach { $0.postHogNoCapture = false } | ||
} | ||
) | ||
} | ||
} | ||
|
||
extension UIView { | ||
var postHogNoCapture: Bool { | ||
get { objc_getAssociatedObject(self, &AssociatedKeys.phNoCapture) as? Bool ?? false } | ||
set { objc_setAssociatedObject(self, &AssociatedKeys.phNoCapture, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC) } | ||
} | ||
} | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// | ||
// PostHogNoMaskViewModifier.swift | ||
// PostHog | ||
// | ||
// Created by Yiannis Josephides on 09/10/2024. | ||
// | ||
|
||
#if os(iOS) && canImport(SwiftUI) | ||
|
||
import SwiftUI | ||
|
||
public extension View { | ||
/** | ||
Marks a SwiftUI View to be excluded from masking in PostHog session replay recordings. | ||
|
||
There are cases where PostHog SDK will unintentionally mask some SwiftUI views. | ||
|
||
Because of the nature of how we intercept SwiftUI view hierarchy (and how it maps to UIKit), | ||
we can't always be 100% confident that a view should be masked. For that reason, we prefer to | ||
take a proactive and prefer to mask views if we're not sure. | ||
|
||
Use this modifier to prevent views from being masked in session replay recordings. | ||
|
||
For example: | ||
```swift | ||
// This view may be accidentally masked by PostHog SDK | ||
SomeSafeView() | ||
|
||
// This custom view (and all its subviews) will not be masked in recordings | ||
SomeSafeView() | ||
.postHogNoMask() | ||
``` | ||
|
||
- Returns: A modified view that will not be masked in session replay recordings | ||
*/ | ||
func postHogNoMask() -> some View { | ||
modifier( | ||
PostHogTagViewModifier { uiViews in | ||
uiViews.forEach { $0.postHogNoMask = true } | ||
} onRemove: { uiViews in | ||
uiViews.forEach { $0.postHogNoMask = false } | ||
} | ||
) | ||
} | ||
} | ||
|
||
extension UIView { | ||
var postHogNoMask: Bool { | ||
get { objc_getAssociatedObject(self, &AssociatedKeys.phNoMask) as? Bool ?? false } | ||
set { objc_setAssociatedObject(self, &AssociatedKeys.phNoMask, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC) } | ||
} | ||
} | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.