Following the Ray Wenderlich tutorial, I've incorporated the latest AdWhirl folders, deleted the adapters I'm not using, setup an account with iAd, uploaded a few house banners, configured the AdWhirl delivery and implemented the retrieve/display code in my Cocos2d app. My logs show that ads are being retrieved successfully but they do not appear on the screen. I'm thinking either they are displayed off screen (no man's land) or are hidden behind a CCLayer. Any suggestions for how to test/resolve this is much appreciated. It's a landscape only orientation if that helps.
Update: the RW tutorial is out of date for Cocos2d version 2.0 users...see below for the solution
Here is my AdWhirl code:
Library.h
#import "AdWhirlView.h"
#import "AdWhirlDelegateProtocol.h"
#import "RootViewController.h"
@interface Library : CCLayer <AdWhirlDelegate>{
this is what the tutorial said but is outdated
//RootViewController *viewController;
this is what is needed instead
UINavigationController *viewController
AdWhirlView *adWhirlView;
enum GameStatePP _state; }
@property(nonatomic,retain) AdWhirlView *adWhirlView;
@property(nonatomic) enum GameStatePP state;
Library.mm
@implementation Library
@synthesize state = _state, adWhirlView;
In Init
self.state = kGameStatePlaying;
Then the AdWhirl methods
- (void)adWhirlWillPresentFullScreenModal {
if (self.state == kGameStatePlaying) {
[[CCDirector sharedDirector] pause];} }
- (void)adWhirlDidDismissFullScreenModal {
if (self.state == kGameStatePaused)
return;
else {
self.state = kGameStatePlaying;
[[CCDirector sharedDirector] resume]; }}
- (NSString *)adWhirlApplicationKey {
return @"myadWhirlKeyGoesHere"; }
- (UIViewController *)viewControllerForPresentingModalView {
return viewController;}
-(void)adjustAdSize {
[UIView beginAnimations:@"AdResize" context:nil];
[UIView setAnimationDuration:0.2];
CGSize adSize = [adWhirlView actualAdSize];
CGRect newFrame = adWhirlView.frame;
newFrame.size.height = adSize.height;
CGSize winSize = [CCDirector sharedDirector].winSize;
newFrame.size.width = winSize.width;
newFrame.origin.x = (self.adWhirlView.bounds.size.width - adSize.width)/2;
newFrame.origin.y = (winSize.height - adSize.height);
adWhirlView.frame = newFrame;
[UIView commitAnimations]; }
- (void)adWhirlDidReceiveAd:(AdWhirlView *)adWhirlVieww {
[adWhirlView rotateToOrientation:UIInterfaceOrientationLandscapeRight];
[self adjustAdSize];
NSLog(@"Library - adWhirlDidReceiveAd");
NSLog(@"%@",[adWhirlView mostRecentNetworkName]); }
-(void)adWhirlDidFailToReceiveAd:(AdWhirlView *)adWhirlVieww usingBackup:(BOOL)yesOrNo {
NSLog(@"Library - adWhirlDidFailToReceiveAd");
NSLog(@"%@",[adWhirlView mostRecentNetworkName]); }
-(void)onEnter {
this is what the tutorial said and is wrong for version Cocos2d 2.0 users
//viewController = [(AppDelegate *)[[UIApplication sharedApplication] delegate] viewController];
this is the correct way
viewController = [(AppDelegate *)[[UIApplication sharedApplication] delegate] navController];
self.adWhirlView = [AdWhirlView requestAdWhirlViewWithDelegate:self];
self.adWhirlView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin;
[adWhirlView updateAdWhirlConfig];
CGSize adSize = [adWhirlView actualAdSize];
CGSize winSize = [CCDirector sharedDirector].winSize;
self.adWhirlView.frame = CGRectMake((winSize.width/2)-(adSize.width/2),winSize.height-adSize.height,winSize.width,adSize.height);
self.adWhirlView.clipsToBounds = YES;
[viewController.view addSubview:adWhirlView];
[viewController.view bringSubviewToFront:adWhirlView];
[super onEnter]; }
-(void)onExit {
if (adWhirlView) {
[adWhirlView removeFromSuperview];
[adWhirlView replaceBannerViewWith:nil];
[adWhirlView ignoreNewAdRequests];
[adWhirlView setDelegate:nil];
self.adWhirlView = nil;
}
[super onExit]; }
- (void) dealloc {
self.adWhirlView.delegate = nil;
self.adWhirlView = nil; }
In My AppDelegate.h
Wrong:
//RootViewController *viewController;
//UIViewController *viewController;
this is right and is provided in the 2.0 Cocos2d template so doesn't need to be added, just needs to be used:
UINavigationController *navController_;
And so these are not needed
//@property (nonatomic, retain) RootViewController *viewController;
//@property (nonatomic, retain) UIViewController *viewController;
AppDelegate.mm
@synthesize window=window_, navController=navController_, director=director_;
not needed because we will be using the navController instead:
//viewController = viewController_;
DidFinishLaunchingWithOptions (with all references to viewController commented out)
//viewController = [[UIViewController alloc] initWithNibName:nil bundle:nil];
//viewController.wantsFullScreenLayout = YES;
CCGLView *glView = [CCGLView viewWithFrame:[window_ bounds]
pixelFormat:kEAGLColorFormatRGB565
depthFormat:0
preserveBackbuffer:NO
sharegroup:nil
multiSampling:NO
numberOfSamples:0];
director_ = (CCDirectorIOS*) [CCDirector sharedDirector];
director_.wantsFullScreenLayout = YES;
[director_ setDisplayStats:NO];
[director_ setAnimationInterval:1.0/60];
[director_ setDelegate:self];
[director_ setProjection:kCCDirectorProjection2D];
[director_ setView:glView];
if( ! [director_ enableRetinaDisplay:YES] )CCLOG(@"Retina Display Not supported");
navController_ = [[UINavigationController alloc] initWithRootViewController:director_];
navController_.navigationBarHidden = YES;
this is the important line, setting the navController
[window_ addSubview:navController_.view];
[glView setMultipleTouchEnabled:YES];
//[viewController_ setView:glView];
//[window_ addSubview:viewController_.view];
[window_ makeKeyAndVisible];
[CCTexture2D setDefaultAlphaPixelFormat:kCCTexture2DPixelFormat_RGBA8888];
[CCFileUtils setiPadSuffix:@"-ipad"];
[CCFileUtils setRetinaDisplaySuffix:@"-hd"];
[CCTexture2D PVRImagesHavePremultipliedAlpha:YES];
[director_ pushScene: [TitleScene scene]];
return YES;
You specify the viewController with this line:
Are you sure that your appDelegate is setting its viewController property? In other words, is
viewController
nil? I would check that.