Adding iAd in Phonegap Apps

How to add iAd to PhoneGap app: (Este post está em inglês para ajudar os desenvolvedores PhoneGap em todo mundo)

Step 1)

Modify the following function in yourAppDelegate.m:

From:

- (void)webViewDidFinishLoad:(UIWebView *)theWebView
{
	return [ super webViewDidFinishLoad:theWebView ];
}

To:

- (void)webViewDidFinishLoad:(UIWebView *)theWebView
{
	bannerIsVisible = YES;
	ADBannerView *adView = [[ADBannerView alloc] initWithFrame:CGRectZero];
	adView.frame = CGRectMake(0, 410, 320, 50); // if you want the banner to be on top of the screen remove this line
	adView.delegate = self;
	adView.currentContentSizeIdentifier = ADBannerContentSizeIdentifier320x50;
	[theWebView addSubview:adView];
	return [ super webViewDidFinishLoad:theWebView ];
}

Step 2:

Modify the yourAppDelegate.h:

From:

@interface Desafio3x3AppDelegate : PhoneGapDelegate {
}

To:

#import "iAd/iAd.h"
 
@interface Desafio3x3AppDelegate : PhoneGapDelegate <ADBannerViewDelegate> {
	BOOL bannerIsVisible;
}

Step 3:

Add the following methods before the @end in yourAppDelegate.m

- (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error
{
	if (bannerIsVisible)
    {
        [UIView beginAnimations:@"animateAdBannerOff" context:NULL];
	// assumes the banner view is at the bottom of the screen.
        banner.frame = CGRectOffset(banner.frame, 0, 50); // if the banner is on top of the screen use -50
        [UIView commitAnimations];
        bannerIsVisible = NO;
    }
}
 
- (void)bannerViewDidLoadAd:(ADBannerView *)banner
{
    if (!bannerIsVisible)
    {
        [UIView beginAnimations:@"animateAdBannerOn" context:NULL];
	// assumes the banner view is offset -50 pixels so that it is not visible.
        banner.frame = CGRectOffset(banner.frame, 0, -50); // if the banner is on top of the screen use 50
        [UIView commitAnimations];
        bannerIsVisible = YES;
    }
}
 
- (BOOL)bannerViewActionShouldBegin:(ADBannerView *)banner willLeaveApplication:(BOOL)willLeave
{
    NSLog(@"Banner view is beginning an ad action");
    BOOL shouldExecuteAction = YES; // your application implements this method if you want it not fixed
    if (!willLeave && shouldExecuteAction)
    {
        // insert code here to suspend any services that might conflict with the advertisement
    }
    return shouldExecuteAction;
}

Step 4:

Add the iAd Framework to the list of Frameworks of your project.

Ready!

Don’t forget to enable iAds when you upload your app.