I have airpods connected to my device and I am trying to switch the audio to speaker. I am using the code below which works when nothing is attached, other bluetooth devices are connected and when headphones are plugged in but it doesn't when there are airpods connected.
do {
try AVAudioSession.sharedInstance().overrideOutputAudioPort(AVAudioSession.PortOverride.speaker)
} catch let error as NSError {
print("audioSession error turning on speaker: \(error.localizedDescription)")
}