diff --git a/ios/RNCallKeep/RNCallKeep.h b/ios/RNCallKeep/RNCallKeep.h index c8e7bbdf..1eec7908 100644 --- a/ios/RNCallKeep/RNCallKeep.h +++ b/ios/RNCallKeep/RNCallKeep.h @@ -47,4 +47,6 @@ continueUserActivity:(NSUserActivity *)userActivity + (void)setup:(NSDictionary *)options; ++ (void)setup:(NSDictionary *)options callRejectHandler: (void (^) (NSString* uuid, void (^completion)(void))) onReject; + @end diff --git a/ios/RNCallKeep/RNCallKeep.m b/ios/RNCallKeep/RNCallKeep.m index 4864d258..46afd0a6 100644 --- a/ios/RNCallKeep/RNCallKeep.m +++ b/ios/RNCallKeep/RNCallKeep.m @@ -43,7 +43,8 @@ @implementation RNCallKeep bool _hasListeners; NSMutableArray *_delayedEvents; } - +NSMutableDictionary *_answeredCalls; +void (^onRejectHandler) (NSString* uuid, void (^completion)(void)); static bool isSetupNatively; static CXProvider* sharedProvider; @@ -81,7 +82,9 @@ - (void)dealloc if (self.callKeepProvider != nil) { [self.callKeepProvider invalidate]; } + onRejectHandler = nil; sharedProvider = nil; + _answeredCalls = nil; } // Override method of RCTEventEmitter @@ -142,6 +145,19 @@ + (void)setup:(NSDictionary *)options { isSetupNatively = YES; } ++ (void)setup:(NSDictionary *)options callRejectHandler: (void (^) (NSString* uuid, void (^completion)(void))) onReject { + RNCallKeep *callKeep = [RNCallKeep allocWithZone: nil]; + [callKeep setup:options]; + isSetupNatively = YES; + + if (onReject != nil) { + onRejectHandler = onReject; + } + if (_answeredCalls == nil) { + _answeredCalls = [[NSMutableDictionary alloc] init]; + } +} + RCT_EXPORT_METHOD(setup:(NSDictionary *)options) { if (isSetupNatively) { @@ -521,6 +537,8 @@ + (void)reportNewIncomingCall:(NSString *)uuidString callUpdate.localizedCallerName = localizedCallerName; [RNCallKeep initCallKitProvider]; + [_answeredCalls setValue:NO forKey:uuidString]; + [sharedProvider reportNewIncomingCallWithUUID:uuid update:callUpdate completion:^(NSError * _Nullable error) { RNCallKeep *callKeep = [RNCallKeep allocWithZone: nil]; [callKeep sendEventWithNameWrapper:RNCallKeepDidDisplayIncomingCall body:@{ @@ -768,6 +786,7 @@ - (void)provider:(CXProvider *)provider performAnswerCallAction:(CXAnswerCallAct #ifdef DEBUG NSLog(@"[RNCallKeep][CXProviderDelegate][provider:performAnswerCallAction]"); #endif + [_answeredCalls setValue:[NSNumber numberWithBool:YES] forKey:action.callUUID.UUIDString]; [self configureAudioSession]; [self sendEventWithNameWrapper:RNCallKeepPerformAnswerCallAction body:@{ @"callUUID": [action.callUUID.UUIDString lowercaseString] }]; [action fulfill]; @@ -780,7 +799,14 @@ - (void)provider:(CXProvider *)provider performEndCallAction:(CXEndCallAction *) NSLog(@"[RNCallKeep][CXProviderDelegate][provider:performEndCallAction]"); #endif [self sendEventWithNameWrapper:RNCallKeepPerformEndCallAction body:@{ @"callUUID": [action.callUUID.UUIDString lowercaseString] }]; - [action fulfill]; + + if (onRejectHandler != nil && _answeredCalls[action.callUUID.UUIDString] == NO) { + onRejectHandler(action.callUUID.UUIDString, ^() { + [action fulfill]; + }); + } else { + [action fulfill]; + } } -(void)provider:(CXProvider *)provider performSetHeldCallAction:(CXSetHeldCallAction *)action