Cocos2D 2.1: "Delegate" deprecated in iOS 6. How do I set the delegate for this AVAudioSession?

baptzmoffire picture baptzmoffire · Oct 26, 2012 · Viewed 10.7k times · Source

Started a Cocos2D 2.1 template (with no physics engine) in Xcode 4.5, targeted for iOS 6 and iPad. In the CDAudioManager.m file, the following code...

AVAudioSession* session = [AVAudioSession sharedInstance];
session.delegate = self;  // Which is what is automatically generated by the template.

...generates the following warning...

"delegate deprecated:  first deprecated in iOS 6"

So I go to the apple developer documentation, and it says under "delegate," "Deprecated in iOS 6.0. Use the notifications described in the Notifications section of this class instead."

http://developer.apple.com/library/ios/#documentation/AVFoundation/Reference/AVAudioSession_ClassReference/DeprecationAppendix/AppendixADeprecatedAPI.html#//apple_ref/occ/instp/AVAudioSession/delegate

Problem is, it looks to me like all we're trying to do--forgive my inexperience--is set the delegate for the AVAudioSession to the CDAudioManager instance itself. How do the notifications accomplish this? Or am I just wrong about the goal of the above code?

Answer

Jason P. picture Jason P. · Nov 9, 2012

The error you are running into is in this block of code

AVAudioSession* session = [AVAudioSession sharedInstance];
session.delegate = self;// <-------- DEPRECATED IN IOS 6.0

To silence the warning change those 2 lines to this:

[[AVAudioSession sharedInstance] setActive:YES error:nil];

Hope this helps.