Skip to content

Commit

Permalink
fix(utils): crash when dealing with colors from storyboard
Browse files Browse the repository at this point in the history
  • Loading branch information
ioannisj committed Nov 4, 2024
1 parent 4524c42 commit 90dccb5
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 8 deletions.
4 changes: 4 additions & 0 deletions PostHog.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@
69F5181A2BAC81FC00F52C14 /* UITextInputTraits+Util.swift in Sources */ = {isa = PBXBuildFile; fileRef = 69F518192BAC81FC00F52C14 /* UITextInputTraits+Util.swift */; };
69F518382BB2BA0100F52C14 /* PostHogSwizzler.swift in Sources */ = {isa = PBXBuildFile; fileRef = 69F518372BB2BA0100F52C14 /* PostHogSwizzler.swift */; };
69F5183A2BB2BA8300F52C14 /* UIApplicationTracker.swift in Sources */ = {isa = PBXBuildFile; fileRef = 69F518392BB2BA8300F52C14 /* UIApplicationTracker.swift */; };
DAB80B252CD8D88A009288AF /* Colors.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = DAB80B242CD8D88A009288AF /* Colors.xcassets */; };
DAD5DD0C2CB6DEF30087387B /* PostHogMaskViewModifier.swift in Sources */ = {isa = PBXBuildFile; fileRef = DAD5DD072CB6DEE70087387B /* PostHogMaskViewModifier.swift */; };
/* End PBXBuildFile section */

Expand Down Expand Up @@ -376,6 +377,7 @@
69F518192BAC81FC00F52C14 /* UITextInputTraits+Util.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UITextInputTraits+Util.swift"; sourceTree = "<group>"; };
69F518372BB2BA0100F52C14 /* PostHogSwizzler.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PostHogSwizzler.swift; sourceTree = "<group>"; };
69F518392BB2BA8300F52C14 /* UIApplicationTracker.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UIApplicationTracker.swift; sourceTree = "<group>"; };
DAB80B242CD8D88A009288AF /* Colors.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Colors.xcassets; sourceTree = "<group>"; };
DAD5DD072CB6DEE70087387B /* PostHogMaskViewModifier.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PostHogMaskViewModifier.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

Expand Down Expand Up @@ -729,6 +731,7 @@
69F518032BAC768300F52C14 /* Assets.xcassets */,
69F518052BAC768300F52C14 /* LaunchScreen.storyboard */,
69F518082BAC768300F52C14 /* Info.plist */,
DAB80B242CD8D88A009288AF /* Colors.xcassets */,
);
path = PostHogExampleStoryboard;
sourceTree = "<group>";
Expand Down Expand Up @@ -1054,6 +1057,7 @@
buildActionMask = 2147483647;
files = (
69F518042BAC768300F52C14 /* Assets.xcassets in Resources */,
DAB80B252CD8D88A009288AF /* Colors.xcassets in Resources */,
69F518072BAC768300F52C14 /* Base in Resources */,
69F518022BAC768100F52C14 /* Base in Resources */,
);
Expand Down
8 changes: 7 additions & 1 deletion PostHog/Replay/CGColor+Util.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,13 @@

extension CGColor {
func toRGBString() -> String? {
guard let components = components, components.count >= 3 else {
// see dicussion: https://github.com/PostHog/posthog-ios/issues/226
// Allow only CGColors with an intiialized value of `numberOfComponents` with a value in 3...4 range
// Loading dynamic colors from storyboard sometimes leads to some random values for numberOfComponents like `105553118884896` which crashes the app
guard
3 ... 4 ~= numberOfComponents, // check range
let components = components, // we now assume it's safe to access `components`
components.count >= 3 else {
return nil
}

Expand Down
6 changes: 3 additions & 3 deletions PostHogExampleStoryboard/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
config.debug = true
config.sendFeatureFlagEvent = false
config.sessionReplay = true
config.sessionReplayConfig.maskAllTextInputs = true
config.sessionReplayConfig.screenshotMode = true
config.sessionReplayConfig.maskAllImages = true
config.sessionReplayConfig.screenshotMode = false
config.sessionReplayConfig.maskAllTextInputs = false
config.sessionReplayConfig.maskAllImages = false
config.sessionReplayConfig.captureNetworkTelemetry = true

PostHogSDK.shared.setup(config)
Expand Down
30 changes: 28 additions & 2 deletions PostHogExampleStoryboard/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="32700.99.1234" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES" initialViewController="BYZ-38-t0r">
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="23094" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES" initialViewController="BYZ-38-t0r">
<device id="retina6_12" orientation="portrait" appearance="light"/>
<dependencies>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="22684"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="23084"/>
<capability name="Named colors" minToolsVersion="9.0"/>
<capability name="Safe area layout guides" minToolsVersion="9.0"/>
<capability name="System colors in document resources" minToolsVersion="11.0"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
Expand All @@ -16,8 +17,30 @@
<view key="view" contentMode="scaleToFill" id="8bC-Xf-vdC">
<rect key="frame" x="0.0" y="0.0" width="393" height="852"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<button opaque="NO" contentMode="scaleToFill" misplaced="YES" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="system" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="ZVx-EC-V8A">
<rect key="frame" x="158" y="408.66666666666669" width="75" height="35"/>
<constraints>
<constraint firstAttribute="height" constant="35" id="N5d-Oe-rDb"/>
<constraint firstAttribute="width" constant="75" id="ghp-rf-JdZ"/>
</constraints>
<state key="normal" title="Button"/>
<buttonConfiguration key="configuration" style="plain" title="Button">
<color key="baseForegroundColor" name="PostHogGray"/>
</buttonConfiguration>
<userDefinedRuntimeAttributes>
<userDefinedRuntimeAttribute type="color" keyPath="layer.borderColor">
<color key="value" name="PostHogGray"/>
</userDefinedRuntimeAttribute>
</userDefinedRuntimeAttributes>
</button>
</subviews>
<viewLayoutGuide key="safeArea" id="6Tk-OE-BBY"/>
<color key="backgroundColor" systemColor="systemBackgroundColor"/>
<constraints>
<constraint firstItem="ZVx-EC-V8A" firstAttribute="centerX" secondItem="8bC-Xf-vdC" secondAttribute="centerX" id="8gS-lA-pIc"/>
<constraint firstItem="ZVx-EC-V8A" firstAttribute="centerY" secondItem="8bC-Xf-vdC" secondAttribute="centerY" id="Wyr-6F-6UJ"/>
</constraints>
</view>
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="dkx-z0-nzr" sceneMemberID="firstResponder"/>
Expand All @@ -26,6 +49,9 @@
</scene>
</scenes>
<resources>
<namedColor name="PostHogGray">
<color white="0.18823529779911041" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
</namedColor>
<systemColor name="systemBackgroundColor">
<color white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
</systemColor>
Expand Down
6 changes: 6 additions & 0 deletions PostHogExampleStoryboard/Colors.xcassets/Contents.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"info" : {
"author" : "xcode",
"version" : 1
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"colors" : [
{
"color" : {
"color-space" : "gray-gamma-22",
"components" : {
"alpha" : "1.000",
"white" : "200"
}
},
"idiom" : "universal"
},
{
"appearances" : [
{
"appearance" : "luminosity",
"value" : "dark"
}
],
"color" : {
"color-space" : "gray-gamma-22",
"components" : {
"alpha" : "1.000",
"white" : "97"
}
},
"idiom" : "universal"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}
4 changes: 2 additions & 2 deletions PostHogExampleStoryboard/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,12 @@ class ViewController: UIViewController {
await fetch(url: url, imageView: imageView)
}

view.addSubview(imageView)
// view.addSubview(imageView)

let textView = UITextView(frame: CGRect(x: 5, y: 105, width: 100, height: 20))
textView.text = "test"

view.addSubview(textView)
// view.addSubview(textView)
}

func fetch(url: URL, imageView: UIImageView) async {
Expand Down

0 comments on commit 90dccb5

Please sign in to comment.