I’m new to objective c (moving from .NET developing to iPhone). Well Now I have a simple question for someone who is experienced in iPhone development.
I’m writing a static library which reacts on some audio events using audiotoolbox. My header looks like:
@interface ccreaderlib : NSObject {
id __unsafe_unretained delegate;
SEL _DevicePluggedEvent;
SEL _DeviceUnpluggedEvent;
}
@property (nonatomic, assign) id delegate;
@property (nonatomic, assign) SEL onDevicePlugged;
@property (nonatomic, assign) SEL onDeviceUnplugged;
- (id)init;
- (void)startMonitor;
- (void)stopMonitor;
@end
now in UIViewController I’m doing this:
_lib = [[ccreaderlib alloc] init];
_lib.delegate = self;
_lib.onDevicePlugged = @selector(OnDevicePluggedIn);
_lib.onDeviceUnplugged = @selector(OnDeviceUnplugged);
[_lib startMonitor];
My idea is to call UIViewController’s selectors from my static library. How can I do this. At the moment I’m trying to do this in this way:
void audioRouteChangeListenerCallback (
void *inUserData,
AudioSessionPropertyID inPropertyID,
UInt32 inPropertyValueSize,
const void *inPropertyValue)
{
if (inPropertyID != kAudioSessionProperty_AudioRouteChange) return;
CFDictionaryRef routeChangeDictionary = inPropertyValue;
CFNumberRef routeChangeReasonRef =
CFDictionaryGetValue ( routeChangeDictionary, CFSTR (kAudioSession_AudioRouteChangeKey_Reason));
SInt32 routeChangeReason;
CFNumberGetValue (
routeChangeReasonRef,
kCFNumberSInt32Type,
&routeChangeReason);
CFStringRef oldRouteRef =
CFDictionaryGetValue (
routeChangeDictionary,
CFSTR (kAudioSession_AudioRouteChangeKey_OldRoute));
NSString *oldRouteString = (__bridge NSString *)oldRouteRef;
ccreaderlib *self = (__bridge id)inUserData;
id reactClass = [self delegate];
if (routeChangeReason == kAudioSessionRouteChangeReason_NewDeviceAvailable)
{
if ([oldRouteString isEqualToString:@"Speaker"])
{
[reactClass performSelector:@selector(onDevicePlugged)];
self.bIsReaderPlugged = YES;
}
}
if (routeChangeReason == kAudioSessionRouteChangeReason_OldDeviceUnavailable) {
if ([oldRouteString isEqualToString:@"Headphone"]){
[reactClass performSelector:@selector(onDeviceUnplugged)];
self.bIsReaderPlugged = NO;
}
}
}
but in this way I get SIGABRT. Please help me,spent 3 days already trying to solve this.
***UPDATE:
Found answer myself. Changed my c callback function to look this way:
if (routeChangeReason == kAudioSessionRouteChangeReason_NewDeviceAvailable)
{
if ([oldRouteString isEqualToString:@"Speaker"])
{
if([self.delegate respondsToSelector:self.onDevicePlugged])
[self.delegate performSelector:self.onDevicePlugged];
self.bIsReaderPlugged = YES;
}
}
if (routeChangeReason == kAudioSessionRouteChangeReason_OldDeviceUnavailable) {
if ([oldRouteString isEqualToString:@"Headphone"]){
if([self.delegate respondsToSelector:self.onDeviceUnplugged])
[self.delegate performSelector:self.onDeviceUnplugged];
self.bIsReaderPlugged = NO;
}
}
thanks to http://brandontreb.com/objective-c-programming-tutorial-creating-a-twitter-client-part-1/
Are you sure you got the spelling correct – in one case it appears capitalized in the other not (first ‘o’).
What you should do in the class, at the very start, is verify the delegate does in fact repond to the selector:
I would assert on these during development, as no reason to proceed if you have not gotten this correct,