Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Danneman101

macrumors 6502
Original poster
Aug 14, 2008
361
1
Ive read almost everything Ive found on the subject on this forum and elsewhere, but I still cant get "didFailToReceiveAdWithError" to execute, and thus an empty iAd-banner is still displayed even when the wifi is turned off.

To keep the sample simple Ive included only what is relevant to the iAd-code.

Ive imported the iAd-framwork to my project, and the code runs the first piece of code (inside "viewWillAppear") successfully. But never will the add disappear (when turning off the wifi), and never will the log even show didFailToReceiveAdWithError.

MyViewController.h
Code:
#import <iAd/ADBannerView.h>
#import <iAd/iAd.h>

@interface MyViewController : UIViewController <ADBannerViewDelegate>
{
	ADBannerView *adView;
	BOOL bannerIsVisible;
}
@property (nonatomic, assign) BOOL bannerIsVisible;

MyViewController.m
Code:
#import "MyViewController.h"
@implementation MyViewController

@synthesize bannerIsVisible;

- (void)viewWillAppear:(BOOL)animated
{
	if (!bannerIsVisible){
		
		NSLog(@"Show iAd");	
		
		adView = [[ADBannerView alloc] initWithFrame:CGRectZero];
		adView.frame = CGRectOffset(adView.frame, 0, 367);
		adView.currentContentSizeIdentifier = ADBannerContentSizeIdentifier320x50;
		[self.view addSubview:adView];
		adView.delegate=self;
		bannerIsVisible = YES;
	}
}

- (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error
{
	NSLog(@"didFailToReceiveAdWithError");		

	if (bannerIsVisible){
		[UIView beginAnimations:@"animateAdBannerOff" context:NULL];
		adView.frame = CGRectOffset(adView.frame, 0, +50);
		[UIView commitAnimations];
		bannerIsVisible = NO;
	}
}

Am I wrong in thinking that the error should execute when the wifi is turned off? Otherwise, what could be the problem?
 

jared_kipe

macrumors 68030
Dec 8, 2003
2,967
1
Seattle
You should be starting with it hidden and move it into place when it loads an ad. That method is only called when its already displayed and receives an error.
 

LastLine

macrumors 65816
Aug 24, 2005
1,313
21
Assuming you're a registered developer go and watch the iAd WWDC video - the 'ideal implementation' is an exact answer to this problem :) Worked for me anyways.
 

Danneman101

macrumors 6502
Original poster
Aug 14, 2008
361
1
LastLine: I cant find that video. Been looking in http://developer.apple.com/iphone/appstore/iad/ but nothing.

jared_kipe: I tried that approach and it did work better. Thanks :)


Code:
// Synt:		iAd (v.4.0)
@synthesize bannerIsVisible;

- (void)viewWillAppear:(BOOL)animated
{
	// ----------
	// iAD
	// ----------
	// info:	Initierar iAd (v.4.0)
	if (!bannerIsVisible)
	{		
		NSLog(@"Show iAd");	
		
		adView = [[ADBannerView alloc] initWithFrame:CGRectZero];
		adView.frame = CGRectOffset(adView.frame, 0, 417);	// Dont show ad yet	
		adView.requiredContentSizeIdentifiers = [NSSet setWithObject:ADBannerContentSizeIdentifier320x50];
		adView.currentContentSizeIdentifier = ADBannerContentSizeIdentifier320x50;
		[self.view addSubview:adView];
		adView.delegate=self;
	}
}
// ----------
// iAD
// ----------
// info:	Hides iAd if error (v.4.0)
- (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error
{
	NSLog(@"Bannerview error");		
	
	if (bannerIsVisible){
		[UIView beginAnimations:@"animateAdBannerOff" context:NULL];
		adView.frame = CGRectOffset(adView.frame, 0, +50);	// Move below screen
		[UIView commitAnimations];
		bannerIsVisible = NO;
	}
}
// ----------
// iAD
// ----------
// info:	Shows iAd (v.4.0)
- (void)bannerViewDidLoadAd:(ADBannerView *)banner
{
	NSLog(@"Bannerview did load");		
	
	if (!bannerIsVisible)	{
		[UIView beginAnimations:@"animateAdBannerOn" context:NULL];
		adView.frame = CGRectOffset(adView.frame, 0, -50);	// Show at bottom of screen
		[UIView commitAnimations];
		bannerIsVisible = YES;
	}	
}
// ----------
// iAD
// ----------
// info:	Resumes WebView and hides ProgressView when iAd is shut down (v.4.0)
- (void)bannerViewActionDidFinish:(ADBannerView *)banner
{
	// -------------------
	// Show:	WebView
	// -------------------
	[self.webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"document.body.style.visibility='visible';"]];		

	// ---------------------
	// Stop:	ProgressView
	// ---------------------
	[activityView stopAnimating];	
	[activityView hidesWhenStopped];
	[self.view sendSubviewToBack:progressAlert];	
}
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.