Skip to content

Commit 7323a04

Browse files
committed
fix: safely convert floats to int
1 parent 35d7b93 commit 7323a04

File tree

5 files changed

+44
-20
lines changed

5 files changed

+44
-20
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
## Next
22

3+
- fix: occasional crash when converting to Int in session replay wireframe ([#294](https://github.com/PostHog/posthog-ios/pull/294))
4+
35
## 3.19.3 - 2025-02-04
46

57
- fix: custom hosts with a path ([#290](https://github.com/PostHog/posthog-ios/pull/290))

PostHog.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,7 @@
277277
DAC699D62CC790D9000D1D6B /* PostHogAutocaptureIntegration.swift in Sources */ = {isa = PBXBuildFile; fileRef = DAC699D52CC790D9000D1D6B /* PostHogAutocaptureIntegration.swift */; };
278278
DAC699EC2CCA73E5000D1D6B /* ForwardingPickerViewDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = DAC699EB2CCA73E5000D1D6B /* ForwardingPickerViewDelegate.swift */; };
279279
DACF6D5D2CD2F5BC00F14133 /* PostHogAutocaptureIntegrationSpec.swift in Sources */ = {isa = PBXBuildFile; fileRef = DACF6D5C2CD2F5BC00F14133 /* PostHogAutocaptureIntegrationSpec.swift */; };
280+
DAD39DF42D560818002A1A69 /* Float+.swift in Sources */ = {isa = PBXBuildFile; fileRef = DAD39DEE2D560813002A1A69 /* Float+.swift */; };
280281
DAD5DD0C2CB6DEF30087387B /* PostHogMaskViewModifier.swift in Sources */ = {isa = PBXBuildFile; fileRef = DAD5DD072CB6DEE70087387B /* PostHogMaskViewModifier.swift */; };
281282
DAF79A2A2D1309C00078A3C9 /* TestError.swift in Sources */ = {isa = PBXBuildFile; fileRef = DAF79A242D1309BE0078A3C9 /* TestError.swift */; };
282283
DAF95F512D072F04001E82BB /* UIImage+WebP.swift in Sources */ = {isa = PBXBuildFile; fileRef = DAF95F502D072F04001E82BB /* UIImage+WebP.swift */; };
@@ -714,6 +715,7 @@
714715
DAC699D52CC790D9000D1D6B /* PostHogAutocaptureIntegration.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PostHogAutocaptureIntegration.swift; sourceTree = "<group>"; };
715716
DAC699EB2CCA73E5000D1D6B /* ForwardingPickerViewDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ForwardingPickerViewDelegate.swift; sourceTree = "<group>"; };
716717
DACF6D5C2CD2F5BC00F14133 /* PostHogAutocaptureIntegrationSpec.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PostHogAutocaptureIntegrationSpec.swift; sourceTree = "<group>"; };
718+
DAD39DEE2D560813002A1A69 /* Float+.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Float+.swift"; sourceTree = "<group>"; };
717719
DAD5DD072CB6DEE70087387B /* PostHogMaskViewModifier.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PostHogMaskViewModifier.swift; sourceTree = "<group>"; };
718720
DAF79A242D1309BE0078A3C9 /* TestError.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TestError.swift; sourceTree = "<group>"; };
719721
DAF95F502D072F04001E82BB /* UIImage+WebP.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UIImage+WebP.swift"; sourceTree = "<group>"; };
@@ -1053,6 +1055,7 @@
10531055
69EE82B82BA9C4DA00EB9542 /* Replay */ = {
10541056
isa = PBXGroup;
10551057
children = (
1058+
DAD39DEE2D560813002A1A69 /* Float+.swift */,
10561059
69B7F6062CF7702D00A48BCC /* UIImage+Util.swift */,
10571060
69EE82B92BA9C50400EB9542 /* PostHogReplayIntegration.swift */,
10581061
69EE82BB2BA9C53000EB9542 /* PostHogSessionReplayConfig.swift */,
@@ -1779,6 +1782,7 @@
17791782
DA9CE3572D3108AD00DFE652 /* yuv_sse2.c in Sources */,
17801783
DA9CE3582D3108AD00DFE652 /* frame_enc.c in Sources */,
17811784
DA9CE3592D3108AD00DFE652 /* cost_neon.c in Sources */,
1785+
DAD39DF42D560818002A1A69 /* Float+.swift in Sources */,
17821786
DA9CE35A2D3108AD00DFE652 /* enc_sse41.c in Sources */,
17831787
DA9CE35B2D3108AD00DFE652 /* webp_enc.c in Sources */,
17841788
DA9CE35C2D3108AD00DFE652 /* upsampling_neon.c in Sources */,

PostHog/Replay/Float+.swift

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//
2+
// Float+.swift
3+
// PostHog
4+
//
5+
// Created by Yiannis Josephides on 07/02/2025.
6+
//
7+
8+
extension CGFloat {
9+
func toInt() -> Int {
10+
NSNumber(value: rounded()).intValue
11+
}
12+
}
13+
14+
extension Double {
15+
func toInt() -> Int {
16+
NSNumber(value: rounded()).intValue
17+
}
18+
}

PostHog/Replay/PostHogReplayIntegration.swift

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -176,8 +176,8 @@
176176

177177
if !snapshotStatus.sentMetaEvent {
178178
let size = window.bounds.size
179-
let width = Int(size.width)
180-
let height = Int(size.height)
179+
let width = size.width.toInt()
180+
let height = size.height.toInt()
181181

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

@@ -229,10 +229,10 @@
229229
}
230230

231231
private func setPadding(_ insets: UIEdgeInsets, _ style: RRStyle) {
232-
style.paddingTop = Int(insets.top)
233-
style.paddingRight = Int(insets.right)
234-
style.paddingBottom = Int(insets.bottom)
235-
style.paddingLeft = Int(insets.left)
232+
style.paddingTop = insets.top.toInt()
233+
style.paddingRight = insets.right.toInt()
234+
style.paddingBottom = insets.bottom.toInt()
235+
style.paddingLeft = insets.left.toInt()
236236
}
237237

238238
private func createBasicWireframe(_ view: UIView) -> RRWireframe {
@@ -244,10 +244,10 @@
244244
let frame = view.toAbsoluteRect(view.window)
245245

246246
wireframe.id = view.hash
247-
wireframe.posX = Int(frame.origin.x)
248-
wireframe.posY = Int(frame.origin.y)
249-
wireframe.width = Int(frame.size.width)
250-
wireframe.height = Int(frame.size.height)
247+
wireframe.posX = frame.origin.x.toInt()
248+
wireframe.posY = frame.origin.y.toInt()
249+
wireframe.width = frame.size.width.toInt()
250+
wireframe.height = frame.size.height.toInt()
251251

252252
return wireframe
253253
}
@@ -528,8 +528,8 @@
528528
wireframe.disabled = !textView.isEditable
529529
style.color = textView.textColor?.toRGBString()
530530
style.fontFamily = textView.font?.familyName
531-
if let fontSize = textView.font?.pointSize {
532-
style.fontSize = Int(fontSize)
531+
if let fontSize = textView.font?.pointSize.toInt() {
532+
style.fontSize = fontSize
533533
}
534534
setAlignment(textView.textAlignment, style)
535535
setPadding(textView.textContainerInset, style)
@@ -549,8 +549,8 @@
549549
wireframe.disabled = !textField.isEnabled
550550
style.color = textField.textColor?.toRGBString()
551551
style.fontFamily = textField.font?.familyName
552-
if let fontSize = textField.font?.pointSize {
553-
style.fontSize = Int(fontSize)
552+
if let fontSize = textField.font?.pointSize.toInt() {
553+
style.fontSize = fontSize
554554
}
555555
setAlignment(textField.textAlignment, style)
556556
}
@@ -601,8 +601,8 @@
601601
wireframe.disabled = !label.isEnabled
602602
style.color = label.textColor?.toRGBString()
603603
style.fontFamily = label.font?.familyName
604-
if let fontSize = label.font?.pointSize {
605-
style.fontSize = Int(fontSize)
604+
if let fontSize = label.font?.pointSize.toInt() {
605+
style.fontSize = fontSize
606606
}
607607
setAlignment(label.textAlignment, style)
608608
}
@@ -631,8 +631,8 @@
631631

632632
style.backgroundColor = view.backgroundColor?.toRGBString()
633633
let layer = view.layer
634-
style.borderWidth = Int(layer.borderWidth)
635-
style.borderRadius = Int(layer.cornerRadius)
634+
style.borderWidth = layer.borderWidth.toInt()
635+
style.borderRadius = layer.cornerRadius.toInt()
636636
style.borderColor = layer.borderColor?.toRGBString()
637637

638638
wireframe.style = style

PostHog/Replay/UIApplicationTracker.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,8 @@
8383
continue
8484
}
8585

86-
let posX = Int(touch.location.x)
87-
let posY = Int(touch.location.y)
86+
let posX = touch.location.x.toInt()
87+
let posY = touch.location.y.toInt()
8888

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

0 commit comments

Comments
 (0)