Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS 10 : How can I get a call event using CallKit/CXCallObserver?

I changed the CTCallCenter with CXCallObserver in iOS 10.

Here is my code:

#import <CallKit/CXCallObserver.h>
#import <CallKit/CXCall.h>

-(void)viewDidLoad {

    CXCallObserver *callObserver = [[CXCallObserver alloc] init];
    [callObserver setDelegate:self queue:nil];

    ... ...
}

- (void)callObserver:(CXCallObserver *)callObserver callChanged:(CXCall *)call {
    if (call.hasConnected) {
        NSLog(@"********** voice call connected **********/n");        
    } else if(call.hasEnded) {
        NSLog(@"********** voice call disconnected **********/n");        
    }
}

But I can't get a voice call event and I got a warning like this:

Sending 'HomeViewController *const __strong' to parameter of incompatible type 'id<CXCallObserverDelegate> _Nullable

Please help me.

like image 608
W.venus Avatar asked Oct 10 '16 02:10

W.venus


2 Answers

Don't forget to store strong reference to callObserver, so it won't be released too early:

@interface YourClass ()<CXCallObserverDelegate>
@property (nonatomic, strong) CXCallObserver *callObserver;
@end

- (void)viewDidLoad {
    [super viewDidLoad];

    CXCallObserver *callObserver = [[CXCallObserver alloc] init];
    [callObserver setDelegate:self queue:nil];
    self.callObserver = callObserver;
}

For more information, check this answer.

like image 107
Maxim Pavlov Avatar answered Oct 23 '22 18:10

Maxim Pavlov


You missed the CXCallObserverDelegate.

@interface HomeViewController : UIViewController <CXCallObserverDelegate>

@end

Then the warning will disappear and you get a voice call event. I hope this help you.

like image 35
J.YG Avatar answered Oct 23 '22 17:10

J.YG