Adding iAds to Cocos2d-x on iOS

Posted on 2013-11-26 18:08  Flex/AS Programmer  阅读(265)  评论(0编辑  收藏  举报

http://www.mwebb.me.uk/2013/08/adding-iads-to-cocos2d-x-on-ios.html

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 targer, got to Summary, find Link Frameworks and 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, ADBannerViewDelegate,  GADBannerViewDelegate >{
  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 add 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.

Copyright © 2024 Flex/AS Programmer
Powered by .NET 8.0 on Kubernetes