Reputation: 2006
I want to detetct wether audio Jack is plugged in or not in my iPhone/iPad device.
I have tried many code Like C function available on stackOverflow but it is not working
tell me code which compitible with iOS 5.0 to 7
if there is any tutorial/code for it? then tell me
Upvotes: 0
Views: 112
Reputation: 2006
After lots of googling i find the best & Simple solution to detect Audio Jack
first of all add following method in your code
- (BOOL)areHeadphonesPluggedIn {
NSArray *availableOutputs = [[AVAudioSession sharedInstance] currentRoute].outputs;
for (AVAudioSessionPortDescription *portDescription in availableOutputs) {
NSLog(@"%@",portDescription.portType);
if ([portDescription.portType isEqualToString:AVAudioSessionPortHeadphones]) {
return YES;
}
}
return NO;
}
then test it like
if ([self areHeadphonesPluggedIn] == TRUE) {
NSLog(@"Device Detetcted. Headphone PluggedIn");
}
else {
NSLog(@"Device Not Detetcted. Headphone PluggedOut");
}
Very Simple!
Upvotes: 6