Skip to content

Commit 79cbd48

Browse files
Merge pull request #39 from dowatugkins/fix/iosBridge
Changed to use CallKit instead of deprecated API.
2 parents 7c47b4d + 4298b98 commit 79cbd48

File tree

3 files changed

+28
-45
lines changed

3 files changed

+28
-45
lines changed

iOS/RCTCallDetection/RCTCallDetection.xcodeproj/project.pbxproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@
105105
developmentRegion = English;
106106
hasScannedForEncodings = 0;
107107
knownRegions = (
108+
English,
108109
en,
109110
);
110111
mainGroup = 21AAD4EC1EF50F19002E2B59;

iOS/RCTCallDetection/RCTCallDetection/CallDetectionManager.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
//
44
//
55
// Created by Pritesh Nandgaonkar on 16/06/17.
6+
// Updated by Doug Watkins for Inside Real Estate on 31/07/19
67
// Copyright © 2017 Facebook. All rights reserved.
78
//
89
//
@@ -18,8 +19,7 @@
1819
#import <React/RCTEventEmitter.h>
1920
#endif
2021
#import <Foundation/Foundation.h>
21-
#import <CoreTelephony/CTCallCenter.h>
22-
#import <CoreTelephony/CTCall.h>
22+
#import <CallKit/CallKit.h>
2323

24-
@interface CallDetectionManager : RCTEventEmitter <RCTBridgeModule>
24+
@interface CallDetectionManager : RCTEventEmitter <RCTBridgeModule, CXCallObserverDelegate>
2525
@end

iOS/RCTCallDetection/RCTCallDetection/CallDetectionManager.m

Lines changed: 24 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,22 @@
33
//
44
//
55
// Created by Pritesh Nandgaonkar on 16/06/17.
6+
// Updated by Doug Watkins for Inside Real Estate on 31/07/19
67
// Copyright © 2017 Facebook. All rights reserved.
78
//
89

910
#import "CallDetectionManager.h"
10-
@import CoreTelephony;
11+
@import CallKit;
1112

1213
typedef void (^CallBack)();
1314
@interface CallDetectionManager()
1415

1516
@property(strong, nonatomic) RCTResponseSenderBlock block;
16-
@property(strong, nonatomic, nonnull) CTCallCenter *callCenter;
17+
@property(strong, nonatomic) CXCallObserver* callObserver;
1718

1819
@end
19-
2020
@implementation CallDetectionManager
2121

22-
- (NSDictionary *)constantsToExport
23-
{
24-
return @{
25-
@"Connected" : @"Connected",
26-
@"Dialing" : @"Dialing",
27-
@"Disconnected": @"Disconnected",
28-
@"Incoming" : @"Incoming"
29-
};
30-
}
31-
3222
- (NSArray<NSString *> *)supportedEvents {
3323
return @[@"PhoneCallStateUpdate"];
3424
}
@@ -40,46 +30,38 @@ + (BOOL)requiresMainQueueSetup {
4030
RCT_EXPORT_MODULE()
4131

4232
RCT_EXPORT_METHOD(addCallBlock:(RCTResponseSenderBlock) block) {
43-
// Setup call tracking
44-
self.block = block;
45-
self.callCenter = [[CTCallCenter alloc] init];
46-
__typeof(self) weakSelf = self;
47-
self.callCenter.callEventHandler = ^(CTCall *call) {
48-
[weakSelf handleCall:call];
49-
};
33+
// Setup call tracking
34+
self.block = block;
35+
self.callObserver = [[CXCallObserver alloc] init];
36+
__typeof(self) weakSelf = self;
37+
[self.callObserver setDelegate:weakSelf queue:nil];
5038
}
5139

5240
RCT_EXPORT_METHOD(startListener) {
5341
// Setup call tracking
54-
self.callCenter = [[CTCallCenter alloc] init];
42+
self.callObserver = [[CXCallObserver alloc] init];
5543
__typeof(self) weakSelf = self;
56-
self.callCenter.callEventHandler = ^(CTCall *call) {
57-
[weakSelf handleCall:call];
58-
};
44+
[self.callObserver setDelegate:weakSelf queue:nil];
5945
}
6046

6147
RCT_EXPORT_METHOD(stopListener) {
6248
// Setup call tracking
63-
self.callCenter = nil;
49+
self.callObserver = nil;
6450
}
6551

66-
- (void)handleCall:(CTCall *)call {
67-
68-
NSDictionary *eventNameMap = @{
69-
CTCallStateConnected : @"Connected",
70-
CTCallStateDialing : @"Dialing",
71-
CTCallStateDisconnected : @"Disconnected",
72-
CTCallStateIncoming : @"Incoming"
73-
};
74-
75-
_callCenter = [[CTCallCenter alloc] init];
76-
77-
[_callCenter setCallEventHandler:^(CTCall *call) {
78-
[self sendEventWithName:@"PhoneCallStateUpdate"
79-
body:[eventNameMap objectForKey: call.callState]];
80-
}];
81-
[self sendEventWithName:@"PhoneCallStateUpdate"
82-
body:[eventNameMap objectForKey: call.callState]];
52+
- (void)callObserver:(CXCallObserver *)callObserver callChanged:(CXCall *)call {
53+
if (call.hasEnded == true) {
54+
[self sendEventWithName:@"PhoneCallStateUpdate" body:@"Disconnected"];
55+
}
56+
if (call.isOutgoing == true && call.hasConnected == false && call.hasEnded == false) {
57+
[self sendEventWithName:@"PhoneCallStateUpdate" body:@"Dialing"];
58+
}
59+
if (call.isOutgoing == false && call.hasConnected == false) {
60+
[self sendEventWithName:@"PhoneCallStateUpdate" body:@"Incoming"];
61+
}
62+
if (call.hasEnded == false && call.hasConnected == true) {
63+
[self sendEventWithName:@"PhoneCallStateUpdate" body:@"Connected"];
64+
}
8365
}
8466

8567
@end

0 commit comments

Comments
 (0)