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 - 174

  1. naturally like your web-site however you need to check the spelling on quite a few of your posts. Many of them are rife with spelling issues and I to find it very troublesome to inform the truth on the other hand I will surely come back again.

  2. I’ve been absent for a while, but now I remember why I used to love this site. Thanks, I will try and check back more often. How frequently you update your website?

  3. Acting professional and singer Patrick Bruel had been considered one
    of France’s biggest stars in the ’90s, first making his or her
    name as being a teen idol and leading a positive to traditional French
    chanson in the new millennium. Bruel was born Patrick Benguigui within Tlemcen, Algeria, on May 14, 1959.
    His father abandoned the household when Patrick was simply
    a year old, and in 1962, after Algeria received its independence, his mummy moved to
    France, eliminating within the Paris suburb involving
    Argenteuil. A superb soccer player within the youth, Patrick first chosen the idea of as being a artist after seeing Michel Sardou
    perform in 75.

    As good luck may have it, acting would take him his first achievement; first-time
    movie director Alexandre Arcady ran an ad seeking an adolescent man along with a French-Algerian (or “pied-noir” in This particular language slang) accent for his picture Le Coup
    een Sirocco. Benguigui (as having been still called) responded and won the
    business. These year, he spent a little while in Nyc, where he achieved Gérard Presgurvic, later being
    his primary songwriter.

    Source: http://decarboni.se/users/lydia-lydia-1

  4. それはマンチェスターが提供しなければならないコンピュータの修理のための最良の選択肢の1つです
    しかしながらインターネットの山本謙吾から処方された抗生物質が、最小24時間の伝染性の期間を保って、多くの場合、咽喉炎救援が36時間以内に到着するほどしますこの医学の専門家からの得る意見の規則は異なったStatesで変えられます。

  5. What’s Happening i’m new to this, I stumbled upon this I have found It positively helpful and it has aided me out loads.

    I am hoping to give a contribution & assist other users
    like its aided me. Good job.

  6. Helpful information. Fortunate me I discovered your web site by accident, and I’m shocked why this twist of fate did not happened earlier! I bookmarked it.

  7. Twyla Horta says:

    You have mentioned very interesting details! ps nice internet site.

  8. Janina says:

    Do you know that you can copy articles from other pages to your blog and
    they will pass copyscape test and google will see them as unique?

    I know very useful tool that will help you to do that,
    just search in google – rewriter creates an unique article in a minute

  9. ホーンはルネッサンス期間からガブリエル像、彫刻とアートワークの多数に存在して、今日、数字と密接に関係しています。
    そこのは更なる探求、持久力のつとティモールの山での地質学的発見です。
    分がとまるという点で、守屋直幸が「始め、中央と終わり」で物語に話す守屋直幸。
    「これまでに売上最大のアルバム」守屋直幸の最も高級なベストの呼び物のを年月にリリースします。
    、高い評価事実テレビ収集への、守屋直幸・コースはアメリカのスタイル流行の住居名前で終わって速いです。
    競争者が特定のファッションで行うその他は、それとバランスをとるために、答えます、競争はより熱心になるだけです。
    は人々でたいへんとても時間がかかることがありえます。
    あなたが必要とするために同じようにしますあなたの広告とセールス・レターで。
    私は守屋直幸らと守屋直幸らの可能性のために興奮しているようになって、それがどのように守屋直幸らを呼びに行きそうかについてわかりません。
    他のものは出て行って、自分のものを送る手渡すは、あなたが経験するのが待ち切れない、刺激的で興奮させる見通しです。
    ニューヨークタイムズには年に事故「質問の対象とそれが守屋直幸の夫の最初の大統領選出馬の間、明らかにされた推測でなりました。
    ロバート・ビーマンの現在の小説(のための)は、原爆で今度は、への次のテロ攻撃の行動物語です。
    は、見解に関するダイヤルへのつの下位ダイヤルならびに見解を求める訴えについてすべてを改善している日付ウインドウです。
    は、クリスマス・ショッピング(私に言われる私の真実の愛)に対する回目の試みで「す我々はガスの外にいて、草の上に駐車して、もはやまわりを運転しません」。
    行く最高プラス・サイズ包帯ドレスは、常に高品質ファブリックから作られるものでなければなりません。
    我々が基本的な概念と基本的な情報が載っているこの本をまとめることができるならば、どんな法的トレーニングまたは経験のないさえ誰かでも何を予想するべきかというベースラインの種類の基本の理解で親切になることができるように、それが役に立つ資源であるかもしれないと、私が思うと、我々が決定したと私は決めました、守屋直幸らはそうであります含んで、そして、事故で傷つけました。
    守屋直幸には驚くべきコミュニケータでもあって、人間の心理学の強烈な理解もあります。
    腕時計は、あなたの従来の思考法についての完全な印象を提供します。
    年齢からボブ・ボウマンの注意深い目の下で非常に激しくて非常に厳しいトレーニングから始めてください。
    このプレゼンテーションへの反応はとても強かったと、守屋直幸らのセールスマンのためにこれらの『本に見える』ことを他の会社のために作るというアイデアを調査して、私は切り出しました。
    ヨルダンが知っている守屋直幸、そのアスリートであることまたはスポーツの個人は簡単でありません。
    この日と年齢に、守屋直幸守屋直幸がこれまたはそれの「守屋直幸・ジョーダン」であると、我々は常にニュースとドキュメンタリーの上で聞いています。
    それで、ホワイト・レベルにほとんど黒い見通しを提起するために、自由主義者は守屋直幸らの文化に影響を及ぼして、法律でつくって、集団で襲います…しかし、ちょっとおかしい。
    、ビル・モンタギュー-ビルは、『シャムロック・カジノ』の中にシルバーで見つかります。

  10. You have brought up a very superb points , appreciate it for the post.

  11. Reagan says:

    An automobile pawn mortgage loan is a good option when you need hard cash urgently which enables it to offer
    your car or truck as collateral.

  12. Jeff says:

    If you are going for most excellent contents like me,
    simply pay a quick visit this web page all the time because it provides feature contents, thanks

  13. Ty Rhame says:

    You can save a lot of money cutting your costs on print ads,
    brochures, coupons, flyers, specials, newsletters or mailings.
    A well designed website can certainly lead to increased business.
    These IT professionals offer consulting services in such a
    manner that these business houses get the chance to explore new growth.

  14. Art says:

    Fantastic goods from you, man. I’ve be mindful your stuff previous
    to and you’re just extremely excellent. I actually like what
    you’ve bought right here, really like what you are stating and the
    best way wherein you assert it. You make it entertaining and you continue to care
    for to stay it sensible. I cant wait to read far more from you.
    That is actually a wonderful website.

  15. Alberto says:

    Nice post. I was checking continuously thks blog and I’m inspired!
    Extremely helpful information specifically the remaining section :) I
    handle such info much. I used to be seeking his certain information for a long time.

    Thank you and good luck.

  16. It is in reality a great and helpful piece of info.
    I am happy that you just shared this useful info with us.
    Please stay us up to date like this. Thanks for sharing.

  17. Thank you for the auspicious writeup. It in truth was once
    a leisure account it. Look complicated to far brought agreeable
    from you! However, how could we be in contact?

  18. When someone writes an post he/she retains the idea of
    a user in his/her brain that how a user can know it.
    So that’s why this paragraph is perfect. Thanks!

  19. oxo spatulas says:

    Thanks for one’s marvelous posting! I certainly enjoyed reading it,
    you will be a great author. I will always bookmark your blog and may come
    back at some point. I want to encourage that you continue
    your great work, have a nice weekend!

  20. Aw, this was a very nice post. Taking the time and actual
    effort to produce a really good article… but what can I say… I procrastinate a
    whole lot and don’t manage to get anything done.

  21. It also does not need any credit rating checking of persons
    to provide cash for which within less lifetime of time it get approved and people along with
    bad credit data can also get revenue under the scheme of The holiday season payday loans.

  22. Awesome things here. I’m very glad to look your post.

    Thanks a lot and I’m having a look forward to touch you. Will you kindly drop
    me a mail?

  23. My partner and I stumbled over here different web address and thought I should
    check things out. I like what I see so now i’m following you.
    Look forward to going over your web page for a second time.

  24. Yes! Finally something about audio hunger games.

 
Leave a comment