Skip to content

Commit 4e808f5

Browse files
feat(apple): Add UncaughtNSExceptionReporting (#11671)
Add docs for UncaughtNSExceptionReporting, which will be available on Cocoa SDK 8.40.0 and above. Co-authored-by: Karl Heinz Struggl <[email protected]>
1 parent 6a438d2 commit 4e808f5

File tree

1 file changed

+32
-11
lines changed

1 file changed

+32
-11
lines changed

platform-includes/capture-error/apple.mdx

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -103,28 +103,49 @@ The SDK can't install the uncaught exception handler if a debugger is attached.
103103

104104
</Note>
105105

106-
By default, macOS applications do not crash whenever an uncaught exception occurs. To enable this with Sentry:
106+
By default, macOS applications don't crash whenever an uncaught exception occurs. As the Cocoa Frameworks are generally not [exception-safe on macOS](https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/Exceptions/Articles/ExceptionsAndCocoaFrameworks.html), the application could end up in a corrupted state when an uncaught exception occurs. Therefore, we recommend changing your application configuration so it crashes on uncaught NSExceptions. To achieve this, you can use the `SentryCrashExceptionApplication`, or enable the option `enableUncaughtNSExceptionReporting`, available as of version [8.40.0](https://github.com/getsentry/sentry-cocoa/blob/main/CHANGELOG.md#8400). Both options set the `NSApplicationCrashOnExceptions` on the `NSUserDefaults` so your application crashes on uncaught NSExceptions and send the crash report to Sentry.
107+
108+
Once you have configured your application to crash whenever an uncaught exception occurs, the Apple SDK will capture those automatically. You don't need to manually call `captureException`.
109+
110+
### SentryCrashExceptionApplication
111+
112+
<Alert level="warning">
113+
114+
Don't use this option together with the `enableUncaughtNSExceptionReporting` option. Enabling both features can lead to duplicated reports.
115+
116+
</Alert>
117+
118+
To enable this with Sentry:
107119

108120
1. Open the application's `Info.plist` file
109121
2. Search for `Principal class` (the entry is expected to be `NSApplication`)
110122
3. Replace `NSApplication` with `SentryCrashExceptionApplication`
111123

112-
Alternatively, you can set the `NSApplicationCrashOnExceptions` flag:
124+
### Uncaught NSException Reporting Option
113125

114-
```swift {tabTitle:Swift} {4-4}
115-
import Sentry
126+
<Alert level="warning">
116127

117-
func applicationDidFinishLaunching(_ aNotification: Notification) {
118-
UserDefaults.standard.register(defaults: ["NSApplicationCrashOnExceptions": true])
128+
Don't use this option together with the `SentryCrashExceptionApplication`. Enabling both features can lead to duplicated reports.
119129

120-
SentrySDK.start { options in
121-
// ...
122-
}
130+
</Alert>
123131

124-
return true
132+
As of version [8.40.0](https://github.com/getsentry/sentry-cocoa/blob/main/CHANGELOG.md#8400), you can enable uncaught NSException reporting by setting the `enableUncaughtNSExceptionReporting` option to `true`. This is especially useful for applications using the SwiftUI lifecycle in combination with the [`NSApplicationDelegateAdaptor`](https://developer.apple.com/documentation/swiftui/nsapplicationdelegateadaptor), for which you can't easily use the `SentryCrashExceptionApplication` class.
133+
134+
```swift {tabTitle:Swift}
135+
import Sentry
136+
137+
SentrySDK.start { options in
138+
options.dsn = "___PUBLIC_DSN___"
139+
options.enableUncaughtNSExceptionReporting = true
125140
}
126141
```
142+
```objc {tabTitle:Objective-C}
143+
@import Sentry;
127144

128-
Once you have configured your application to crash whenever an uncaught exception occurs, the Apple SDK will capture those automatically. You don't need to manually call `captureException`.
145+
[SentrySDK startWithConfigureOptions:^(SentryOptions *options) {
146+
options.dsn = @"___PUBLIC_DSN___";
147+
options.enableUncaughtNSExceptionReporting = YES;
148+
}];
149+
```
129150
130151
</PlatformSection>

0 commit comments

Comments
 (0)