New smart game. iQBloxing
 
 
 

How to Add iAd Banner in iPhone App

In April 2010 during iOS 4 presentation Steve Jobs announced new advertisement platform – iAd. This platform is intended to allow developers of free apps to earn on their apps showing advertisement. The main idea of iAd is to connect interactivity (using smartphone or tablet) and emotionality of advertisement (JavaScript, HTML5, CSS3, multi-touch). It allows to create absolutely awesome advertisement which looks like apps with video and audio. In addition, iAd ads is opened inside the app and user do not exit his app and can get back anytime he wants.

Below is the guide on how to add iAd banner into iPhone app. Guide is based on iAd Programming Guide and WWDC 2010 video.

Add iAd Banner to iPhone App

The first thing you need to do is to add iAd.framework in your Xcode project which is contained in iOS 4 SDK. Also do not forget to add #import <iAd/iAD.h>. Developers can choose between two banners: 320×50 px for portrait and 480×32 px for landscape. The base of banner is ADBannerView, which is subclass of UIView. So the only thing you have to do is to add this view in your control elements hierarchy (you can do this programmatically or using Interface Builder).

iOS 4 SDK Interface Builder

Apple recommends to put banner at the bottom or at the top of the window and do not place it on any moving elements like ScrollView or TableView as far as that will decrease shows of banner (and your revenue respectively) and will make it more difficult for user to tap on the ads.

Let’s create a new project in Xcode using View-based Application template and add a banner into it. Name of the app – iAdEx. We are going to edit iAdExViewController.h

#import <UIKit/UIKit.h>;
#import <iAd/iAd.h>;

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

and modify viewDidLoad method in iAdExViewController.m

- (void)viewDidLoad {
      adView = [[ADBannerView alloc] initWithFrame:CGRectZero];
      adView.frame = CGRectOffset(adView.frame, 0, -50);
      adView.requiredContentSizeIdentifiers = [NSSet setWithObject:ADBannerContentSizeIdentifier320x50];
      adView.currentContentSizeIdentifier = ADBannerContentSizeIdentifier320x50;
      [self.view addSubview:adView];
      adView.delegate=self;
      self.bannerIsVisible=NO;
      [super viewDidLoad];
}

Let’s talk about requiredContentSizeIdentifiers and currentContentSizeIdentifier properties. In the first one you define all types of banners you are going to use. And the second property defines which type of banner you are going to use at the present moment.

Connection issues

Banners are downloaded from the network. What if we have no network connection right now? Or Apple has any issues with ads server? Our ADBannerView will be empty is these cases. It doesn’t look very nice and wastes space on the screen. Apple recommends to do it in this way: when there is no banner for any reason remove it from the screen; when banner is received – show it again.

We have the ADBannerViewDelegate in our class and it can receive messages from banner – bannerViewDidLoadAd (when banner is loaded successfully) and didFailToReceiveAdWithError (when any problems occured). Let’s implement these messages:

- (void)bannerViewDidLoadAd:(ADBannerView *)banner
{
 if (!self.bannerIsVisible)
 {
  [UIView beginAnimations:@"animateAdBannerOn" context:NULL];
// banner is invisible now and moved out of the screen on 50 px
  banner.frame = CGRectOffset(banner.frame, 0, 50);
  [UIView commitAnimations];
  self.bannerIsVisible = YES;
 }
}

- (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error
{
if (self.bannerIsVisible)
 {
  [UIView beginAnimations:@"animateAdBannerOff" context:NULL];
// banner is visible and we move it out of the screen, due to connection issue
  banner.frame = CGRectOffset(banner.frame, 0, -50);
  [UIView commitAnimations];
  self.bannerIsVisible = NO;
 }
}

It’s time to launch the app and see what we have now:

iAd banner on iPhone

Tap the banner

iAd Detailed View on iPhone Simulator

And that’s it – we’ve done it in accordance with Apple’s recommendations. When we launch the app we see the banner. If we tap it, then full view of advertisement is shown. But we still have one issue…

Stop & Resume your app

In real app we should stop any application’s activity such as video, audio playback or pause game. In order to solve this task we will create two methods bannerViewActionShouldBegin (when full screen ad is shown) and bannerViewActionDidFinish (when we close ads).

- (BOOL)bannerViewActionShouldBegin:(ADBannerView *)banner willLeaveApplication:(BOOL)willLeave
{
 NSLog(@"Banner view is beginning an ad action");
 BOOL shouldExecuteAction = YES;
 if (!willLeave && shouldExecuteAction)
    {
    // stop all interactive processes in the app
    // ;
    // ;
    }
 return shouldExecuteAction;
}

- (void)bannerViewActionDidFinish:(ADBannerView *)banner
{
   // resume everything you've stopped
   // ;
   // ;
}

Change Orientation of iAd

What else should we do? We need to make banner change it’s orientation in accordance with iPhone position. First of all we need to change the line where we define types of banners which we will use:

- (void)viewDidLoad {
...
adView.requiredContentSizeIdentifiers = [NSSet setWithObjects:ADBannerContentSizeIdentifier320x50,ADBannerContentSizeIdentifier480x32,nil];
...
}

and here are methods for changing orientation:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
 // Return YES for supported orientations
 return (interfaceOrientation == UIInterfaceOrientationPortrait|UIInterfaceOrientationPortrait);
}

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
 if (UIInterfaceOrientationIsLandscape(toInterfaceOrientation))
  adView.currentContentSizeIdentifier = ADBannerContentSizeIdentifier480x32;
 else
  adView.currentContentSizeIdentifier = ADBannerContentSizeIdentifier320x50;
}

And here is how it look in a landscape

iAd banner in landscape mode

When device changes orientation we change currentContentSizeIdentifier property of ADBannerView. Now we can see banners in landscape mode too. There is still 50 px offset, but it’s easy to fix.

Apple developers also recommend to write object.delegate=nil; line before deallocating ADBannerView object. In our example it will look like:

- (void)dealloc {
 adView.delegate=nil;
 [adView release];
 [super dealloc];
}

Now we have finished with programming part. Good luck with Apple iAd!

Post to Twitter Post to Delicious Post to Digg Post to Facebook Post to Reddit Post to StumbleUpon

Related Posts

Why Apple iAd Is Really Great?, Apple iAd in iPhone and iPad apps, Top 10 sites to advertise your iPhone app
 

Comments - 145

  1. I discovered your weblog web page on google and examine a number of of one’s early posts. Proceed to maintain up the very very good operate. I only additional up your RSS feed to my MSN Facts Reader. Looking for forward to studying additional from you in a even though!? I’m usually to running a weblog and i really respect your content. The article has really peaks my interest. I am going to bookmark your web site and hold checking for brand new facts.

  2. I’ve been exploring for a bit for any high-quality articles or weblog posts within this sort of house . Exploring in Yahoo I at last stumbled upon this internet site. Reading this information So i¡¦m satisfied to show that I’ve an incredibly great uncanny feeling I came upon specifically what I necessary. I most for sure will make particular to perform not forget this internet site and give it a look a continuing.

  3. Prince of Persia will be the finest, i really like the lead actor as well as the princess, the princess is very rather..

  4. Andre Caris says:

    L.E: the shipping confirmatio electronic mail was in my junk folder, that’s why I in no way gotten it. It explained almost everything I found it necessary to know, I recently in no way discovered it.

  5. Good, probably not for all people this kind of topic definitely will be something ordinary, but my mate introduced me so named “hobby” , which usually I would most likely never think of existing. Dating escorts definitely may be a hobby. I search in to this for a few weeks now and very soon planning to make my first hobby experience.

  6. Make an effort to get some equilibrium in your lifetime. You can have a partner whom enjoys anyone, and when you get uninterested, possibly view a handful of providers every now and then.|You are a whoremonger. Bought it. If any person will not like it, shag Them. I’m not really proud of banging hookers but I’m sure since stuff not ashamed of the idea. I’m just ashamed if I create a dumb-ass oversight and make sure to never allow it to be twice. |{

  7. Good, probably not for all people this specific topic will probably be something conventional, but my good friend introduced me so called “hobby” , which I would by no means suppose of existing. Meeting escorts really could be a hobby. I dig in to this for a few weeks now and very soon arranging to make my first hobby experience.

  8. All right, probably not for most people this particular topic will be something ordinary, but my buddy showed to me so called “hobby” , which I would most likely hardly ever think of existing. Meeting escorts in reality could be a hobby. I dig in to this for a short time now and very soon arranging to make my first hobby experience.

  9. He as well described to be a participant of some private club with independent escorts across the united states which is very good to work with. Their website Miami Adult Entertainment . Furthermore he discussed concerning some VIP programm, however , I didn’t fully understood him. What exactly do you folks feel in relation to such hobby?

  10. So named hobbyist that dates an escort eventually posts escort reviews on user discussion forums just like this kind of and review boards. I understand it’s bad, but is in it all negative feels fantastic?:)

  11. He also described to be a member of some private club with independent escorts nationally that is very good to work with. Their blog Miami City Girls . Also he talked concerning some VIP programm, but I didn’t understood him. Exactly what do you guys think about such hobby?

  12. He also outlined to be a member of some private club with independent escorts countrywide that is good to use. Their internet site Miami Escort Service . Also he spoke in relation to some VIP programm, but I didn’t understood him. Exactly what do you folks think with regards to this kind of hobby?

  13. Okay, probably not for most people this may be something typical, but my friend introduced to me what are known as “hobby” , which usually I would likely by no means believe of existing. Interacting with escorts truly could be a hobby. I search in to this for a short while now and now arranging to make my first hobby experience.

  14. He also pointed out to be a member of some private club with independent escorts countrywide that is good to work with. Their blog Miami escorts . Additionally he talked in relation to some VIP programm, but I didn’t fully understood him. What exactly do you guys think with regards to these kinds of hobby?

  15. Hacks says:

    This is my first time pay a visit at here and i am genuinely impressed
    to read all at single place.

  16. He in addition pointed out to be a fellow member of some private club with independent escorts nationwide which is excellent to work with. Their web site Miami Escort Service . Additionally he discussed in relation to some VIP programm, however I didn’t understood him. What exactly do you fellas think about such hobby?

  17. All right, possibly not for most people this particular topic will certainly be something ordinary, but my pal introduced to me so called “hobby” , which usually I would probably by no means think of existing. Dating escorts truly could be a hobby. I search in to this for a two weeks now and soon arranging to make my first hobby experience.

  18. Fine, probably not for everyone this kind of topic will be something general, but my close friend showed me what are known as “hobby” , which usually I would probably never believe of existing. Interacting with escorts actually could be a hobby. I dig in to this for a couple of minutes now and now planning to make my first hobby experience.

  19. So called hobbyist that dates an escort afterwards posts escort reviews on discussion forums like this kind of and review boards. I realize it’s negative, but is in it all negative feels very good?:)

  20. He in addition pointed out to be a member of some private club with independent escorts all over the country which is good to work with. Their web site Miami Escort Agencies . Additionally he discussed concerning some VIP programm, however , I didn’t understood him. What do you folks feel with regards to such hobby?

  21. Okay, maybe not for everybody this particular topic will probably be something conventional, but my buddy introduced to me what is known as “hobby” , which I would likely hardly ever imagine of existing. Dating escorts actually may be a hobby. I dig in to this for a short while now and now considering to make my first hobby experience.

  22. He as well mentioned to be a participant of some private club with independent escorts across america which is very good to use. Their online site Miami Escort Agencies . Additionally he talked regarding some VIP programm, however I didn’t understood him. What exactly do you fellas feel regarding this kind of hobby?

  23. Alright, perhaps not for everybody this particular topic is going to be something typical, but my close friend introduced to me what are known as “hobby” , which usually I would never suppose of existing. Interacting with escorts truly might be a hobby. I dig in to this for a short time now and soon planning to make my first hobby experience.

  24. Discover A great deal more Here

  25. Woah this weblog is excellent i really like reading your articles. Stay up the great work! You understand, lots of persons are searching round for this information, you could aid them greatly.

  26. Find free anti aging skin care advice given to you
    by Industry Leading Dermatologists at http://www.skinadvice.org/

  27. I’ve read several just right stuff here. Certainly price bookmarking for revisiting. I surprise how a lot effort you set to make the sort of great informative site.

  28. Hi, I just wanted to tell you, you’re dead wrong. Your article doesn’t make any sense.

  29. It certainly is nearly impossible to come across well-aware individual on this theme, yet somehow you look like you comprehend whatever you’re talking about! Thanks A Lot

  30. Seriously enjoyable highlights you have said, thank you so much for publishing.

  31. Now, you could learn, we’re major supporters of Dr. Dre beats studio headphones. We also ColorWare followers, custom color glimpse just about all the popular products. Well, now’s the DRE ColorWare custom hand-painted studio headphones supply the defeat, they look hot. We’d showing you some custom ColorWare defeat headphones, including Ron Herman White defeat, beat David red, green and Charlie Hamilton beat. ColorWare these headphones some wonderful work, while you could from the above three features. wholesale headphones visit TradeTang.com.

  32. Kym Conkin says:

    I discovered more a new challenge on this fat reduction issue. 1 issue is a good nutrition is highly vital whenever dieting. A massive reduction in fast foods, sugary meals, fried foods, sugary foods, red meat, and white-colored flour products may be necessary. Having wastes unwanted organisms, and harmful toxins may prevent targets for fat loss. While certain drugs in the short term solve the issue, the unpleasant side effects aren’t worth it, and in addition they never present more than a short-lived solution. This can be a known idea that 95 of diet plans fail. Many thanks for sharing your notions on this website.

  33. Ramon Combs says:

    Thanks for the post. My spouse and i have often seen that most people are wanting to lose weight as they wish to appear slim in addition to looking attractive. Nonetheless, they do not constantly realize that there are other benefits so that you can losing weight additionally. Doctors say that fat people have problems with a variety of illnesses that can be perfectely attributed to the excess weight. The good news is that people who are overweight and also suffering from various diseases are able to reduce the severity of their illnesses by means of losing weight. You possibly can see a steady but noticeable improvement in health whenever even a minor amount of losing weight is achieved.

  34. trucking says:

    Pardon me, has anyone heard about that electrical contractor on Chambers by the name of New York Electrician, 94 Chambers St, New York, NY 10007 (646) 415-6723

  35. a says:

    Thank you for any other excellent post. The place else may just anybody get that kind of information in such an ideal means of writing?
    I’ve a presentation next week, and I am on the search for such information.

  36. Thank you for the sensible critique. Me & my neighbor were just preparing to do a little research on this. We got a grab a book from our local library but I think I learned more clear from this post. I’m very glad to see such magnificent information being shared freely out there.

  37. Please let me know if you’re looking for a article author for your weblog.

    You have some really good articles and I feel I would be a good asset.
    If you ever want to take some of the load off, I’d love to write some material for your blog in exchange for a link
    back to mine. Please shoot me an email if interested.

    Thanks!

  38. Well the $999 Apple Macbook Air 11.6 comes with 64GB fast SSD.

  39. Rae Roybal says:

    Have you decrypted your save with Bruteforce?

  40. My brother recommended I would possibly like this blog.

    He was once totally right. This submit truly made my day.

    You can not imagine just how so much time I had spent for this info!

    Thank you!

  41. If you desire to increase your knowledge simply keep visiting this site and be updated with the newest gossip
    posted here.

  42. La salsa dulce, acidez de las manzanas mezcladas con el ahumado del tocino mezclan juntos para hacer de este lomo de cerdo con manzanas y tocino en un verdadero

  43. Link útil says:

    Qué suerte la mía. Muy buena info y encontré tu sitio por casualidad. Lo tengo guardado como favorito! Gracias… Besos

 
Leave a comment