preface

During the recent iterative update of the project, I happened to find that the SDK for making, answering and hanging phone calls in the project had been abandoned, so I tried to replace it with a new SDK. I noticed that there was little relevant content on the Internet, so I recorded my own practice.

warning

This API has been disabled in China. At present, the old phone listening API can still be used on iOS 14. It is recommended to change back

scenario

If the APP is used for a long time, such as watching videos or live broadcasting by anchors, the situation that the Internet may be disconnected due to incoming calls needs to be dealt with in time. After the user comes back, the scene can be restored.

In actual combat

The preparatory work

Header file import:

#import <CallKit/CXCallObserver.h>
#import <CallKit/CXCall.h>
Copy the code

Create a global listener that can be removed or recreated after you leave the page or jump back in

@property (nonatomic, strong) CXCallObserver *callObserver;Copy the code

Protocol needs to be added

CXCallObserverDelegate
Copy the code

Initialize the

Since my use scenario is the live broadcast room, there is a situation that the live broadcast room will jump to user details, so in order not to write duplicate code, I put the initialization into the viewWillApear method

- (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; If (! self.callObserver) { self.callObserver = [[CXCallObserver alloc] init]; [self.callObserver setDelegate:self queue:dispatch_get_main_queue()]; }}Copy the code

If you don’t need a separate page to jump to, you can just put it in viewDidLoad

State monitoring

#pragma mark - call listener - (void)callObserver:(CXCallObserver *)callObserver callChanged:(CXCall *)call {NSLog(@"call observer uuid: %@", call.UUID); NSLog(@"outgoing :%d onHold :%d hasConnected :%d hasEnded :%d",call.outgoing,call.onHold,call.hasConnected,call.hasEnded); / * https://developer.apple.com/documentation/callkit/cxcall hang up outgoing (call) : 0 onHold (to allow) : 0 hasConnected (on) : 0 HasEnded :1 Outgoing :1 onHold :0 hasConnected :0 hasEnded :0 Outgoing :1 onHold :1 outgoing :1 onHold :0 :0 hasConnected :0 hasEnded :1 outgoing :1 onHold :0 hasConnected :1 hasEnded :0 Outgoing :0 hasConnected :0 hasEnded :0 - Outgoing :1 onHold :0 hasConnected :1 hasEnded :1 Outgoing :0 onHold :0 :0 hasConnected :1 hasEnded :0 Outgoing :0 onHold :0 hasConnected :1 hasEnded :1 Outgoing :0 onHold :0 hasConnected :1 hasEnded :1 * / if ((call. Outgoing & & call. HasConnected) | | call. HasConnected) {/ / active dial or on the phone dispatch_async(dispatch_get_main_queue(), ^{ }); } the if ((call hasConnected && call. HasEnded) | | (call. Outgoing & & call. HasEnded)) {/ / that is to meet and hang up or by telephone call and hang up dispatch_async(dispatch_get_main_queue(), ^{ }); return; }}Copy the code

It should be noted that this method cannot judge the current state by a single state, and multi-value judgment is recommended

Cancel to monitor

When away from the page or no longer needed, simply execute the following code to empty the listener and no longer enter the listener

- (void)viewWillDisappear:(BOOL)animated { [super viewWillDisappear:animated]; if (self.callObserver) { [self.callObserver setDelegate:nil queue:dispatch_get_main_queue()]; self.callObserver = nil; }}Copy the code

conclusion

Although it is a simple function, sometimes it is easy to walk into the pit when you can’t find the information. I hope this article can help more peers save time.