diff --git a/README.md b/README.md index b574fef..38f9457 100644 --- a/README.md +++ b/README.md @@ -137,6 +137,8 @@ Initialise RNCallKit with options - **hasVideo**: boolean (optional) - false (default) - **localizedCallerName**: string (optional) +- **showConnectionState**: boolean (optional) + - `false` by default, set it to `true` if you wish to show the user connecting state after hitting the answer button. Note that you will need to update the connection state using `updateConnectionState(connected: boolean)`. Call when you receive incoming calls to display system UI @@ -174,6 +176,14 @@ Checks if there are any active calls on the device and returns a promise with a Checks if the device speaker is on and returns a promise with a boolean value (`true` if speaker is on, `false` otherwise). +### updateConnectionState + +Note: you don't need to call this method if you didn't set `showConnectionState` to `true` while displaying a call. + +Call this method when you establish or lose connection while answering. It transitions a call from `connecting` state to either `connected` or `failed`. + +- **connected**: boolean (default value is `true`) + ## Events ### - didReceiveStartCallAction @@ -198,6 +208,8 @@ User answer the incoming call Do your normal `Answering` actions here +Note: This is the place you might want to call `updateConnectionState` if you set `showConnectionState` to `true` on displaying a call. + **data**: ```javascript @@ -288,6 +300,7 @@ class RNCallKitExample extends React.Component { * Try to do your normal Answering actions here * * e.g. this.handleAnswerCall(data.callUUID); + * */ } diff --git a/index.js b/index.js index 78c5642..adaf181 100644 --- a/index.js +++ b/index.js @@ -79,12 +79,12 @@ export default class RNCallKit { _RNCallKit.setup(options); } - static displayIncomingCall(uuid, handle, handleType = 'number', hasVideo = false, localizedCallerName?: String) { + static displayIncomingCall(uuid, handle, handleType = 'number', hasVideo = false, localizedCallerName = '', showConnectionState = false) { if (Platform.OS !== 'ios') return; - _RNCallKit.displayIncomingCall(uuid, handle, handleType, hasVideo, localizedCallerName); + _RNCallKit.displayIncomingCall(uuid, handle, handleType, hasVideo, localizedCallerName, showConnectionState); } - static startCall(uuid, handle, handleType = 'number', hasVideo = false, contactIdentifier?: String) { + static startCall(uuid, handle, handleType = 'number', hasVideo = false, contactIdentifier) { if (Platform.OS !== 'ios') return; _RNCallKit.startCall(uuid, handle, handleType, hasVideo, contactIdentifier); } @@ -121,6 +121,11 @@ export default class RNCallKit { : Promise.reject('RNCallKit.checkSpeaker was called from unsupported OS'); } + static updateConnectionState(connected = true) { + if (Platform.OS !== 'ios') return; + _RNCallKit.updateConnectionState(connected); + } + /* static setHeldCall(uuid, onHold) { if (Platform.OS !== 'ios') return; diff --git a/ios/RNCallKit/RNCallKit.m b/ios/RNCallKit/RNCallKit.m index fc3439f..6190b35 100644 --- a/ios/RNCallKit/RNCallKit.m +++ b/ios/RNCallKit/RNCallKit.m @@ -30,6 +30,8 @@ @implementation RNCallKit NSMutableDictionary *_settings; NSOperatingSystemVersion _version; BOOL _isStartCallActionEventListenerAdded; + CXAnswerCallAction *_answerCallAction; + BOOL _shouldShowConnectionState; } // should initialise in AppDelegate.m @@ -46,6 +48,7 @@ - (instancetype)init name:RNCallKitHandleStartCallNotification object:nil]; _isStartCallActionEventListenerAdded = NO; + _shouldShowConnectionState = NO; } return self; } @@ -111,7 +114,8 @@ - (void)dealloc handle:(NSString *)handle handleType:(NSString *)handleType hasVideo:(BOOL)hasVideo - localizedCallerName:(NSString * _Nullable)localizedCallerName) + localizedCallerName:(NSString *)localizedCallerName + showConnectionState:(BOOL)showConnectionState) { #ifdef DEBUG NSLog(@"[RNCallKit][displayIncomingCall] uuidString = %@", uuidString); @@ -126,7 +130,10 @@ - (void)dealloc callUpdate.supportsGrouping = NO; callUpdate.supportsUngrouping = NO; callUpdate.hasVideo = hasVideo; - callUpdate.localizedCallerName = localizedCallerName; + if ([localizedCallerName length] > 0) { + callUpdate.localizedCallerName = localizedCallerName; + } + _shouldShowConnectionState = showConnectionState; [self.callKitProvider reportNewIncomingCallWithUUID:uuid update:callUpdate completion:^(NSError * _Nullable error) { [self sendEventWithName:RNCallKitDidDisplayIncomingCall body:@{ @"error": error ? error.localizedDescription : @"" }]; @@ -221,6 +228,18 @@ - (void)dealloc [self requestTransaction:transaction]; } +RCT_EXPORT_METHOD(updateConnectionState:(BOOL )connected) +{ +#ifdef DEBUG + NSLog(@"[RNCallKit][updateConnectionState]"); +#endif + if (connected) { + [_answerCallAction fulfill]; + } else { + [_answerCallAction fail]; + } +} + - (void)requestTransaction:(CXTransaction *)transaction { #ifdef DEBUG @@ -432,7 +451,11 @@ - (void)provider:(CXProvider *)provider performAnswerCallAction:(CXAnswerCallAct } NSString *callUUID = [self containsLowerCaseLetter:action.callUUID.UUIDString] ? action.callUUID.UUIDString : [action.callUUID.UUIDString lowercaseString]; [self sendEventWithName:RNCallKitPerformAnswerCallAction body:@{ @"callUUID": callUUID }]; - [action fulfill]; + if (_shouldShowConnectionState) { + _answerCallAction = action; + } else { + [action fullfill]; + } } // Ending incoming call