3131
3232@implementation RNCallKeep
3333{
34- NSMutableDictionary *_settings;
3534 NSOperatingSystemVersion _version;
3635 BOOL _isStartCallActionEventListenerAdded;
3736}
3837
38+ static CXProvider * sharedProvider;
39+
3940// should initialise in AppDelegate.m
4041RCT_EXPORT_MODULE ()
4142
@@ -64,6 +65,7 @@ - (void)dealloc
6465 if (self.callKeepProvider != nil ) {
6566 [self .callKeepProvider invalidate ];
6667 }
68+ sharedProvider = nil ;
6769}
6870
6971// Override method of RCTEventEmitter
@@ -83,15 +85,28 @@ - (void)dealloc
8385 ];
8486}
8587
88+ + (void )initCallKitProvider {
89+ if (sharedProvider == nil ) {
90+ NSDictionary *settings = [[NSUserDefaults standardUserDefaults ] dictionaryForKey: @" RNCallKeepSettings" ];
91+ sharedProvider = [[CXProvider alloc ] initWithConfiguration: [RNCallKeep getProviderConfiguration: settings]];
92+ }
93+ }
94+
8695RCT_EXPORT_METHOD (setup:(NSDictionary *)options)
8796{
8897#ifdef DEBUG
8998 NSLog (@" [RNCallKeep][setup] options = %@ " , options);
9099#endif
91100 _version = [[[NSProcessInfo alloc ] init ] operatingSystemVersion ];
92101 self.callKeepCallController = [[CXCallController alloc ] init ];
93- _settings = [[NSMutableDictionary alloc ] initWithDictionary: options];
94- self.callKeepProvider = [[CXProvider alloc ] initWithConfiguration: [self getProviderConfiguration ]];
102+ NSDictionary *settings = [[NSMutableDictionary alloc ] initWithDictionary: options];
103+ // Store settings in NSUserDefault
104+ [[NSUserDefaults standardUserDefaults ] setObject: settings forKey: @" RNCallKeepSettings" ];
105+ [[NSUserDefaults standardUserDefaults ] synchronize ];
106+
107+ [RNCallKeep initCallKitProvider ];
108+
109+ self.callKeepProvider = sharedProvider;
95110 [self .callKeepProvider setDelegate: self queue: nil ];
96111}
97112
@@ -128,7 +143,7 @@ - (void)dealloc
128143#ifdef DEBUG
129144 NSLog (@" [RNCallKeep][displayIncomingCall] uuidString = %@ " , uuidString);
130145#endif
131- int _handleType = [self getHandleType: handleType];
146+ int _handleType = [RNCallKeep getHandleType: handleType];
132147 NSUUID *uuid = [[NSUUID alloc ] initWithUUIDString: uuidString];
133148 CXCallUpdate *callUpdate = [[CXCallUpdate alloc ] init ];
134149 callUpdate.remoteHandle = [[CXHandle alloc ] initWithType: _handleType value: handle];
@@ -159,7 +174,7 @@ - (void)dealloc
159174#ifdef DEBUG
160175 NSLog (@" [RNCallKeep][startCall] uuidString = %@ " , uuidString);
161176#endif
162- int _handleType = [self getHandleType: handleType];
177+ int _handleType = [RNCallKeep getHandleType: handleType];
163178 NSUUID *uuid = [[NSUUID alloc ] initWithUUIDString: uuidString];
164179 CXHandle *callHandle = [[CXHandle alloc ] initWithType: _handleType value: handle];
165180 CXStartCallAction *startCallAction = [[CXStartCallAction alloc ] initWithCallUUID: uuid handle: callHandle];
@@ -316,6 +331,32 @@ - (void)requestTransaction:(CXTransaction *)transaction
316331 }];
317332}
318333
334+ + (void )reportNewIncomingCall : (NSString *)uuidString
335+ handle : (NSString *)handle
336+ handleType : (NSString *)handleType
337+ hasVideo : (BOOL )hasVideo
338+ localizedCallerName : (NSString * _Nullable)localizedCallerName
339+ {
340+ #ifdef DEBUG
341+ NSLog (@" [RNCallKeep][reportNewIncomingCall] uuidString = %@ " , uuidString);
342+ #endif
343+ int _handleType = [RNCallKeep getHandleType: handleType];
344+ NSUUID *uuid = [[NSUUID alloc ] initWithUUIDString: uuidString];
345+ CXCallUpdate *callUpdate = [[CXCallUpdate alloc ] init ];
346+ callUpdate.remoteHandle = [[CXHandle alloc ] initWithType: _handleType value: handle];
347+ callUpdate.supportsDTMF = YES ;
348+ callUpdate.supportsHolding = YES ;
349+ callUpdate.supportsGrouping = YES ;
350+ callUpdate.supportsUngrouping = YES ;
351+ callUpdate.hasVideo = hasVideo;
352+ callUpdate.localizedCallerName = localizedCallerName;
353+
354+ [RNCallKeep initCallKitProvider ];
355+ [sharedProvider reportNewIncomingCallWithUUID: uuid update: callUpdate completion: ^(NSError * _Nullable error) {
356+ if (error == nil ) {}
357+ }];
358+ }
359+
319360- (BOOL )lessThanIos10_2
320361{
321362 if (_version.majorVersion < 10 ) {
@@ -327,7 +368,7 @@ - (BOOL)lessThanIos10_2
327368 }
328369}
329370
330- - (int )getHandleType : (NSString *)handleType
371+ + (int )getHandleType : (NSString *)handleType
331372{
332373 int _handleType;
333374 if ([handleType isEqualToString: @" generic" ]) {
@@ -342,30 +383,30 @@ - (int)getHandleType:(NSString *)handleType
342383 return _handleType;
343384}
344385
345- - (CXProviderConfiguration *)getProviderConfiguration
386+ + (CXProviderConfiguration *)getProviderConfiguration : ( NSDictionary *) settings
346387{
347388#ifdef DEBUG
348389 NSLog (@" [RNCallKeep][getProviderConfiguration]" );
349390#endif
350- CXProviderConfiguration *providerConfiguration = [[CXProviderConfiguration alloc ] initWithLocalizedName: _settings [@" appName" ]];
391+ CXProviderConfiguration *providerConfiguration = [[CXProviderConfiguration alloc ] initWithLocalizedName: settings [@" appName" ]];
351392 providerConfiguration.supportsVideo = YES ;
352393 providerConfiguration.maximumCallGroups = 3 ;
353394 providerConfiguration.maximumCallsPerCallGroup = 1 ;
354395 providerConfiguration.supportedHandleTypes = [NSSet setWithObjects: [NSNumber numberWithInteger: CXHandleTypePhoneNumber], nil ];
355- if (_settings [@" supportsVideo" ]) {
356- providerConfiguration.supportsVideo = _settings [@" supportsVideo" ];
396+ if (settings [@" supportsVideo" ]) {
397+ providerConfiguration.supportsVideo = settings [@" supportsVideo" ];
357398 }
358- if (_settings [@" maximumCallGroups" ]) {
359- providerConfiguration.maximumCallGroups = [_settings [@" maximumCallGroups" ] integerValue ];
399+ if (settings [@" maximumCallGroups" ]) {
400+ providerConfiguration.maximumCallGroups = [settings [@" maximumCallGroups" ] integerValue ];
360401 }
361- if (_settings [@" maximumCallsPerCallGroup" ]) {
362- providerConfiguration.maximumCallsPerCallGroup = [_settings [@" maximumCallsPerCallGroup" ] integerValue ];
402+ if (settings [@" maximumCallsPerCallGroup" ]) {
403+ providerConfiguration.maximumCallsPerCallGroup = [settings [@" maximumCallsPerCallGroup" ] integerValue ];
363404 }
364- if (_settings [@" imageName" ]) {
365- providerConfiguration.iconTemplateImageData = UIImagePNGRepresentation ([UIImage imageNamed: _settings [@" imageName" ]]);
405+ if (settings [@" imageName" ]) {
406+ providerConfiguration.iconTemplateImageData = UIImagePNGRepresentation ([UIImage imageNamed: settings [@" imageName" ]]);
366407 }
367- if (_settings [@" ringtoneSound" ]) {
368- providerConfiguration.ringtoneSound = _settings [@" ringtoneSound" ];
408+ if (settings [@" ringtoneSound" ]) {
409+ providerConfiguration.ringtoneSound = settings [@" ringtoneSound" ];
369410 }
370411 return providerConfiguration;
371412}
0 commit comments