From c7bedac48269ec3d54227075eb3bd1fab67d7557 Mon Sep 17 00:00:00 2001 From: bitsandfoxes Date: Wed, 11 Nov 2026 11:57:34 +0100 Subject: [PATCH 1/6] Attach screenshots to iOS native crashes with runtime initialization --- CHANGELOG.md | 1 + src/Sentry.Unity.iOS/SentryCocoaBridgeProxy.cs | 14 +++++++++----- src/Sentry.Unity.iOS/SentryNativeCocoa.cs | 4 ++-- .../Sentry.Unity.iOS.Tests/SentryNativeIosTests.cs | 11 +++++++++++ 4 files changed, 23 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4c3e47a20..11ac63375 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ ### Fixes +- On iOS, native crashes come with a screenshot when `AttachScreenshot` is enabled, also when the native SDK is initialized at runtime, the default. ([#PR](https://github.com/getsentry/sentry-unity/pull/PR)) - The Event Sample Rate in the Sentry window now accepts values from 0.01 to 1.0. A rate of 0 made the SDK fail to initialize and builds fail to load the options. ([#2929](https://github.com/getsentry/sentry-unity/pull/2929)) - On iOS, development builds now respect `UploadDevelopmentSymbols` and no longer upload debug symbols unless it is enabled, matching the other platforms. ([#2928](https://github.com/getsentry/sentry-unity/pull/2928)) diff --git a/src/Sentry.Unity.iOS/SentryCocoaBridgeProxy.cs b/src/Sentry.Unity.iOS/SentryCocoaBridgeProxy.cs index dced1a8ab..2c3bd3265 100644 --- a/src/Sentry.Unity.iOS/SentryCocoaBridgeProxy.cs +++ b/src/Sentry.Unity.iOS/SentryCocoaBridgeProxy.cs @@ -1,6 +1,7 @@ using System; using System.Runtime.InteropServices; using Sentry.Extensibility; +using UnityEngine; namespace Sentry.Unity.iOS; @@ -16,7 +17,7 @@ internal static class SentryCocoaBridgeProxy private static IDiagnosticLogger? Logger; public static bool IsEnabled() => SentryNativeBridgeIsEnabled() == 1; - public static bool Init(SentryUnityOptions options) + public static bool Init(SentryUnityOptions options, RuntimePlatform platform) { if (LoadLibrary() != 1) { @@ -52,10 +53,9 @@ public static bool Init(SentryUnityOptions options) Logger?.LogDebug("Setting SendDefaultPii: {0}", options.SendDefaultPii); OptionsSetInt(cOptions, "sendDefaultPii", options.SendDefaultPii ? 1 : 0); - // macOS screenshots currently don't work, because there's no UIKit. Cocoa logs: "Sentry - info:: NO UIKit" - // Logger?.LogDebug("Setting AttachScreenshot: {0}", options.AttachScreenshot); - // OptionsSetInt(cOptions, "attachScreenshot", options.AttachScreenshot ? 1 : 0); - OptionsSetInt(cOptions, "attachScreenshot", 0); + var attachScreenshot = ShouldAttachScreenshot(options, platform); + Logger?.LogDebug("Setting AttachScreenshot: {0}", attachScreenshot); + OptionsSetInt(cOptions, "attachScreenshot", attachScreenshot ? 1 : 0); Logger?.LogDebug("Setting MaxBreadcrumbs: {0}", options.MaxBreadcrumbs); OptionsSetInt(cOptions, "maxBreadcrumbs", options.MaxBreadcrumbs); @@ -91,6 +91,10 @@ public static bool Init(SentryUnityOptions options) return result is 1; } + // sentry-cocoa takes screenshots through UIKit, which macOS doesn't have. + internal static bool ShouldAttachScreenshot(SentryUnityOptions options, RuntimePlatform platform) => + platform == RuntimePlatform.IPhonePlayer && options.AttachScreenshot; + [DllImport("__Internal", EntryPoint = "SentryNativeBridgeLoadLibrary")] private static extern int LoadLibrary(); diff --git a/src/Sentry.Unity.iOS/SentryNativeCocoa.cs b/src/Sentry.Unity.iOS/SentryNativeCocoa.cs index ea913e04d..6853a0074 100644 --- a/src/Sentry.Unity.iOS/SentryNativeCocoa.cs +++ b/src/Sentry.Unity.iOS/SentryNativeCocoa.cs @@ -45,7 +45,7 @@ internal static void Configure(SentryUnityOptions options, RuntimePlatform platf "Make sure the app initializes Sentry before the game starts. Skipping native support."); return; } - else if (!SentryCocoaBridgeProxy.Init(options)) + else if (!SentryCocoaBridgeProxy.Init(options, platform)) { Logger?.LogWarning("Failed to initialize the native SDK"); return; @@ -72,7 +72,7 @@ internal static void Configure(SentryUnityOptions options, RuntimePlatform platf } else { - if (!SentryCocoaBridgeProxy.Init(options)) + if (!SentryCocoaBridgeProxy.Init(options, platform)) { Logger?.LogWarning("Failed to initialize the native SDK"); return; diff --git a/test/Sentry.Unity.iOS.Tests/SentryNativeIosTests.cs b/test/Sentry.Unity.iOS.Tests/SentryNativeIosTests.cs index eac8b89de..5e2415a0b 100644 --- a/test/Sentry.Unity.iOS.Tests/SentryNativeIosTests.cs +++ b/test/Sentry.Unity.iOS.Tests/SentryNativeIosTests.cs @@ -48,4 +48,15 @@ public void Configure_NativeSupportDisabled_macOS() Assert.Null(options.CrashedLastRun); Assert.False(options.EnableScopeSync); } + + [TestCase(RuntimePlatform.IPhonePlayer, true, true)] + [TestCase(RuntimePlatform.IPhonePlayer, false, false)] + [TestCase(RuntimePlatform.OSXPlayer, true, false)] + [TestCase(RuntimePlatform.OSXPlayer, false, false)] + public void ShouldAttachScreenshot_FollowsOptionOnIosOnly(RuntimePlatform platform, bool attachScreenshot, bool expected) + { + var options = new SentryUnityOptions { AttachScreenshot = attachScreenshot }; + + Assert.AreEqual(expected, SentryCocoaBridgeProxy.ShouldAttachScreenshot(options, platform)); + } } From 8f0d32af8ed92db857a49783640cf2fd3f1f2963 Mon Sep 17 00:00:00 2001 From: bitsandfoxes Date: Wed, 11 Nov 2026 12:00:40 +0100 Subject: [PATCH 2/6] Keep sentry-cocoa screenshots to native crashes --- package-dev/Plugins/iOS/SentryNativeBridge.m | 3 +++ 1 file changed, 3 insertions(+) diff --git a/package-dev/Plugins/iOS/SentryNativeBridge.m b/package-dev/Plugins/iOS/SentryNativeBridge.m index a28ff3e53..3446a01cf 100644 --- a/package-dev/Plugins/iOS/SentryNativeBridge.m +++ b/package-dev/Plugins/iOS/SentryNativeBridge.m @@ -48,6 +48,9 @@ dictOptions[@"sdk"] = @ { @"name" : @"sentry.cocoa.unity" }; dictOptions[@"enableAutoSessionTracking"] = @NO; dictOptions[@"enableAppHangTracking"] = @NO; + // Screenshots for C# events come from the Unity SDK. With `attachScreenshot`, sentry-cocoa attaches one + // only to native crashes: the crash-time screenshot doesn't call this block. + dictOptions[@"beforeCaptureScreenshot"] = ^BOOL(id event) { return NO; }; return CFBridgingRetain(dictOptions); } From 0ad28ea7cd5adf75cdaa31b63d88ed966187f420 Mon Sep 17 00:00:00 2001 From: bitsandfoxes Date: Wed, 11 Nov 2026 12:02:41 +0100 Subject: [PATCH 3/6] Keep sentry-cocoa screenshots to native crashes with build-time initialization --- src/Sentry.Unity.Editor.iOS/NativeOptions.cs | 3 +++ test/Sentry.Unity.Editor.iOS.Tests/NativeOptionsTests.cs | 8 ++++++++ 2 files changed, 11 insertions(+) diff --git a/src/Sentry.Unity.Editor.iOS/NativeOptions.cs b/src/Sentry.Unity.Editor.iOS/NativeOptions.cs index 8522eeb12..9ed683070 100644 --- a/src/Sentry.Unity.Editor.iOS/NativeOptions.cs +++ b/src/Sentry.Unity.Editor.iOS/NativeOptions.cs @@ -35,6 +35,9 @@ internal static string Generate(SentryUnityOptions options) @""failedRequestStatusCodes"" : @[{failedRequestStatusCodesArray}], @""sendDefaultPii"" : @{ToObjCString(options.SendDefaultPii)}, @""attachScreenshot"" : @{ToObjCString(options.AttachScreenshot)}, + // Screenshots for C# events come from the Unity SDK. sentry-cocoa attaches one only to native crashes, + // as its crash-time screenshot doesn't call this block. + @""beforeCaptureScreenshot"" : ^BOOL(id event) {{ return NO; }}, @""release"" : @""{options.Release}"", @""environment"" : @""{options.Environment}"", @""enableNetworkBreadcrumbs"" : @NO, diff --git a/test/Sentry.Unity.Editor.iOS.Tests/NativeOptionsTests.cs b/test/Sentry.Unity.Editor.iOS.Tests/NativeOptionsTests.cs index fea49a8b1..48fb828c2 100644 --- a/test/Sentry.Unity.Editor.iOS.Tests/NativeOptionsTests.cs +++ b/test/Sentry.Unity.Editor.iOS.Tests/NativeOptionsTests.cs @@ -89,6 +89,14 @@ public void CreateOptionsFile_AppHangTimeout_WrittenAsSeconds() StringAssert.Contains("@\"appHangTimeoutInterval\": @7.5", nativeOptions); } + [Test] + public void CreateOptionsFile_BeforeCaptureScreenshot_SkipsNonFatalEvents() + { + var nativeOptions = NativeOptions.Generate(new SentryUnityOptions { AttachScreenshot = true }); + + StringAssert.Contains("@\"beforeCaptureScreenshot\" : ^BOOL(id event) { return NO; }", nativeOptions); + } + [Test] public void CreateOptionsFile_FilterBadGatewayEnabled_AddsFiltering() { From 9d91c777a18895887fce8b29d8a621f6046bb7c2 Mon Sep 17 00:00:00 2001 From: bitsandfoxes Date: Wed, 11 Nov 2026 12:58:32 +0100 Subject: [PATCH 4/6] Skip native-crash screenshots when a screenshot callback is set --- CHANGELOG.md | 2 +- src/Sentry.Unity.Editor.iOS/NativeOptions.cs | 2 +- .../SentryCocoaBridgeProxy.cs | 6 +++++- src/Sentry.Unity/SentryUnityOptions.cs | 6 ++++++ .../NativeOptionsTests.cs | 19 +++++++++++++++++++ .../SentryNativeIosTests.cs | 18 ++++++++++++++++++ 6 files changed, 50 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 11ac63375..0c4cabf64 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,7 +4,7 @@ ### Fixes -- On iOS, native crashes come with a screenshot when `AttachScreenshot` is enabled, also when the native SDK is initialized at runtime, the default. ([#PR](https://github.com/getsentry/sentry-unity/pull/PR)) +- On iOS, native crashes come with a screenshot when `AttachScreenshot` is enabled, also when the native SDK is initialized at runtime, the default. With `SetBeforeCaptureScreenshot` or `SetBeforeSendScreenshot` set, native crashes come without one, as these callbacks can't run during a crash. Events that sentry-cocoa captures itself, such as failed HTTP requests, no longer get a screenshot. ([#PR](https://github.com/getsentry/sentry-unity/pull/PR)) - The Event Sample Rate in the Sentry window now accepts values from 0.01 to 1.0. A rate of 0 made the SDK fail to initialize and builds fail to load the options. ([#2929](https://github.com/getsentry/sentry-unity/pull/2929)) - On iOS, development builds now respect `UploadDevelopmentSymbols` and no longer upload debug symbols unless it is enabled, matching the other platforms. ([#2928](https://github.com/getsentry/sentry-unity/pull/2928)) diff --git a/src/Sentry.Unity.Editor.iOS/NativeOptions.cs b/src/Sentry.Unity.Editor.iOS/NativeOptions.cs index 9ed683070..238cfd413 100644 --- a/src/Sentry.Unity.Editor.iOS/NativeOptions.cs +++ b/src/Sentry.Unity.Editor.iOS/NativeOptions.cs @@ -34,7 +34,7 @@ internal static string Generate(SentryUnityOptions options) @""enableCaptureFailedRequests"": @{ToObjCString(options.CaptureFailedRequests)}, @""failedRequestStatusCodes"" : @[{failedRequestStatusCodesArray}], @""sendDefaultPii"" : @{ToObjCString(options.SendDefaultPii)}, - @""attachScreenshot"" : @{ToObjCString(options.AttachScreenshot)}, + @""attachScreenshot"" : @{ToObjCString(options.AttachNativeCrashScreenshot)}, // Screenshots for C# events come from the Unity SDK. sentry-cocoa attaches one only to native crashes, // as its crash-time screenshot doesn't call this block. @""beforeCaptureScreenshot"" : ^BOOL(id event) {{ return NO; }}, diff --git a/src/Sentry.Unity.iOS/SentryCocoaBridgeProxy.cs b/src/Sentry.Unity.iOS/SentryCocoaBridgeProxy.cs index 2c3bd3265..97d7eb948 100644 --- a/src/Sentry.Unity.iOS/SentryCocoaBridgeProxy.cs +++ b/src/Sentry.Unity.iOS/SentryCocoaBridgeProxy.cs @@ -54,6 +54,10 @@ public static bool Init(SentryUnityOptions options, RuntimePlatform platform) OptionsSetInt(cOptions, "sendDefaultPii", options.SendDefaultPii ? 1 : 0); var attachScreenshot = ShouldAttachScreenshot(options, platform); + if (options.AttachScreenshot && platform == RuntimePlatform.IPhonePlayer && !attachScreenshot) + { + Logger?.LogDebug("Not attaching screenshots to native crashes: a screenshot callback is set, and it can't run during a crash."); + } Logger?.LogDebug("Setting AttachScreenshot: {0}", attachScreenshot); OptionsSetInt(cOptions, "attachScreenshot", attachScreenshot ? 1 : 0); @@ -93,7 +97,7 @@ public static bool Init(SentryUnityOptions options, RuntimePlatform platform) // sentry-cocoa takes screenshots through UIKit, which macOS doesn't have. internal static bool ShouldAttachScreenshot(SentryUnityOptions options, RuntimePlatform platform) => - platform == RuntimePlatform.IPhonePlayer && options.AttachScreenshot; + platform == RuntimePlatform.IPhonePlayer && options.AttachNativeCrashScreenshot; [DllImport("__Internal", EntryPoint = "SentryNativeBridgeLoadLibrary")] private static extern int LoadLibrary(); diff --git a/src/Sentry.Unity/SentryUnityOptions.cs b/src/Sentry.Unity/SentryUnityOptions.cs index 667f02300..40cf4f992 100644 --- a/src/Sentry.Unity/SentryUnityOptions.cs +++ b/src/Sentry.Unity/SentryUnityOptions.cs @@ -409,12 +409,17 @@ internal bool NativeAppHangTrackingEnabled internal Func? BeforeCaptureScreenshotInternal { get; private set; } + // The native SDKs take native-crash screenshots in their crash handler, where these C# callbacks can't run. + internal bool AttachNativeCrashScreenshot => + AttachScreenshot && BeforeCaptureScreenshotInternal is null && BeforeSendScreenshotInternal is null; + /// /// Configures a callback function to be invoked before capturing and attaching a screenshot to an event. /// /// /// This callback will get invoked when the event gets captured, possibly off the main thread, so don't use the Unity /// API in it. If the screenshot should not be taken return `false`. + /// Setting it turns off screenshots for native crashes on iOS, as the callback can't run during a crash. /// public void SetBeforeCaptureScreenshot(Func beforeAttachScreenshot) { @@ -431,6 +436,7 @@ public void SetBeforeCaptureScreenshot(Func beforeAttachScree /// You can modify the texture (blur areas, redact PII, etc.) and return it, or return null to discard. /// It runs once per screenshot. Events captured together share one, and the callback receives the first of them /// that didn't get dropped. + /// Setting it turns off screenshots for native crashes on iOS, as the callback can't run during a crash. /// /// The callback function to invoke before sending screenshots. public void SetBeforeSendScreenshot(Func beforeSendScreenshot) diff --git a/test/Sentry.Unity.Editor.iOS.Tests/NativeOptionsTests.cs b/test/Sentry.Unity.Editor.iOS.Tests/NativeOptionsTests.cs index 48fb828c2..aee5f5d07 100644 --- a/test/Sentry.Unity.Editor.iOS.Tests/NativeOptionsTests.cs +++ b/test/Sentry.Unity.Editor.iOS.Tests/NativeOptionsTests.cs @@ -89,6 +89,25 @@ public void CreateOptionsFile_AppHangTimeout_WrittenAsSeconds() StringAssert.Contains("@\"appHangTimeoutInterval\": @7.5", nativeOptions); } + [Test] + public void CreateOptionsFile_AttachScreenshot_SetsYes() + { + var nativeOptions = NativeOptions.Generate(new SentryUnityOptions { AttachScreenshot = true }); + + StringAssert.Contains("@\"attachScreenshot\" : @YES", nativeOptions); + } + + [Test] + public void CreateOptionsFile_AttachScreenshotWithScreenshotCallback_SetsNo() + { + var options = new SentryUnityOptions { AttachScreenshot = true }; + options.SetBeforeCaptureScreenshot(_ => true); + + var nativeOptions = NativeOptions.Generate(options); + + StringAssert.Contains("@\"attachScreenshot\" : @NO", nativeOptions); + } + [Test] public void CreateOptionsFile_BeforeCaptureScreenshot_SkipsNonFatalEvents() { diff --git a/test/Sentry.Unity.iOS.Tests/SentryNativeIosTests.cs b/test/Sentry.Unity.iOS.Tests/SentryNativeIosTests.cs index 5e2415a0b..2d2f175d3 100644 --- a/test/Sentry.Unity.iOS.Tests/SentryNativeIosTests.cs +++ b/test/Sentry.Unity.iOS.Tests/SentryNativeIosTests.cs @@ -59,4 +59,22 @@ public void ShouldAttachScreenshot_FollowsOptionOnIosOnly(RuntimePlatform platfo Assert.AreEqual(expected, SentryCocoaBridgeProxy.ShouldAttachScreenshot(options, platform)); } + + [Test] + public void ShouldAttachScreenshot_BeforeCaptureScreenshotSet_False() + { + var options = new SentryUnityOptions { AttachScreenshot = true }; + options.SetBeforeCaptureScreenshot(_ => true); + + Assert.IsFalse(SentryCocoaBridgeProxy.ShouldAttachScreenshot(options, RuntimePlatform.IPhonePlayer)); + } + + [Test] + public void ShouldAttachScreenshot_BeforeSendScreenshotSet_False() + { + var options = new SentryUnityOptions { AttachScreenshot = true }; + options.SetBeforeSendScreenshot((screenshot, _) => screenshot); + + Assert.IsFalse(SentryCocoaBridgeProxy.ShouldAttachScreenshot(options, RuntimePlatform.IPhonePlayer)); + } } From 85854037ad60c81675efdbef4f8994dcc73f1112 Mon Sep 17 00:00:00 2001 From: bitsandfoxes Date: Wed, 11 Nov 2026 13:46:45 +0100 Subject: [PATCH 5/6] Link the PR in the CHANGELOG --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0c4cabf64..3849c0b1e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,7 +4,7 @@ ### Fixes -- On iOS, native crashes come with a screenshot when `AttachScreenshot` is enabled, also when the native SDK is initialized at runtime, the default. With `SetBeforeCaptureScreenshot` or `SetBeforeSendScreenshot` set, native crashes come without one, as these callbacks can't run during a crash. Events that sentry-cocoa captures itself, such as failed HTTP requests, no longer get a screenshot. ([#PR](https://github.com/getsentry/sentry-unity/pull/PR)) +- On iOS, native crashes come with a screenshot when `AttachScreenshot` is enabled, also when the native SDK is initialized at runtime, the default. With `SetBeforeCaptureScreenshot` or `SetBeforeSendScreenshot` set, native crashes come without one, as these callbacks can't run during a crash. Events that sentry-cocoa captures itself, such as failed HTTP requests, no longer get a screenshot. ([#2941](https://github.com/getsentry/sentry-unity/pull/2941)) - The Event Sample Rate in the Sentry window now accepts values from 0.01 to 1.0. A rate of 0 made the SDK fail to initialize and builds fail to load the options. ([#2929](https://github.com/getsentry/sentry-unity/pull/2929)) - On iOS, development builds now respect `UploadDevelopmentSymbols` and no longer upload debug symbols unless it is enabled, matching the other platforms. ([#2928](https://github.com/getsentry/sentry-unity/pull/2928)) From 0730acd8348b8d20bfe48f1af09ecbc1173f1145 Mon Sep 17 00:00:00 2001 From: Stefan Jandl Date: Thu, 12 Nov 2026 12:13:02 +0100 Subject: [PATCH 6/6] Apply suggestion from @bitsandfoxes --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 747c25d17..cb0079bdc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,7 +8,7 @@ ### Fixes -- On iOS, native crashes come with a screenshot when `AttachScreenshot` is enabled, also when the native SDK is initialized at runtime, the default. With `SetBeforeCaptureScreenshot` or `SetBeforeSendScreenshot` set, native crashes come without one, as these callbacks can't run during a crash. Events that sentry-cocoa captures itself, such as failed HTTP requests, no longer get a screenshot. ([#2941](https://github.com/getsentry/sentry-unity/pull/2941)) +- iOS native crashes now come with a screenshot when `AttachScreenshot` is enabled. Screenshots captured during a crash skip `SetBeforeCaptureScreenshot` and `SetBeforeSendScreenshot` because these callbacks can't run during a crash. To make sure a screenshot you'd skip or redact isn't sent unredacted, setting either callback turns crash screenshots off. Events that sentry-cocoa captures itself, such as failed HTTP requests, no longer get a screenshot ([#2941](https://github.com/getsentry/sentry-unity/pull/2941)) - Values set in a `SentryCliOptionsConfiguration`, such as an auth token read from an environment variable, no longer get saved into `SentryCliOptions.asset` by the Sentry window after a build. The configuration now runs on a copy of the options. ([#2938](https://github.com/getsentry/sentry-unity/pull/2938)) - The configuration window's tooltips and help boxes and the option docs describe the SDK's defaults and requirements correctly: `Auto Set UserName` is disabled by default, `Attach Screenshots` also attaches screenshots to events on background threads, `Org Slug` is required unless the Auth Token is an organization token, `Scene Traces` has no Unity version requirement, and `IosWatchdogTerminationIntegrationEnabled` turns on reporting of watchdog terminations on iOS. ([#2933](https://github.com/getsentry/sentry-unity/pull/2933)) - The SDK no longer throws a `NullReferenceException` during startup on Android and iOS when it can't load its options, for example when the configuration lives in an OBB file that isn't available yet. ([#2931](https://github.com/getsentry/sentry-unity/pull/2931))