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.

Comments on this entry are closed.

  • Matthew David

    Hello,

    I followed your steps and added the iAd framework but I get 11 errors when I go to build my app.

    Could you add a sample project file for us to download?

  • Pedro Paulo Jr

    @mattew I think you forgot to import iAd.h

  • Hello,
    I really like your tutorial, but I have one question. This is possible to add javascript control to iAd. For example if ppl pay for application I want to disable it with javascript or enable if needed?

  • Pedro Paulo

    @serge: it’s possible however I don’t know if Apple allow you to remove IAd using in app purchase

  • @Pedro Paulo: Do you have any ideas how can i implement it this with javascript on phonegap? I just want to to build application where by default iAds is disable and then if I need to display it I just run javascript function displayAd(). But I don’t have any ideas where I have to start. Im not good with xcode((

  • john l

    I too have 11 errors when I try to build my application. I have imported iAd.framework into the Frameworks folder and modified the code exactly as above.

    These are my build errors:

    In file included from /Users/MyMac/phonegap_new/Classes/phonegap_newAppDelegate.m:9:
    /Users/MyMac/phonegap_new/Classes/phonegap_newAppDelegate.h:25: error: expected ‘;’ before ‘{‘ token
    /Users/MyMac/phonegap_new/Classes/phonegap_newAppDelegate.m:14: warning: cannot find interface declaration for ‘phonegap_newAppDelegate’
    /Users/MyMac/phonegap_new/Classes/phonegap_newAppDelegate.m: In function ‘-[phonegap_newAppDelegate init]’:
    /Users/MyMac/phonegap_new/Classes/phonegap_newAppDelegate.m:19: error: no super class declared in @interface for ‘phonegap_newAppDelegate’
    /Users/MyMac/phonegap_new/Classes/phonegap_newAppDelegate.m:20: warning: control reaches end of non-void function
    /Users/MyMac/phonegap_new/Classes/phonegap_newAppDelegate.m: In function ‘-[phonegap_newAppDelegate applicationDidFinishLaunching:]’:
    /Users/MyMac/phonegap_new/Classes/phonegap_newAppDelegate.m:27: error: no super class declared in @interface for ‘phonegap_newAppDelegate’
    /Users/MyMac/phonegap_new/Classes/phonegap_newAppDelegate.m: In function ‘-[phonegap_newAppDelegate getCommandInstance:]’:
    /Users/MyMac/phonegap_new/Classes/phonegap_newAppDelegate.m:35: error: no super class declared in @interface for ‘phonegap_newAppDelegate’
    /Users/MyMac/phonegap_new/Classes/phonegap_newAppDelegate.m:36: warning: control reaches end of non-void function
    /Users/MyMac/phonegap_new/Classes/phonegap_newAppDelegate.m: In function ‘-[phonegap_newAppDelegate webViewDidFinishLoad:]’:
    /Users/MyMac/phonegap_new/Classes/phonegap_newAppDelegate.m:53: error: ‘bannerIsVisible’ undeclared (first use in this function)
    /Users/MyMac/phonegap_new/Classes/phonegap_newAppDelegate.m:53: error: (Each undeclared identifier is reported only once
    /Users/MyMac/phonegap_new/Classes/phonegap_newAppDelegate.m:53: error: for each function it appears in.)
    /Users/MyMac/phonegap_new/Classes/phonegap_newAppDelegate.m:56: warning: class ‘phonegap_newAppDelegate’ does not implement the ‘ADBannerViewDelegate’ protocol
    /Users/MyMac/phonegap_new/Classes/phonegap_newAppDelegate.m:59: error: no super class declared in @interface for ‘phonegap_newAppDelegate’
    /Users/MyMac/phonegap_new/Classes/phonegap_newAppDelegate.m:59: warning: ‘return’ with a value, in function returning void
    /Users/MyMac/phonegap_new/Classes/phonegap_newAppDelegate.m: In function ‘-[phonegap_newAppDelegate webViewDidStartLoad:]’:
    /Users/MyMac/phonegap_new/Classes/phonegap_newAppDelegate.m:66: error: no super class declared in @interface for ‘phonegap_newAppDelegate’
    /Users/MyMac/phonegap_new/Classes/phonegap_newAppDelegate.m:66: warning: ‘return’ with a value, in function returning void
    /Users/MyMac/phonegap_new/Classes/phonegap_newAppDelegate.m: In function ‘-[phonegap_newAppDelegate webView:didFailLoadWithError:]’:
    /Users/MyMac/phonegap_new/Classes/phonegap_newAppDelegate.m:75: error: no super class declared in @interface for ‘phonegap_newAppDelegate’
    /Users/MyMac/phonegap_new/Classes/phonegap_newAppDelegate.m:75: warning: ‘return’ with a value, in function returning void
    /Users/MyMac/phonegap_new/Classes/phonegap_newAppDelegate.m: In function ‘-[phonegap_newAppDelegate webView:shouldStartLoadWithRequest:navigationType:]’:
    /Users/MyMac/phonegap_new/Classes/phonegap_newAppDelegate.m:85: error: no super class declared in @interface for ‘phonegap_newAppDelegate’
    /Users/MyMac/phonegap_new/Classes/phonegap_newAppDelegate.m:86: warning: control reaches end of non-void function
    /Users/MyMac/phonegap_new/Classes/phonegap_newAppDelegate.m: In function ‘-[phonegap_newAppDelegate execute:]’:
    /Users/MyMac/phonegap_new/Classes/phonegap_newAppDelegate.m:91: error: no super class declared in @interface for ‘phonegap_newAppDelegate’
    /Users/MyMac/phonegap_new/Classes/phonegap_newAppDelegate.m:92: warning: control reaches end of non-void function
    /Users/MyMac/phonegap_new/Classes/phonegap_newAppDelegate.m: In function ‘-[phonegap_newAppDelegate dealloc]’:
    /Users/MyMac/phonegap_new/Classes/phonegap_newAppDelegate.m:96: error: no super class declared in @interface for ‘phonegap_newAppDelegate’

  • Pedro Paulo

    Have you imported the iAd header file?

  • Matt Wallace

    This is great, Thank You! One question: How can I get the Webview to start below the IAd? Right now the IAd sits on top of the webview and covers up the top of the webview.

  • Pedro Paulo

    You should use a spacer in your webdesign to give you 48px height free

  • Matt Wallace

    I did do that and my app was accepted…but I recently made an update and it was rejected because of the following quote from Apple: “we cannot post this version to the App Store because it displays an empty iAd banner when ad content is not available.” Basically when an add is not available for what ever reason, there is a blank space on top and Apple doesn’t allow this any more. If interested my current app is here: http://goo.gl/PnrV

  • Pedro Paulo

    @mattwallace – You should follow step 3 – it’s the methods that remove the banner when you don’t get an Ad.

    To try if it’s working correctly put your IPhone in Airplane Mode

  • Matt Wallace

    Thanks Pedro, I have included step 3 and it does remove the banner. The problem is that there is still 48px of white space (because of the spacer) when a banner is not loaded and Apple will not allow this blank space.

  • Pedro Paulo

    Can you send a screenshot to ppaulojr at gmail ?

  • Helo Pedro Paulo

    Thank you for code how to add iAd to phonegap. After I added your code, it compile and run fine on xcode with iphone simulator but I don’t see any banner ads to display on the top of the screen.

  • Claudio

    PQP Pedro Paulo,

    Você é o cara. Não tem como eu colocar uma dedicatória para vcs na minha app não?

    Abração e feliz natal.

  • Pedro Paulo

    @claudio – Fico feliz de saber que funcionou. Depois manda o link da App. Lembrando que iAd ainda só funciona em poucos países.

  • Rui

    Pedro,

    Estou para publicar um joguinho. Mas fiquei na dúvida: Vale mais apenas cobrar, por exemplo 0,99 pelo aplicativo ou colocar de graça e usar o iAds. vejo que o jogo Zeroes não é mais pago e tem o recurso iAds, valeu mais a pena?

    E como eu utilizo o recurso in App Purchase no phone gap?

    Obrigado

  • Pedro Paulo

    Dificil responder esta pengunta de forma taxativa.

    iAD só tem nos EUA por enquanto, portanto a menos que o jogo seja para os EUA você não ganhará dinheiro.

    Eu acho que precisa experimentar. O Zeroes não dá dinheiro quase.

  • Pedro Paulo

    InApp no Phonegap é complexo. Eu não sei se eles adicionaram isto recentemente. Eu penso que talvez valha a pena aprender Objective-C, depois do susto inicial fica tranquilo

  • Hi !

    Thanks for this nice Tutorial. I just have a problem.
    When I test my App on the simulator in Debug Mode, everything’s ok, I have the iAd test but when I try in Debug Mode on the Device, the iAd View appears then disappear and never appears again.
    Do you had this problem before ?

    Thanks !

  • Claudio

    Pedro Paulo,

    A app é a “De 0 a 10” bem simplesinha, quase um hello world.
    Uma coisa só que estou achando estranho é que nela não aparece a iAd nem que a vaca tussa.

  • Pedro Paulo

    @sabbish and @claudio: some countries don’t have iAds. The iAd is displaying in US, UK, Germany, France, Japan and Italy

  • Here is another guide to adding iAd to iPhone app. Hope it may be useful http://bees4honey.com/blog/tutorial/how-to-add-iad-banner-in-iphoneipad-app/

  • Thanks Pedro Paulo, but I’m from France.

  • Pedro Paulo

    Maybe Apple are with a low fill rate for France

  • Maybe, I’ll try again and see. I’ll let know you if I find a clue.

  • dolgia

    Hi, i followed this great tutorial, all went well until Xcode 4 came out… Now, when testing with the 4.3 simulator, i am getting the following error:

    ADBannerView: WARNING A banner view (0x5d32ca0) has an ad but may be obscured. This message is only printed once per banner view

    and the banner doesn’t show. With 4.2 simulator all goes fine.

    Any idea of what may be happening? Thanks a lot, cheers