When I set:
[[AVAudioSession sharedInstance] setCategory:
AVAudioSessionCategoryPlayAndRecord error:NULL];
…recording and playing works
I ran into this problem today and it seems this answer is a bit outdated. AudioSessionsSetProperty(...) is now deprecated.
The following seems to work though, providing full volume through the speakers but automatically routing the audio to headphones if they're plugged in before the app is run:
[audioSession setCategory:AVAudioSessionCategoryPlayAndRecord withOptions:AVAudioSessionCategoryOptionDefaultToSpeaker error: nil];
And if you'd rather the audio play through the speakers at full volume even if the headphones are plugged in, this works too:
[audioSession overrideOutputAudioPort:AVAudioSessionPortOverrideSpeaker error:nil];