Using AVAudioSession, get the availableInputs. The return value is an array of AVAudioSessionPortDescriptions. Iterate through the array checking the portType property to match your preferred port type, then set the preferredInput using the port description.
Swift:
let audioSession = AVAudioSession.sharedInstance()
if let desc = audioSession.availableInputs?.first(where: { (desc) -> Bool in
return desc.portType == AVAudioSessionPortUSBAudio
}){
do{
try audioSession.setPreferredInput(desc)
} catch let error{
print(error)
}
}
Objective-C:
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
NSString *preferredPortType = AVAudioSessionPortUSBAudio;
for (AVAudioSessionPortDescription *desc in audioSession.availableInputs) {
if ([desc.portType isEqualToString: preferredPortType]) {
[audioSession setPreferredInput:desc error:nil];
}
}