2

I'm trying to detect call states on iOS 10. I have tested this on iOS 9.3 and it works fine. But on iOS 10, CTCallCenter is deprecated, so I used Callkit.

I can't detect the call state. I can't find any correct answers.

My code is:

@property (nonatomic, strong) CXCallObserver *callObserver;

...

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

...

- (void)callObserver:(CXCallObserver *)callObserver callChanged:(CXCall *)call {
    if (call.hasConnected) {
        NSLog(@"connected/n");
        // perform necessary actions
    } else if(call.hasEnded) {
         NSLog(@"disconnected/n");
    }
}
jscs
  • 63,694
  • 13
  • 151
  • 195
W.venus
  • 470
  • 2
  • 6
  • 19

2 Answers2

2

This code is working for me. The only difference is I provide a dispatch queue for the observer, instead of using the main queue.

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

@interface CallStatusIos10 : NSObject <CXCallObserverDelegate>
{
    dispatch_queue_t dq;
}
@property (nonatomic, strong) CXCallObserver* observer;

@end

@implementation CallStatusIos10

@synthesize observer;

-(id) init
{
    if(self = [super init])
    {
        observer = [[CXCallObserver alloc] init];
        // Hang on to a reference to the queue, as the
        // CXCallObserver only maintains a weak reference
        dq = dispatch_queue_create("my.ios10.call.status.queue", DISPATCH_QUEUE_SERIAL);
        [observer setDelegate: self queue: dq];
    }
    return self;
}

#pragma mark - CXCallObserverDelegate

- (void)callObserver:(CXCallObserver *)callObserver callChanged:(CXCall *)call
{
    NSString* callId = [call.UUID UUIDString];

    if(!call.hasConnected && !call.hasEnded && !call.onHold)
    {
        if(call.outgoing)
        {
            // outgoing call is being dialling;
        }
        else
        {
            // incoming call is ringing;
        }
    }
    else if(call.onHold)
    {
        // call is on hold;
    }
    else if(call.hasEnded)
    {
        // call has disconnected;
    }
    else if(call.hasConnected)
    {
        // call has been answered;
    }
}

@end
BitByteDog
  • 3,074
  • 2
  • 26
  • 39
-1

Try this

Appdelegate.h

#import <CoreTelephony/CTCallCenter.h>
#import <CoreTelephony/CTCall.h>
...
@property (nonatomic, strong) CTCallCenter* callCenter;

AppDelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{  
   .... 
  self.callCenter = [[CTCallCenter alloc] init];
  [self handleCall]; //Call this method when you want to use it
   .... 
}

-(void)handleCall
{
    self.callCenter.callEventHandler = ^(CTCall *call){

       if ([call.callState isEqualToString: CTCallStateConnected])
       {
          //NSLog(@"call stopped");
       }
       else if ([call.callState isEqualToString: CTCallStateDialing])
       {
        }
       else if ([call.callState isEqualToString: CTCallStateDisconnected])
       {
          //NSLog(@"call played");
        }
       else if ([call.callState isEqualToString: CTCallStateIncoming])
       {
           //NSLog(@"call stopped");
       }
     };
  }

Taken from: How to detect call incoming programmatically

To know more: CoreTelephony Framework iOS 7

Community
  • 1
  • 1
Jamshed Alam
  • 12,424
  • 5
  • 26
  • 49