Swift iOS CallKit audio resource contention

I noticed the following behavior with CallKit when receiving a VolP push notification:

  1. When the app is in the foreground and a CallKit incoming call banner appears, pressing the answer button directly causes the speaker indicator in the CallKit interface to turn on. However, the audio is not actually activated (the iPhone's orange microphone indicator does not light up).

  2. In the same foreground scenario, if I expand the CallKit banner before answering the call, the speaker indicator does not turn on, but the orange microphone indicator does light up, and audio works as expected.

  3. When the app is in the background or not running, the incoming call banner works as expected: I can answer the call directly without expanding the banner, and the speaker does not turn on automatically. The orange microphone indicator lights up as it should.

Why is there a difference in behavior between answering directly from the banner versus expanding it first when the app is in the foreground? Is there a way to ensure consistent audio activation behavior across these scenarios?

I tried reconfiguring the audio when answering a call, but an error occurred during setActive, preventing the configuration from succeeding.

    
    let audioSession = AVAudioSession.sharedInstance()
    do {
        try audioSession.setActive(false)
        try audioSession.setCategory(.playAndRecord, mode: .voiceChat, options: [.defaultToSpeaker])
        try audioSession.setActive(true, options: [])
    } catch {
        print("Failed to activate audio session: \(error)")
    }
    
    action.fulfill()

}

Error Domain=NSOSStatusErrorDomain Code=561017449 "Session activation failed" UserInfo={NSLocalizedDescription=Session activation failed}

In the foreground, if I use a custom incoming call UI to initiate the call first and then connect to CallKit, it seems to resolve the issue where the microphone cannot be activated properly. But is this approach reasonable?

Swift iOS CallKit audio resource contention
 
 
Q