Skip to content

Commit

Permalink
fix: handle zero touch coordinates (#256)
Browse files Browse the repository at this point in the history
* fix: handle zero touch coordinates

* chore: update CHANGELOG.md
  • Loading branch information
ioannisj authored Nov 19, 2024
1 parent cccf985 commit d7ee960
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## Next

- fix: reading screen size could sometimes lead to a deadlock ([#252](https://github.com/PostHog/posthog-ios/pull/252))
- fix: avoid zero touch locations ([#256](https://github.com/PostHog/posthog-ios/pull/256))

## 3.15.3 - 2024-11-18

Expand Down
20 changes: 16 additions & 4 deletions PostHog/Replay/UIApplicationTracker.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,16 @@
return
}

PostHogReplayIntegration.dispatchQueue.async {
// capture necessary touch information on the main thread before performing any asynchronous operations
// - this ensures that UITouch associated objects like UIView, UIWindow, or [UIGestureRecognizer] are still valid.
// - these objects may be released or erased by the system if accessed asynchronously, resulting in invalid/zeroed-out touch coordinates
let touchInfo = touches.map {
(phase: $0.phase, location: $0.location(in: window))
}

PostHogReplayIntegration.dispatchQueue.async { [touchInfo] in
var snapshotsData: [Any] = []
for touch in touches {
for touch in touchInfo {
let phase = touch.phase

let type: Int
Expand All @@ -65,8 +72,13 @@
continue
}

let posX = Int(touch.location(in: window).x)
let posY = Int(touch.location(in: window).y)
// we keep a failsafe here just in case, but this will likely never be triggered
guard touch.location != .zero else {
continue
}

let posX = Int(touch.location.x)
let posY = Int(touch.location.y)

// if the id is 0, BE transformer will set it to the virtual bodyId
let touchData: [String: Any] = ["id": 0, "pointerType": 2, "source": 2, "type": type, "x": posX, "y": posY]
Expand Down

0 comments on commit d7ee960

Please sign in to comment.