@@ -18,9 +18,10 @@ import Bugsnag
1818class AppDelegate : UIResponder , UIApplicationDelegate {
1919
2020 func application ( _ application : UIApplication , didFinishLaunchingWithOptions launchOptions : [ UIApplication . LaunchOptionsKey : Any ] ? ) -> Bool {
21- // Override point for customization after application launch.
22- let config = BugsnagConfiguration . loadConfig ( )
23- BugsnagConfigKt . startBugsnag ( config : config )
21+ // Configures Bugsnag for Kotlin crash handling, starts it, and registers the
22+ // sink with the Kotlin side (which installs the unhandled-exception hook).
23+ let sink = BugsnagSink . start ( BugsnagConfiguration . loadConfig ( ) )
24+ BugsnagKt . registerBugsnagSink ( sink : sink )
2425
2526 return true
2627 }
@@ -40,3 +41,75 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
4041 }
4142}
4243
44+ // Copy of the reference sink from the CrashKiOS Swift package
45+ // (Sources/CrashKiOSBugsnag/BugsnagSink.swift), inlined + the protocol declared in
46+ // BridgingHeader.h so the sample doesn't need the package wired into the Xcode
47+ // project. Prefer consuming the CrashKiOSBugsnag package product in a real app.
48+ final class BugsnagSink : NSObject , CrashKiOSBugsnagSink {
49+
50+ /// Configures `config` to suppress the duplicate termination crash that follows a
51+ /// recorded Kotlin fatal, starts Bugsnag, and returns the sink.
52+ static func start ( _ config : BugsnagConfiguration ) -> BugsnagSink {
53+ overrideOriginalUnhandledValue ( )
54+ config . addOnSendError { event in
55+ ! event . unhandled || ! event . featureFlags . contains { $0 . name == kotlinCrashedFeatureFlag }
56+ }
57+ config . clearFeatureFlag ( name : kotlinCrashedFeatureFlag )
58+ Bugsnag . start ( with : config )
59+ return BugsnagSink ( )
60+ }
61+
62+ func leaveBreadcrumb ( _ message : String ) {
63+ Bugsnag . leaveBreadcrumb ( withMessage : message )
64+ }
65+
66+ func notify ( with exceptions : [ NSException ] , handled : Bool ) {
67+ guard let exception = exceptions . first else { return }
68+ Bugsnag . notify ( exception ) { event in
69+ if handled {
70+ event . severity = . warning
71+ } else {
72+ event . unhandled = true
73+ event . severity = . error
74+ }
75+ event . errors += exceptions . dropFirst ( ) . map ( BugsnagError . init )
76+ return true
77+ }
78+ }
79+
80+ func addMetadata ( _ value : Any , key : String , section : String ) {
81+ Bugsnag . addMetadata ( value , key : key , section : section )
82+ }
83+
84+ func markFatalCrashRecorded ( ) {
85+ // Called only by the Kotlin unhandled-exception hook, right before termination —
86+ // never on direct sendFatalException() calls, which would poison the session and
87+ // make the OnSendError filter discard later genuine crashes.
88+ Bugsnag . addFeatureFlag ( name : kotlinCrashedFeatureFlag )
89+ }
90+ }
91+
92+ /// Feature flag used to mark the Kotlin termination crash.
93+ private let kotlinCrashedFeatureFlag = " crashkios.kotlin_crashed "
94+
95+ /// In Bugsnag 6.26.2+ the `originalUnhandledValue` property prevents our synthetic
96+ /// unhandled exceptions from being stored to disk; alias it to `unhandled`.
97+ /// https://github.com/bugsnag/bugsnag-cocoa/pull/1549
98+ private func overrideOriginalUnhandledValue ( ) {
99+ guard let handledStateClass = NSClassFromString ( " BugsnagHandledState " ) ,
100+ let originalMethod = class_getInstanceMethod ( handledStateClass , NSSelectorFromString ( " originalUnhandledValue " ) ) ,
101+ let method = class_getInstanceMethod ( handledStateClass , NSSelectorFromString ( " unhandled " ) )
102+ else { return }
103+ method_setImplementation ( originalMethod , method_getImplementation ( method ) )
104+ }
105+
106+ private extension BugsnagError {
107+ /// Creates a BugsnagError from a (Kotlin-synthesized) NSException.
108+ convenience init ( _ exception : NSException ) {
109+ self . init ( )
110+ errorClass = exception . name . rawValue
111+ errorMessage = exception . reason
112+ stacktrace = BugsnagStackframe . stackframes ( withCallStackReturnAddresses : exception . callStackReturnAddresses )
113+ type = . cocoa
114+ }
115+ }
0 commit comments