66#import " SentryDependencyContainer.h"
77#import " SentryHub.h"
88#import " SentryLog.h"
9+ #import " SentryReachability.h"
910#import " SentryScope.h"
1011#import " SentrySwift.h"
1112#import " SentrySwizzle.h"
1516# import < UIKit/UIKit.h>
1617#elif TARGET_OS_OSX || TARGET_OS_MACCATALYST
1718# import < Cocoa/Cocoa.h>
18- #endif
19+ #endif // !TARGET_OS_WATCH
1920
2021NS_ASSUME_NONNULL_BEGIN
2122
2425
2526@interface
2627SentryBreadcrumbTracker ()
28+ #if !TARGET_OS_WATCH
29+ <SentryReachabilityObserver>
30+ #endif // !TARGET_OS_WATCH
2731
2832@property (nonatomic, weak) id <SentryBreadcrumbDelegate> delegate;
2933
3034@end
3135
3236@implementation SentryBreadcrumbTracker
3337
34- - (instancetype )init
38+ #if !TARGET_OS_WATCH
39+ - (void )dealloc
3540{
36- return [ super init ];
41+ [SentryDependencyContainer.sharedInstance.reachability removeObserver: self ];
3742}
43+ #endif // !TARGET_OS_WATCH
3844
3945- (void )startWithDelegate : (id <SentryBreadcrumbDelegate>)delegate
4046{
4147 _delegate = delegate;
4248 [self addEnabledCrumb ];
4349 [self trackApplicationUIKitNotifications ];
50+ #if !TARGET_OS_WATCH
51+ [self trackNetworkConnectivityChanges ];
52+ #endif // !TARGET_OS_WATCH
4453}
4554
4655- (void )startSwizzle
@@ -56,7 +65,7 @@ - (void)stop
5665#if SENTRY_HAS_UIKIT
5766 [SentryDependencyContainer.sharedInstance.swizzleWrapper
5867 removeSwizzleSendActionForKey: SentryBreadcrumbTrackerSwizzleSendAction];
59- #endif
68+ #endif // SENTRY_HAS_UIKIT
6069 _delegate = nil ;
6170}
6271
@@ -70,10 +79,10 @@ - (void)trackApplicationUIKitNotifications
7079 // Will resign Active notification is the nearest one to
7180 // UIApplicationDidEnterBackgroundNotification
7281 NSNotificationName backgroundNotificationName = NSApplicationWillResignActiveNotification ;
73- #else
82+ #else // TARGET_OS_WATCH
7483 SENTRY_LOG_DEBUG (@" NO UIKit, OSX and Catalyst -> [SentryBreadcrumbTracker "
7584 @" trackApplicationUIKitNotifications] does nothing." );
76- #endif
85+ #endif // !TARGET_OS_WATCH
7786
7887 // not available for macOS
7988#if SENTRY_HAS_UIKIT
@@ -90,9 +99,9 @@ - (void)trackApplicationUIKitNotifications
9099 crumb.message = @" Low memory" ;
91100 [self .delegate addBreadcrumb: crumb];
92101 }];
93- #endif
102+ #endif // SENTRY_HAS_UIKIT
94103
95- #if SENTRY_HAS_UIKIT || TARGET_OS_OSX || TARGET_OS_MACCATALYST
104+ #if !TARGET_OS_WATCH
96105 [NSNotificationCenter .defaultCenter addObserverForName: backgroundNotificationName
97106 object: nil
98107 queue: nil
@@ -114,9 +123,25 @@ - (void)trackApplicationUIKitNotifications
114123 withDataKey: @" state"
115124 withDataValue: @" foreground" ];
116125 }];
117- #endif
126+ #endif // !TARGET_OS_WATCH
118127}
119128
129+ #if !TARGET_OS_WATCH
130+ - (void )trackNetworkConnectivityChanges
131+ {
132+ [SentryDependencyContainer.sharedInstance.reachability
133+ addObserver: self
134+ withCallback: ^(BOOL connected, NSString *_Nonnull typeDescription) {
135+ SentryBreadcrumb *crumb =
136+ [[SentryBreadcrumb alloc ] initWithLevel: kSentryLevelInfo
137+ category: @" device.connectivity" ];
138+ crumb.type = @" connectivity" ;
139+ crumb.data = [NSDictionary dictionaryWithObject: typeDescription forKey: @" connectivity" ];
140+ [self .delegate addBreadcrumb: crumb];
141+ }];
142+ }
143+ #endif // !TARGET_OS_WATCH
144+
120145- (void )addBreadcrumbWithType : (NSString *)type
121146 withCategory : (NSString *)category
122147 withLevel : (SentryLevel)level
@@ -155,7 +180,7 @@ + (BOOL)avoidSender:(id)sender forTarget:(id)target action:(NSString *)action
155180 }
156181 return NO ;
157182}
158- #endif
183+ #endif // SENTRY_HAS_UIKIT
159184
160185- (void )swizzleSendAction
161186{
@@ -183,9 +208,9 @@ - (void)swizzleSendAction
183208 }
184209 forKey: SentryBreadcrumbTrackerSwizzleSendAction];
185210
186- #else
211+ #else // !SENTRY_HAS_UIKIT
187212 SENTRY_LOG_DEBUG (@" NO UIKit -> [SentryBreadcrumbTracker swizzleSendAction] does nothing." );
188- #endif
213+ #endif // SENTRY_HAS_UIKIT
189214}
190215
191216- (void )swizzleViewDidAppear
@@ -223,9 +248,9 @@ - (void)swizzleViewDidAppear
223248 }),
224249 mode, swizzleViewDidAppearKey);
225250# pragma clang diagnostic pop
226- #else
251+ #else // !SENTRY_HAS_UIKIT
227252 SENTRY_LOG_DEBUG (@" NO UIKit -> [SentryBreadcrumbTracker swizzleViewDidAppear] does nothing." );
228- #endif
253+ #endif // SENTRY_HAS_UIKIT
229254}
230255
231256#if SENTRY_HAS_UIKIT
@@ -287,7 +312,7 @@ + (NSDictionary *)fetchInfoAboutViewController:(UIViewController *)controller
287312
288313 return info;
289314}
290- #endif
315+ #endif // SENTRY_HAS_UIKIT
291316
292317@end
293318
0 commit comments