0
votes

I have followed the instructions (I believe) Use your app delegate to share info between objects

but I keep getting the following errors:

[AppDelegate setBackgroundAudio:]: unrecognized selector sent to instance 0x10d822400 * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[AppDelegate setBackgroundAudio:]: unrecognized selector sent to instance 0x10d822400'

MyAppDelegate.h

@interface MyAppDelegate : NSObject{
    AVAudioPlayer *backgroundAudio;
}

@property (strong, nonatomic) AVAudioPlayer *backgroundAudio;

@end

MyAppDelegate.m

@implementation MyAppDelegate
@synthesize backgroundAudio;
@end

ViewController.m

MyAppDelegate *app_delegate = (MyAppDelegate*) [UIApplication sharedApplication].delegate;

backgroundAudioPath = [[NSBundle mainBundle] pathForResource:@"music" ofType:@"mp3"];

backgroundAudioURL = [NSURL fileURLWithPath:backgroundAudioPath];
app_delegate.backgroundAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:backgroundAudioURL error:Nil];

[app_delegate.backgroundAudio setDelegate:self];
[app_delegate.backgroundAudio setNumberOfLoops:-1];
[app_delegate.backgroundAudio prepareToPlay];

if (app_delegate.backgroundAudio != nil){
    [app_delegate.backgroundAudio stop];
    [app_delegate.backgroundAudio setCurrentTime:0];
    [app_delegate.backgroundAudio play];
}
1

1 Answers

1
votes

You've made MyAppDelegate a subclass of NSObject, so it's in no way (other than it's name) your app delegate.

When you get your real app delegate and cast it as MyAppDelegate, you've now got an object that you've told the compiler implements backgroundAudio, but it doesn't. The quick remedy is to redeclare MyAppDelegate as a subclass of UIResponder, implementing the <UIApplicationDelegate> protocol.

Be sure to tell your app main about it, like this:

// main.m
// all the usual stuff from your main.m

return UIApplicationMain(argc, argv, nil, NSStringFromClass([MyAppDelegate class]));