Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

recording: fix RN iOS masking #230

Merged
merged 6 commits into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
## Next

- recording: fix RN iOS masking ([#230](https://github.com/PostHog/posthog-ios/pull/230))

## 3.14.0 - 2024-11-05

- add option to pass a custom timestamp when calling capture() ([#228](https://github.com/PostHog/posthog-ios/pull/228))
Expand Down
49 changes: 39 additions & 10 deletions PostHog/Replay/PostHogReplayIntegration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@
private let swiftUIGenericTypes = ["_TtCOCV7SwiftUI11DisplayList11ViewUpdater8Platform13CGDrawingView",
"_TtC7SwiftUIP33_A34643117F00277B93DEBAB70EC0697122_UIShapeHitTestingView"].compactMap { NSClassFromString($0) }

private let reactNativeTextView: AnyClass? = NSClassFromString("RCTTextView")
private let reactNativeImageView: AnyClass? = NSClassFromString("RCTImageView")

static let dispatchQueue = DispatchQueue(label: "com.posthog.PostHogReplayIntegration",
target: .global(qos: .utility))

Expand Down Expand Up @@ -97,7 +100,7 @@

var data: [String: Any] = ["width": width, "height": height]

if let screenName {
if let screenName = screenName {
data["href"] = screenName
}

Expand Down Expand Up @@ -155,7 +158,7 @@
return wireframe
}

private func findMaskableWidgets(_ view: UIView, _ parent: UIView, _ maskableWidgets: inout [CGRect]) {
private func findMaskableWidgets(_ view: UIView, _ parent: UIView, _ maskableWidgets: inout [CGRect], _ maskChildren: inout Bool) {
if let textView = view as? UITextView { // TextEditor, SwiftUI.TextEditorTextView, SwiftUI.UIKitTextView
if isTextViewSensitive(textView) {
maskableWidgets.append(view.toAbsoluteRect(parent))
Expand All @@ -170,13 +173,27 @@
}
}

if let reactNativeTextView = reactNativeTextView {
if view.isKind(of: reactNativeTextView), config.sessionReplayConfig.maskAllTextInputs {
maskableWidgets.append(view.toAbsoluteRect(parent))
return
}
}

if let image = view as? UIImageView { // Image, this code might never be reachable in SwiftUI, see swiftUIImageTypes instead
if isImageViewSensitive(image) {
maskableWidgets.append(view.toAbsoluteRect(parent))
return
}
}

if let reactNativeImageView = reactNativeImageView {
if view.isKind(of: reactNativeImageView), config.sessionReplayConfig.maskAllImages {
maskableWidgets.append(view.toAbsoluteRect(parent))
return
}
}

if let label = view as? UILabel { // Text, this code might never be reachable in SwiftUI, see swiftUIImageTypes instead
if isLabelSensitive(label) {
maskableWidgets.append(view.toAbsoluteRect(parent))
Expand Down Expand Up @@ -232,9 +249,18 @@
}
}

// manually masked views through view modifier `PostHogMaskViewModifier`
if view.phIsManuallyMasked {
marandaneto marked this conversation as resolved.
Show resolved Hide resolved
maskableWidgets.append(view.toAbsoluteRect(parent))
// on RN, lots get converted to RCTRootContentView, RCTRootView, RCTView and sometimes its just the whole screen, we dont want to mask
// in such cases
if view.isNoCapture() || maskChildren {
let viewRect = view.toAbsoluteRect(parent)
let parentRect = parent.frame

// Check if the rectangles do not match
if !viewRect.equalTo(parentRect) {
maskableWidgets.append(view.toAbsoluteRect(parent))
} else {
maskChildren = true
ioannisj marked this conversation as resolved.
Show resolved Hide resolved
}
}

if !view.subviews.isEmpty {
Expand All @@ -243,9 +269,10 @@
continue
}

findMaskableWidgets(child, parent, &maskableWidgets)
findMaskableWidgets(child, parent, &maskableWidgets, &maskChildren)
}
}
maskChildren = false
}

private func toScreenshotWireframe(_ view: UIView) -> RRWireframe? {
Expand All @@ -254,7 +281,8 @@
}

var maskableWidgets: [CGRect] = []
findMaskableWidgets(view, view, &maskableWidgets)
var maskChildren = false
findMaskableWidgets(view, view, &maskableWidgets, &maskChildren)

let wireframe = createBasicWireframe(view)

Expand Down Expand Up @@ -311,11 +339,11 @@
}

private func hasText(_ text: String?) -> Bool {
if let text, !text.isEmpty {
true
if let text = text, !text.isEmpty {
marandaneto marked this conversation as resolved.
Show resolved Hide resolved
return true
} else {
// if there's no text, there's nothing to mask
false
return false
}
}

Expand Down Expand Up @@ -534,6 +562,7 @@
private protocol AnyObjectUIHostingViewController: AnyObject {}

extension UIHostingController: AnyObjectUIHostingViewController {}

#endif

// swiftlint:enable cyclomatic_complexity
Loading