3
3
//
4
4
//
5
5
// Created by Pritesh Nandgaonkar on 16/06/17.
6
+ // Updated by Doug Watkins for Inside Real Estate on 31/07/19
6
7
// Copyright © 2017 Facebook. All rights reserved.
7
8
//
8
9
9
10
#import " CallDetectionManager.h"
10
- @import CoreTelephony ;
11
+ @import CallKit ;
11
12
12
13
typedef void (^CallBack)();
13
14
@interface CallDetectionManager ()
14
15
15
16
@property (strong , nonatomic ) RCTResponseSenderBlock block;
16
- @property (strong , nonatomic , nonnull ) CTCallCenter *callCenter ;
17
+ @property (strong , nonatomic ) CXCallObserver * callObserver ;
17
18
18
19
@end
19
-
20
20
@implementation CallDetectionManager
21
21
22
- - (NSDictionary *)constantsToExport
23
- {
24
- return @{
25
- @" Connected" : @" Connected" ,
26
- @" Dialing" : @" Dialing" ,
27
- @" Disconnected" : @" Disconnected" ,
28
- @" Incoming" : @" Incoming"
29
- };
30
- }
31
-
32
22
- (NSArray <NSString *> *)supportedEvents {
33
23
return @[@" PhoneCallStateUpdate" ];
34
24
}
@@ -40,46 +30,38 @@ + (BOOL)requiresMainQueueSetup {
40
30
RCT_EXPORT_MODULE ()
41
31
42
32
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 ];
50
38
}
51
39
52
40
RCT_EXPORT_METHOD (startListener) {
53
41
// Setup call tracking
54
- self.callCenter = [[CTCallCenter alloc ] init ];
42
+ self.callObserver = [[CXCallObserver alloc ] init ];
55
43
__typeof (self) weakSelf = self;
56
- self.callCenter .callEventHandler = ^(CTCall *call) {
57
- [weakSelf handleCall: call];
58
- };
44
+ [self .callObserver setDelegate: weakSelf queue: nil ];
59
45
}
60
46
61
47
RCT_EXPORT_METHOD (stopListener) {
62
48
// Setup call tracking
63
- self.callCenter = nil ;
49
+ self.callObserver = nil ;
64
50
}
65
51
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
+ }
83
65
}
84
66
85
67
@end
0 commit comments