Pages

Adding iAds to Cocos2d-x on iOS

****Updated for XCode 5 ****

Looking at the forums it seems a lot of people are having trouble getting iADs working on Cocos2d-x on iOS.  It's actually pretty straight forward, but I guess a lot of people using Cocos2d-x aren't familiar with Objective C.

Here's a quick explaination

First, you need to add the iADFramework your project (click your target, go to Build Phases, find Link Binary with Libraries and add it).

Now you need to add the code.    In project explore, expand the folder with the same name as your application, and you'll see a folder (or group) call iOS.  In there is the AppController clash.  This is the place you need to put the code.

First, in AppController.h

Add an import for iAds.



#import <iAd/iAd.h>


Now you need to make the class a delegate for ADBannerViewDelegate as it needs to response to callbacks.

So my interface declaration looks like this now:


@interface AppController : NSObject <UIAccelerometerDelegate, UIAlertViewDelegate, UITextFieldDelegate,UIApplicationDelegate, ADBannerViewDelegate>{


OK - see it's got ADBannerViewDelegate at the end?

Now you need add you view to the header, and also a variable to keep track of whether your add is visible (as Apple with reject it if you don't hide non-filled iAD views)

So my header now looks like this:


#import <iAd/iAd.h>

@class RootViewController;

@interface AppController : NSObject <UIAccelerometerDelegate, UIAlertViewDelegate, UITextFieldDelegate,UIApplicationDelegate, ADBannerViewDelegateGADBannerViewDelegate >{
    UIWindow *window;
    RootViewController    *viewController;
    ADBannerView *adView; 
    bool bannerIsVisible;
}

@property (nonatomic, retain) UIWindow *window;
@property (nonatomic, retain) RootViewController *viewController;
@property (nonatomic, retain) ADBannerView *adView;
@property bool bannerIsVisible;
@end


OK, that's it for the header. Now go to the AppController.mm

Firstly, at the top, just under implementation, do your synthesising:


@synthesize adView;
@synthesize bannerIsVisible;


Now you need to create an ad banner, and add it to your root view controller.  Find the lines:


 // Set RootViewController to window
    if ( [[UIDevice currentDevice].systemVersion floatValue] < 6.0)
    {
        // warning: addSubView doesn't work on iOS6
        [window addSubview: viewController.view];
    }
    else
    {
        // use this method on ios6
        [window setRootViewController:viewController];
    }


That creates the viewController that you need to add your ad view to.

I want to add my ad at the bottom, so this is the code I use:


adView = [[ADBannerView alloc]initWithFrame:CGRectZero];
adView.delegate = self
[viewController.view addSubview:adView];

 self.bannerIsVisible = NO;
    
   CGRect adFrame = adView.frame;
    adFrame.origin.y = viewController.view.frame.size.width+adView.frame.size.height;
    adFrame.origin.x = viewController.view.frame.size.height/2-adView.frame.size.width/2;
    adView.frame = adFrame;
   


The first three lines create the ad, and the last three move it off the bottom of the screen (as it's not got an ad yet, so it needs to be hidden.  I'll leave you to work out how to change the code if you want it off the top.

IAds will call either 'BannerViewDidLoadAd' or 'didFailToRecieveAdWithError'.  You need to respond to these, either by move the view onto the screen (if it loaded an ad) or moving it off it didn't (I know it's off the screen on the first call, but this may be a later ad).  Add these at the bottom (just before @end)



- (void)bannerViewDidLoadAd:(ADBannerView *)banner
{
   

    
     if (!self.bannerIsVisible)
    {
        [UIView beginAnimations:@"animateAdBannerOn" context:NULL];
        // Assumes the banner view is just off the bottom of the screen.
        CGRect adFrame = adView.frame;
        adFrame.origin.y = viewController.view.frame.size.width-adView.frame.size.height;
        adFrame.origin.x = viewController.view.frame.size.height/2-adView.frame.size.width/2;
        adView.frame = adFrame;
        [UIView commitAnimations];
        self.bannerIsVisible = YES;
    }
}

- (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error
{
    
    
    NSLog(error.description);
   if (self.bannerIsVisible)
    {
        [UIView beginAnimations:@"animateAdBannerOff" context:NULL];
        // Assumes the banner view is placed at the bottom of the screen.
        banner.frame = CGRectOffset(banner.frame, 0, banner.frame.size.height);
        [UIView commitAnimations];
        self.bannerIsVisible = NO;
  }
}


That's all you have to do (other than enable iADs in iTunesConnect for your ad - that's pretty easy).

Remember to make sure your test device its connected to the net when you test otherwise nothing will happen!

Also, iADs test server often responds with errors - don't worry - this is normal.

9 comments:

  1. I am getting hung up creating the ad banner and adding it to the Root View Controller. Where do I ad this code and the code snippet after? and how do I add the ads to my different scenes?

    ReplyDelete
    Replies
    1. đồng tâm
      game mu
      cho thuê nhà trọ
      cho thuê phòng trọ
      nhac san cuc manh
      số điện thoại tư vấn pháp luật miễn phí
      văn phòng luật
      tổng đài tư vấn pháp luật
      dịch vụ thành lập công ty
      http://we-cooking.com/
      chém gió
      trung tâm tiếng anh

      Thấy người này xuất hiện, mọi người đang ngồi đều đứng lên, ngay cả Ny Na cũng không ngoại lệ

      Bởi vì người đến không phải ai khác, chính là Mễ Lan đế quốc hoàng đế, Tây Nhĩ Duy Áo Bối Lỗ Tư Khoa Ni. Đi theo bên người, tự nhiên chính là đế quốc nguyên soái, cũng là Mễ Lan đế quốc đệ nhất cao thủ Tây Đa Phu

      - Tham kiến bệ hạ.

      Mọi người quỳ xuống hành lễ, đương nhiên, Diệp Âm Trúc là ngoại lệ. Từ khi tại Mễ Lan đại điện phát sinh chuyện, Tây Nhĩ Duy Áo đã hạ lệnh miễn hắn quỳ.

      - Được rồi, mọi người không cần hành lễ. Hôm nay xem như bữa cơm gia đình đi. Cũng là ngày tẩy trần cho Âm Trúc, không nên câu thúc làm gì. Các người cứ xem ta như thúc thúc khoản đãi một bữa là được rồi.

      Tình cảnh lúc này, Tây Nhĩ Duy Áo đại đế có vẻ cực kỳ bình dị dễ gần.

      Đệ tử năm nhất thần âm hệ không biết thân phận của Hương Loan, Tây Nhĩ Duy Áo cũng không có chào hỏi nữ nhi, mà trực tiếp đi tới bên cạnh Diệp Âm Trúc, nhìn Ny Na mỉm cười nói:

      Delete
  2. I used your code to implement iads and it worked perfectly, although the first paragraph led me to believe it wouldn't work, there was no iOS folder and no AppController. Instead, I implemented it in the AppDelegate.

    Very simple and easy to follow tutorial, I had nothing to modify other than my RootViewController being navController_ and not ViewController.

    Thank you so much, now I have to figure out how to resize the Cocos2d interface so the ad doesn't overlay.

    ReplyDelete
  3. Hello Michael Webb,

    Could you please tell me how to disable iAD in some Scenes.
    It would be awesome! :D

    ReplyDelete
  4. what about the full screen Advertisement.This is just for the banner right?help me with suitable link.Thank you.

    ReplyDelete
  5. Hi! I recently posted a more step by step tutorial in my blog (which actually references this post). Check it out if you need further help: http://becomingindiedev.blogspot.com.es/2015/02/integrating-iad-in-cocos2d-x-v3x.html

    ReplyDelete
  6. Thank you for your very nice article, do not forget to read my articles also
    gambar lucu
    kata kata cinta
    kata kata galau
    kata kata lucu
    kata kata mutiara
    dp bbm cinta
    cara menghilangkan jerawat
    kata kata mutiara cinta
    are deliberately presented to the loyal readers.

    ReplyDelete
  7. I have followed all the steps one by one it also shows that add receive sucessfully but it didnt show on my screen.

    ReplyDelete