Skip to content

Commit 4b08b08

Browse files
committed
Fix crash on onAudioRouteChange when output is nil
1 parent e552a0f commit 4b08b08

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

ios/RNCallKeep/RNCallKeep.m

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,11 @@ - (void)onAudioRouteChange:(NSNotification *)notification
126126
{
127127
NSDictionary *info = notification.userInfo;
128128
NSInteger reason = [[info valueForKey:AVAudioSessionRouteChangeReasonKey] integerValue];
129-
NSString *output = [AVAudioSession sharedInstance].currentRoute.outputs.count > 0 ? [AVAudioSession sharedInstance].currentRoute.outputs[0].portType : nil;
129+
NSString *output = [RNCallKeep getAudioOutput];
130+
131+
if (output == nil) {
132+
return;
133+
}
130134

131135
[self sendEventWithName:RNCallKeepDidChangeAudioRoute body:@{
132136
@"output": output,
@@ -154,6 +158,10 @@ + (void)initCallKitProvider {
154158
}
155159
}
156160

161+
+ (NSString *) getAudioOutput {
162+
return [AVAudioSession sharedInstance].currentRoute.outputs.count > 0 ? [AVAudioSession sharedInstance].currentRoute.outputs[0].portType : nil;
163+
}
164+
157165
+ (void)setup:(NSDictionary *)options {
158166
RNCallKeep *callKeep = [RNCallKeep allocWithZone: nil];
159167
[callKeep setup:options];
@@ -203,7 +211,7 @@ + (void)setup:(NSDictionary *)options {
203211
#ifdef DEBUG
204212
NSLog(@"[RNCallKeep][checkSpeaker]");
205213
#endif
206-
NSString *output = [AVAudioSession sharedInstance].currentRoute.outputs.count > 0 ? [AVAudioSession sharedInstance].currentRoute.outputs[0].portType : nil;
214+
NSString *output = [RNCallKeep getAudioOutput];
207215
resolve(@([output isEqualToString:@"Speaker"]));
208216
}
209217

0 commit comments

Comments
 (0)