@@ -102,8 +102,13 @@ - (void)initBatteryObserver:(UIDevice *)currentDevice
102102- (void )batteryStateChanged : (NSNotification *)notification
103103{
104104 // Notifications for battery level change are sent no more frequently than once per minute
105- NSMutableDictionary <NSString *, id > *batteryData = [self getBatteryStatus: notification.object];
106- batteryData[@" action" ] = @" BATTERY_STATE_CHANGE" ;
105+ UIDevice *currentDevice = notification.object ;
106+ // The object of an NSNotification may be nil.
107+ if (currentDevice == nil ) {
108+ return ;
109+ }
110+
111+ NSDictionary <NSString *, id > *batteryData = [self getBatteryStatus: notification.object];
107112
108113 SentryBreadcrumb *crumb = [[SentryBreadcrumb alloc ] initWithLevel: kSentryLevelInfo
109114 category: @" device.event" ];
@@ -112,7 +117,7 @@ - (void)batteryStateChanged:(NSNotification *)notification
112117 [_delegate addBreadcrumb: crumb];
113118}
114119
115- - (NSMutableDictionary <NSString *, id> *)getBatteryStatus : (UIDevice *)currentDevice
120+ - (NSDictionary <NSString *, id> *)getBatteryStatus : (UIDevice *)currentDevice
116121{
117122 // borrowed and adapted from
118123 // https://github.com/apache/cordova-plugin-battery-status/blob/master/src/ios/CDVBattery.m
@@ -124,7 +129,8 @@ - (void)batteryStateChanged:(NSNotification *)notification
124129 isPlugged = YES ;
125130 }
126131 float currentLevel = [currentDevice batteryLevel ];
127- NSMutableDictionary <NSString *, id > *batteryData = [NSMutableDictionary new ];
132+ NSMutableDictionary <NSString *, id > *batteryData =
133+ [NSMutableDictionary dictionaryWithCapacity: 3 ];
128134
129135 // W3C spec says level must be null if it is unknown
130136 if ((currentState != UIDeviceBatteryStateUnknown) && (currentLevel != -1.0 )) {
@@ -135,6 +141,8 @@ - (void)batteryStateChanged:(NSNotification *)notification
135141 }
136142
137143 batteryData[@" plugged" ] = @(isPlugged);
144+ batteryData[@" action" ] = @" BATTERY_STATE_CHANGE" ;
145+
138146 return batteryData;
139147}
140148
0 commit comments