codeIgnitor
codeIgnitor

Reputation: 765

Can I detect if the System(iOS) is on Call using Call Kit

I have developed a Video Calling application using Twilio Video SDK, I have a scenario where if the iPhone is on another call(Voice call) I need to detect it and send busy status(recipient on another call) to my application. Is there any way I can do this with CallKit

Currently what is happening?

If the recipient is on another call (Voice call), and my application calls the recipient both call goes on with microphone being used by voice call.

What I want?

Detect if system is on call(voice call) so that I can perform needed action based on this

Upvotes: 1

Views: 5403

Answers (2)

Miki
Miki

Reputation: 921

You can, by implementing CXCallObserver and using it's delegate method callObserver(_ callChanged:)

Another solution is to implement so called audio interruption notification function that will handle external audio interruptions that your application can respond to, like receiving an incoming call, alarms, etc.

Steps

First, add observer

NotificationCenter.default.addObserver(self, selector: #selector(ViewController.handleInterruption(notification:)), name: NSNotification.Name.AVAudioSessionInterruption, object: theSession)

And implement notification handler

func handleInterruption(notification: NSNotification) {
    guard let value = (notification.userInfo?[AVAudioSessionInterruptionTypeKey] as? NSNumber)?.uintValue,
        let interruptionType =  AVAudioSessionInterruptionType(rawValue: value) else {
            return
        }
    switch interruptionType {
    case .began:
        // stop app audio here
        break
    case .ended:
        // resume app audio here
        break
    }
}

My advice to you is to use CallKit's CXCallObserver, since it responds well to external call events.

Upvotes: 0

Sharad Chauhan
Sharad Chauhan

Reputation: 4891

Please refer this link : Find if user is in a call or not?

Also you can use CallKit and can combine logic to find many values :

func callObserver(_ callObserver: CXCallObserver, callChanged call: CXCall) {

   if call.hasEnded == true {
       print("CXCallState: Disconnected")
   }

   if call.isOutgoing == true && call.hasConnected == false {
       print("CXCallState: Dialing")
   }

   if call.isOutgoing == false && call.hasConnected == false && call.hasEnded == false {
       print("CXCallState: Incoming")
   }

   if call.hasConnected == true && call.hasEnded == false {
       print("CXCallState: Connected")
   }
}

Upvotes: 2

Related Questions