4949# import " RNSentryRNSScreen.h"
5050#endif
5151
52+ #import " RNSentryStart.h"
5253#import " RNSentryVersion.h"
5354
5455@interface
@@ -63,7 +64,6 @@ + (void)storeEnvelope:(SentryEnvelope *)envelope;
6364static bool hasFetchedAppStart;
6465
6566@implementation RNSentry {
66- bool sentHybridSdkDidBecomeActive;
6767 bool hasListeners;
6868 RNSentryTimeToDisplay *_timeToDisplay;
6969}
@@ -94,181 +94,16 @@ - (instancetype)init
9494 : (RCTPromiseRejectBlock)reject)
9595{
9696 NSError *error = nil ;
97- SentryOptions *sentryOptions = [self createOptionsWithDictionary: options error: &error];
97+ SentryOptions *sentryOptions = [RNSentryStart createOptionsWithDictionary: options error: &error];
9898 if (error != nil ) {
9999 reject (@" SentryReactNative" , error.localizedDescription , error);
100100 return ;
101101 }
102102
103- NSString *sdkVersion = [PrivateSentrySDKOnly getSdkVersionString ];
104- [PrivateSentrySDKOnly setSdkName: NATIVE_SDK_NAME andVersionString: sdkVersion];
105- [PrivateSentrySDKOnly addSdkPackage: REACT_NATIVE_SDK_PACKAGE_NAME
106- version: REACT_NATIVE_SDK_PACKAGE_VERSION];
107-
108- [SentrySDK startWithOptions: sentryOptions];
109-
110- #if TARGET_OS_IPHONE || TARGET_OS_MACCATALYST
111- BOOL appIsActive =
112- [[UIApplication sharedApplication ] applicationState ] == UIApplicationStateActive;
113- #else
114- BOOL appIsActive = [[NSApplication sharedApplication ] isActive ];
115- #endif
116-
117- // If the app is active/in foreground, and we have not sent the SentryHybridSdkDidBecomeActive
118- // notification, send it.
119- if (appIsActive && !sentHybridSdkDidBecomeActive
120- && (PrivateSentrySDKOnly.options .enableAutoSessionTracking
121- || PrivateSentrySDKOnly.options .enableWatchdogTerminationTracking )) {
122- [[NSNotificationCenter defaultCenter ] postNotificationName: @" SentryHybridSdkDidBecomeActive"
123- object: nil ];
124-
125- sentHybridSdkDidBecomeActive = true ;
126- }
127-
128- #if SENTRY_TARGET_REPLAY_SUPPORTED
129- [RNSentryReplay postInit ];
130- #endif
131-
103+ [RNSentryStart startWithOptions: sentryOptions];
132104 resolve (@YES );
133105}
134106
135- - (SentryOptions *_Nullable)createOptionsWithDictionary : (NSDictionary *_Nonnull)options
136- error : (NSError *_Nonnull *_Nonnull)errorPointer
137- {
138- SentryBeforeSendEventCallback beforeSend = ^SentryEvent *(SentryEvent *event)
139- {
140- // We don't want to send an event after startup that came from a Unhandled JS Exception of
141- // react native Because we sent it already before the app crashed.
142- if (nil != event.exceptions .firstObject .type &&
143- [event.exceptions.firstObject.type rangeOfString: @" Unhandled JS Exception" ].location
144- != NSNotFound ) {
145- return nil ;
146- }
147-
148- [self setEventOriginTag: event];
149-
150- return event;
151- };
152-
153- NSMutableDictionary *mutableOptions = [options mutableCopy ];
154- [mutableOptions setValue: beforeSend forKey: @" beforeSend" ];
155-
156- // remove performance traces sample rate and traces sampler since we don't want to synchronize
157- // these configurations to the Native SDKs. The user could tho initialize the SDK manually and
158- // set themselves.
159- [mutableOptions removeObjectForKey: @" tracesSampleRate" ];
160- [mutableOptions removeObjectForKey: @" tracesSampler" ];
161- [mutableOptions removeObjectForKey: @" enableTracing" ];
162-
163- #if SENTRY_TARGET_REPLAY_SUPPORTED
164- [RNSentryReplay updateOptions: mutableOptions];
165- #endif
166-
167- SentryOptions *sentryOptions = [[SentryOptions alloc ] initWithDict: mutableOptions
168- didFailWithError: errorPointer];
169- if (*errorPointer != nil ) {
170- return nil ;
171- }
172-
173- // Exclude Dev Server and Sentry Dsn request from Breadcrumbs
174- NSString *dsn = [self getURLFromDSN: [mutableOptions valueForKey: @" dsn" ]];
175- NSString *devServerUrl = [mutableOptions valueForKey: @" devServerUrl" ];
176- sentryOptions.beforeBreadcrumb
177- = ^SentryBreadcrumb *_Nullable (SentryBreadcrumb *_Nonnull breadcrumb)
178- {
179- NSString *url = breadcrumb.data [@" url" ] ?: @" " ;
180-
181- if ([@" http" isEqualToString: breadcrumb.type]
182- && ((dsn != nil && [url hasPrefix: dsn])
183- || (devServerUrl != nil && [url hasPrefix: devServerUrl]))) {
184- return nil ;
185- }
186- return breadcrumb;
187- };
188-
189- if ([mutableOptions valueForKey: @" enableNativeCrashHandling" ] != nil ) {
190- BOOL enableNativeCrashHandling = [mutableOptions[@" enableNativeCrashHandling" ] boolValue ];
191-
192- if (!enableNativeCrashHandling) {
193- NSMutableArray *integrations = sentryOptions.integrations .mutableCopy ;
194- [integrations removeObject: @" SentryCrashIntegration" ];
195- sentryOptions.integrations = integrations;
196- }
197- }
198-
199- // Set spotlight option
200- if ([mutableOptions valueForKey: @" spotlight" ] != nil ) {
201- id spotlightValue = [mutableOptions valueForKey: @" spotlight" ];
202- if ([spotlightValue isKindOfClass: [NSString class ]]) {
203- NSLog (@" Using Spotlight on address: %@ " , spotlightValue);
204- sentryOptions.enableSpotlight = true ;
205- sentryOptions.spotlightUrl = spotlightValue;
206- } else if ([spotlightValue isKindOfClass: [NSNumber class ]]) {
207- sentryOptions.enableSpotlight = [spotlightValue boolValue ];
208- id defaultSpotlightUrl = [mutableOptions valueForKey: @" defaultSidecarUrl" ];
209- if (defaultSpotlightUrl != nil ) {
210- sentryOptions.spotlightUrl = defaultSpotlightUrl;
211- }
212- }
213- }
214-
215- // Enable the App start and Frames tracking measurements
216- if ([mutableOptions valueForKey: @" enableAutoPerformanceTracing" ] != nil ) {
217- BOOL enableAutoPerformanceTracing =
218- [mutableOptions[@" enableAutoPerformanceTracing" ] boolValue ];
219- PrivateSentrySDKOnly.appStartMeasurementHybridSDKMode = enableAutoPerformanceTracing;
220- #if TARGET_OS_IPHONE || TARGET_OS_MACCATALYST
221- PrivateSentrySDKOnly.framesTrackingMeasurementHybridSDKMode = enableAutoPerformanceTracing;
222- #endif
223- }
224-
225- // Failed requests can only be enabled in one SDK to avoid duplicates
226- sentryOptions.enableCaptureFailedRequests = NO ;
227-
228- return sentryOptions;
229- }
230-
231- - (NSString *_Nullable)getURLFromDSN : (NSString *)dsn
232- {
233- NSURL *url = [NSURL URLWithString: dsn];
234- if (!url) {
235- return nil ;
236- }
237- return [NSString stringWithFormat: @" %@ ://%@ " , url.scheme, url.host];
238- }
239-
240- - (void )setEventOriginTag : (SentryEvent *)event
241- {
242- if (event.sdk != nil ) {
243- NSString *sdkName = event.sdk [@" name" ];
244-
245- // If the event is from react native, it gets set
246- // there and we do not handle it here.
247- if ([sdkName isEqual: NATIVE_SDK_NAME]) {
248- [self setEventEnvironmentTag: event origin: @" ios" environment: @" native" ];
249- }
250- }
251- }
252-
253- - (void )setEventEnvironmentTag : (SentryEvent *)event
254- origin : (NSString *)origin
255- environment : (NSString *)environment
256- {
257- NSMutableDictionary *newTags = [NSMutableDictionary new ];
258-
259- if (nil != event.tags && [event.tags count ] > 0 ) {
260- [newTags addEntriesFromDictionary: event.tags];
261- }
262- if (nil != origin) {
263- [newTags setValue: origin forKey: @" event.origin" ];
264- }
265- if (nil != environment) {
266- [newTags setValue: environment forKey: @" event.environment" ];
267- }
268-
269- event.tags = newTags;
270- }
271-
272107RCT_EXPORT_METHOD (initNativeReactNavigationNewFrameTracking
273108 : (RCTPromiseResolveBlock)resolve rejecter
274109 : (RCTPromiseRejectBlock)reject)
0 commit comments