35
35
36
36
@implementation RNCallKeep
37
37
{
38
- NSMutableDictionary *_settings;
39
38
NSOperatingSystemVersion _version;
40
39
BOOL _isStartCallActionEventListenerAdded;
41
40
}
42
41
42
+ static CXProvider * sharedProvider;
43
+
43
44
// should initialise in AppDelegate.m
44
45
RCT_EXPORT_MODULE ()
45
46
@@ -73,23 +74,31 @@ - (void)dealloc
73
74
if (self.callKeepProvider != nil ) {
74
75
[self .callKeepProvider invalidate ];
75
76
}
77
+ sharedProvider = nil ;
76
78
}
77
79
78
80
// Override method of RCTEventEmitter
79
81
- (NSArray <NSString *> *)supportedEvents
80
82
{
81
83
return @[
82
- RNCallKeepDidReceiveStartCallAction,
83
- RNCallKeepPerformAnswerCallAction,
84
- RNCallKeepPerformEndCallAction,
85
- RNCallKeepDidActivateAudioSession,
86
- RNCallKeepDidDeactivateAudioSession,
87
- RNCallKeepDidDisplayIncomingCall,
88
- RNCallKeepDidPerformSetMutedCallAction,
89
- RNCallKeepPerformPlayDTMFCallAction,
90
- RNCallKeepDidToggleHoldAction,
91
- RNCallKeepProviderReset
92
- ];
84
+ RNCallKeepDidReceiveStartCallAction,
85
+ RNCallKeepPerformAnswerCallAction,
86
+ RNCallKeepPerformEndCallAction,
87
+ RNCallKeepDidActivateAudioSession,
88
+ RNCallKeepDidDeactivateAudioSession,
89
+ RNCallKeepDidDisplayIncomingCall,
90
+ RNCallKeepDidPerformSetMutedCallAction,
91
+ RNCallKeepPerformPlayDTMFCallAction,
92
+ RNCallKeepDidToggleHoldAction,
93
+ RNCallKeepProviderReset
94
+ ];
95
+ }
96
+
97
+ + (void )initCallKitProvider {
98
+ if (sharedProvider == nil ) {
99
+ NSDictionary *settings = [[NSUserDefaults standardUserDefaults ] dictionaryForKey: @" RNCallKeepSettings" ];
100
+ sharedProvider = [[CXProvider alloc ] initWithConfiguration: [RNCallKeep getProviderConfiguration: settings]];
101
+ }
93
102
}
94
103
95
104
RCT_EXPORT_METHOD (setup:(NSDictionary *)options)
@@ -99,8 +108,14 @@ - (void)dealloc
99
108
#endif
100
109
_version = [[[NSProcessInfo alloc ] init ] operatingSystemVersion ];
101
110
self.callKeepCallController = [[CXCallController alloc ] init ];
102
- _settings = [[NSMutableDictionary alloc ] initWithDictionary: options];
103
- self.callKeepProvider = [[CXProvider alloc ] initWithConfiguration: [self getProviderConfiguration ]];
111
+ NSDictionary *settings = [[NSMutableDictionary alloc ] initWithDictionary: options];
112
+ // Store settings in NSUserDefault
113
+ [[NSUserDefaults standardUserDefaults ] setObject: settings forKey: @" RNCallKeepSettings" ];
114
+ [[NSUserDefaults standardUserDefaults ] synchronize ];
115
+
116
+ [RNCallKeep initCallKitProvider ];
117
+
118
+ self.callKeepProvider = sharedProvider;
104
119
[self .callKeepProvider setDelegate: self queue: nil ];
105
120
}
106
121
@@ -134,29 +149,7 @@ - (void)dealloc
134
149
hasVideo:(BOOL )hasVideo
135
150
localizedCallerName:(NSString * _Nullable)localizedCallerName)
136
151
{
137
- #ifdef DEBUG
138
- NSLog (@" [RNCallKeep][displayIncomingCall] uuidString = %@ " , uuidString);
139
- #endif
140
- int _handleType = [self getHandleType: handleType];
141
- NSUUID *uuid = [[NSUUID alloc ] initWithUUIDString: uuidString];
142
- CXCallUpdate *callUpdate = [[CXCallUpdate alloc ] init ];
143
- callUpdate.remoteHandle = [[CXHandle alloc ] initWithType: _handleType value: handle];
144
- callUpdate.supportsDTMF = YES ;
145
- callUpdate.supportsHolding = YES ;
146
- callUpdate.supportsGrouping = YES ;
147
- callUpdate.supportsUngrouping = YES ;
148
- callUpdate.hasVideo = hasVideo;
149
- callUpdate.localizedCallerName = localizedCallerName;
150
-
151
- [self .callKeepProvider reportNewIncomingCallWithUUID: uuid update: callUpdate completion: ^(NSError * _Nullable error) {
152
- [self sendEventWithName: RNCallKeepDidDisplayIncomingCall body: @{ @" error" : error ? error.localizedDescription : @" " }];
153
- if (error == nil ) {
154
- // Workaround per https://forums.developer.apple.com/message/169511
155
- if ([self lessThanIos10_2 ]) {
156
- [self configureAudioSession ];
157
- }
158
- }
159
- }];
152
+ [RNCallKeep reportNewIncomingCall: uuidString handle: handle handleType: handleType hasVideo: hasVideo localizedCallerName: localizedCallerName fromPushKit: NO ];
160
153
}
161
154
162
155
RCT_EXPORT_METHOD (startCall:(NSString *)uuidString
@@ -168,7 +161,7 @@ - (void)dealloc
168
161
#ifdef DEBUG
169
162
NSLog (@" [RNCallKeep][startCall] uuidString = %@ " , uuidString);
170
163
#endif
171
- int _handleType = [self getHandleType: handleType];
164
+ int _handleType = [RNCallKeep getHandleType: handleType];
172
165
NSUUID *uuid = [[NSUUID alloc ] initWithUUIDString: uuidString];
173
166
CXHandle *callHandle = [[CXHandle alloc ] initWithType: _handleType value: handle];
174
167
CXStartCallAction *startCallAction = [[CXStartCallAction alloc ] initWithCallUUID: uuid handle: callHandle];
@@ -325,6 +318,40 @@ - (void)requestTransaction:(CXTransaction *)transaction
325
318
}];
326
319
}
327
320
321
+ + (void )reportNewIncomingCall : (NSString *)uuidString
322
+ handle : (NSString *)handle
323
+ handleType : (NSString *)handleType
324
+ hasVideo : (BOOL )hasVideo
325
+ localizedCallerName : (NSString * _Nullable)localizedCallerName
326
+ fromPushKit : (BOOL )fromPushKit
327
+ {
328
+ #ifdef DEBUG
329
+ NSLog (@" [RNCallKeep][reportNewIncomingCall] uuidString = %@ " , uuidString);
330
+ #endif
331
+ int _handleType = [RNCallKeep getHandleType: handleType];
332
+ NSUUID *uuid = [[NSUUID alloc ] initWithUUIDString: uuidString];
333
+ CXCallUpdate *callUpdate = [[CXCallUpdate alloc ] init ];
334
+ callUpdate.remoteHandle = [[CXHandle alloc ] initWithType: _handleType value: handle];
335
+ callUpdate.supportsDTMF = YES ;
336
+ callUpdate.supportsHolding = YES ;
337
+ callUpdate.supportsGrouping = YES ;
338
+ callUpdate.supportsUngrouping = YES ;
339
+ callUpdate.hasVideo = hasVideo;
340
+ callUpdate.localizedCallerName = localizedCallerName;
341
+
342
+ [RNCallKeep initCallKitProvider ];
343
+ [sharedProvider reportNewIncomingCallWithUUID: uuid update: callUpdate completion: ^(NSError * _Nullable error) {
344
+ RNCallKeep *callKeep = [RNCallKeep allocWithZone: nil ];
345
+ [callKeep sendEventWithName: RNCallKeepDidDisplayIncomingCall body: @{ @" error" : error ? error.localizedDescription : @" " , @" callUUID" : uuidString, @" handle" : handle, @" localizedCallerName" : localizedCallerName, @" fromPushKit" : fromPushKit }];
346
+ if (error == nil ) {
347
+ // Workaround per https://forums.developer.apple.com/message/169511
348
+ if ([callKeep lessThanIos10_2 ]) {
349
+ [callKeep configureAudioSession ];
350
+ }
351
+ }
352
+ }];
353
+ }
354
+
328
355
- (BOOL )lessThanIos10_2
329
356
{
330
357
if (_version.majorVersion < 10 ) {
@@ -336,7 +363,7 @@ - (BOOL)lessThanIos10_2
336
363
}
337
364
}
338
365
339
- - (int )getHandleType : (NSString *)handleType
366
+ + (int )getHandleType : (NSString *)handleType
340
367
{
341
368
int _handleType;
342
369
if ([handleType isEqualToString: @" generic" ]) {
@@ -351,30 +378,30 @@ - (int)getHandleType:(NSString *)handleType
351
378
return _handleType;
352
379
}
353
380
354
- - (CXProviderConfiguration *)getProviderConfiguration
381
+ + (CXProviderConfiguration *)getProviderConfiguration : ( NSDictionary *) settings
355
382
{
356
383
#ifdef DEBUG
357
384
NSLog (@" [RNCallKeep][getProviderConfiguration]" );
358
385
#endif
359
- CXProviderConfiguration *providerConfiguration = [[CXProviderConfiguration alloc ] initWithLocalizedName: _settings [@" appName" ]];
386
+ CXProviderConfiguration *providerConfiguration = [[CXProviderConfiguration alloc ] initWithLocalizedName: settings [@" appName" ]];
360
387
providerConfiguration.supportsVideo = YES ;
361
388
providerConfiguration.maximumCallGroups = 3 ;
362
389
providerConfiguration.maximumCallsPerCallGroup = 1 ;
363
390
providerConfiguration.supportedHandleTypes = [NSSet setWithObjects: [NSNumber numberWithInteger: CXHandleTypePhoneNumber], nil ];
364
- if (_settings [@" supportsVideo" ]) {
365
- providerConfiguration.supportsVideo = _settings [@" supportsVideo" ];
391
+ if (settings [@" supportsVideo" ]) {
392
+ providerConfiguration.supportsVideo = settings [@" supportsVideo" ];
366
393
}
367
- if (_settings [@" maximumCallGroups" ]) {
368
- providerConfiguration.maximumCallGroups = [_settings [@" maximumCallGroups" ] integerValue ];
394
+ if (settings [@" maximumCallGroups" ]) {
395
+ providerConfiguration.maximumCallGroups = [settings [@" maximumCallGroups" ] integerValue ];
369
396
}
370
- if (_settings [@" maximumCallsPerCallGroup" ]) {
371
- providerConfiguration.maximumCallsPerCallGroup = [_settings [@" maximumCallsPerCallGroup" ] integerValue ];
397
+ if (settings [@" maximumCallsPerCallGroup" ]) {
398
+ providerConfiguration.maximumCallsPerCallGroup = [settings [@" maximumCallsPerCallGroup" ] integerValue ];
372
399
}
373
- if (_settings [@" imageName" ]) {
374
- providerConfiguration.iconTemplateImageData = UIImagePNGRepresentation ([UIImage imageNamed: _settings [@" imageName" ]]);
400
+ if (settings [@" imageName" ]) {
401
+ providerConfiguration.iconTemplateImageData = UIImagePNGRepresentation ([UIImage imageNamed: settings [@" imageName" ]]);
375
402
}
376
- if (_settings [@" ringtoneSound" ]) {
377
- providerConfiguration.ringtoneSound = _settings [@" ringtoneSound" ];
403
+ if (settings [@" ringtoneSound" ]) {
404
+ providerConfiguration.ringtoneSound = settings [@" ringtoneSound" ];
378
405
}
379
406
return providerConfiguration;
380
407
}
@@ -449,9 +476,9 @@ + (BOOL)application:(UIApplication *)application
449
476
450
477
if (handle != nil && handle.length > 0 ){
451
478
NSDictionary *userInfo = @{
452
- @" handle" : handle,
453
- @" video" : @(isVideoCall)
454
- };
479
+ @" handle" : handle,
480
+ @" video" : @(isVideoCall)
481
+ };
455
482
456
483
RNCallKeep *callKeep = [RNCallKeep allocWithZone: nil ];
457
484
[callKeep handleStartCallNotification: userInfo];
@@ -583,10 +610,10 @@ - (void)provider:(CXProvider *)provider didActivateAudioSession:(AVAudioSession
583
610
NSLog (@" [RNCallKeep][CXProviderDelegate][provider:didActivateAudioSession]" );
584
611
#endif
585
612
NSDictionary *userInfo
586
- = @{
587
- AVAudioSessionInterruptionTypeKey: [NSNumber numberWithInt: AVAudioSessionInterruptionTypeEnded],
588
- AVAudioSessionInterruptionOptionKey: [NSNumber numberWithInt: AVAudioSessionInterruptionOptionShouldResume]
589
- };
613
+ = @{
614
+ AVAudioSessionInterruptionTypeKey: [NSNumber numberWithInt: AVAudioSessionInterruptionTypeEnded],
615
+ AVAudioSessionInterruptionOptionKey: [NSNumber numberWithInt: AVAudioSessionInterruptionOptionShouldResume]
616
+ };
590
617
[[NSNotificationCenter defaultCenter ] postNotificationName: AVAudioSessionInterruptionNotification object: nil userInfo: userInfo];
591
618
592
619
[self configureAudioSession ];
0 commit comments