Enable loudspeaker programmatically during a voip call
Swift 3.0
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayAndRecord, mode: AVAudioSessionModeVoiceChat, options: .defaultToSpeaker)
try AVAudioSession.sharedInstance().overrideOutputAudioPort(.speaker)
try AVAudioSession.sharedInstance().setActive(true)
Look up the kAudioSessionProperty_OverrideAudioRoute key and the kAudioSessionOverrideAudioRoute_Speaker property in Apple's Audio Session documentation.