I am trying to integrate iAd with my existing Cocosd2d 1.x project. I picked up the codes from this site. I put this in my main menu class. it compiles and link fine but the banner is not showing. The NSlog shows that bannerViewDidLoadAd is called. What am I missing here? Your help is highly appreciated. The code is given below.
//iAd begin
-(void)onEnter
{
[super onEnter];
NSLog(#"onEnter called");
adView = [[ADBannerView alloc]initWithFrame:CGRectZero];
adView.delegate = self;
adView.requiredContentSizeIdentifiers = [NSSet setWithObjects:ADBannerContentSizeIdentifier320x50, ADBannerContentSizeIdentifier480x32, nil];
adView.currentContentSizeIdentifier = ADBannerContentSizeIdentifier480x32;
CGSize widowSize = [[CCDirector sharedDirector] winSize];
adView.center = CGPointMake(adView.frame.size.width/2, widowSize.height/2+145);
adView.hidden = YES;
}
-(void)onExit
{
NSLog(#"onExit called");
adView.delegate = nil;
[adView removeFromSuperview];
[adView release];
adView = nil;
[super onExit];
}
-(void)bannerViewDidLoadAd:(ADBannerView *)banner
{
NSLog(#"bannerViewDidLoadAd called");
adView.hidden = NO;
}
-(void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error
{
NSLog(#"banner didFailToReceiveAdWithError called");
adView.hidden = YES;
}
-(void)bannerViewActionDidFinish:(ADBannerView *)banner
{
NSLog(#"bannViewActiondidFinishe called");
[[UIApplication sharedApplication] setStatusBarOrientation:(UIInterfaceOrientation)[[CCDirector sharedDirector]deviceOrientation]];
}
-(BOOL)bannerViewActionShouldBegin:(ADBannerView *)banner willLeaveApplication:(BOOL)willLeave
{
NSLog(#"bannerviewActionShouldBegin called");
return YES;
}
//end iAd
You add this code to your onEnter method,
[[[CCDirector sharedDirector]view] addSubview:adView];
Regarding your specific question, I think #Liya is on the right track: you need to add the AdBannerView object to the view hierarchy; now, you're just creating the object but not using it.
Not sure if it will help, but I wrote recently a tutorial on integrating iAds with Cocos2d-x. The language is different (C++ instead of Objective-C), but the organization of code and some software engineering tips might be helpful if you want to integrate iAds with the scenes and layers of your game in Cocos2d: http://becomingindiedev.blogspot.com.es/2015/02/integrating-iad-in-cocos2d-x-v3x.html
Related
My iAd is white. It looks like bannerViewDidLoadAd is run when the iAd is not fully loaded. It happens only when I show my ViewController first time (application starts). When I go to another controller and return, iAd is loaded properly. Do you have any idea why?
- (void)bannerViewDidLoadAd:(ADBannerView *)banner {
_bannerView.hidden = NO;
[self.view setNeedsLayout];
}
- (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error {
_bannerView.hidden = YES;
[self.view setNeedsLayout];
}
-(void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
if (!_bannerView) {
_bannerView = [[ADBannerView alloc] initWithAdType:ADAdTypeBanner];
_bannerView.delegate = self;
_bannerView.hidden = YES;
CGRect bannerFrame = _bannerView.frame;
bannerFrame.origin.y = self.view.bounds.size.height - _bannerView.bounds.size.height;
_bannerView.frame = bannerFrame;
[self.view addSubview:_bannerView];
}
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
_bannerView.delegate = nil;
[_bannerView removeFromSuperview];
_bannerView = nil;
}
I've created a SpriteKit game where i create a banner ad in the main view controller and then hide and unhide the banner as required using NSNotification Center. That all works great from any SKScene in the game.
But if i click on the ad, and then eventually close it, instead of going back to the original SKScene a new view controller instance is created and i appear back at the first scene...like the whole application just rebooted.
In the view controller the banner ad is set up as follows:
- (void)viewWillLayoutSubviews
{
[super viewWillLayoutSubviews];
self.canDisplayBannerAds = YES;
_banner = [[ADBannerView alloc] initWithFrame:CGRectOffset(_banner.frame, 0, 0)];
_banner.delegate = self;
_banner.hidden = TRUE;
[self.view addSubview:_banner];
// Configure the view.
SKView * skView = (SKView *)self.originalContentView;
MainMenu* scene = [MainMenu sceneWithSize:skView.bounds.size];
scene.scaleMode = SKSceneScaleModeAspectFill;
[skView presentScene:scene];
Here's the delegate methods:
- (void)bannerViewDidLoadAd:(ADBannerView *)banner {
bannerAdIsLoaded = TRUE;
NSLog(#"Banner ad is loaded");
}
- (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error {
bannerAdIsLoaded = FALSE;
NSLog(#"Failed to receive banner ad");
}
- (BOOL)bannerViewActionShouldBegin:(ADBannerView *)banner willLeaveApplication:(BOOL)willLeave {
NSLog(#"Starting banner ad action");
return YES;
}
- (void)bannerViewActionDidFinish:(ADBannerView *)banner {
NSLog(#"Banner ad Did Finish");
}
-(void)bannerViewWillLoadAd:(ADBannerView *)banner {
NSLog(#"Loading a banner ad...");
}
When the ad is dismissed, how can i make it return to the last SKScene, where the ad was launched, and not relaunch the the application?
I've been struggling with this now for a long time. Where is my mistake? At the moment the hide and show seems to work, but everytime I come "back" to my viewcontroller I see that my view is shifted up, but there is no ad in there. But the first time I see the view controller, there is an ad. What am I doing wrong?
I just want to show the same ad across view controllers and this is like the parent UIViewController class, a lot of other view controllers inherit from:
#pragma mark View lifecycle
-(void)viewDidLoad
{
[super viewDidLoad];
if(![[NSUserDefaults standardUserDefaults] objectForKey:kInAppPurchaseNoAds]){
self.bannerContainer = ((AppDelegate *)[[UIApplication sharedApplication] delegate]).bannerView;
self.bannerContainer.frame = CGRectOffset(self.bannerContainer.frame, 0, self.view.frame.size.height);
[self.view addSubview:self.bannerContainer];
}
}
//Handle the in app purchases
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
//iAd
if(![[NSUserDefaults standardUserDefaults] objectForKey:kInAppPurchaseNoAds] && !self.bannerContainer){
self.bannerContainer.delegate = self;
}
if(self.bannerContainer.bannerLoaded){
[self showBanner];
}
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(checkIAdPurchase) name:IAPHelperProductPurchasedNotification object:nil];
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[self hideBanner];
self.bannerContainer.delegate = nil;
[[NSNotificationCenter defaultCenter] removeObserver:self name:IAPHelperProductPurchasedNotification object:nil];
}
#pragma mark Check iAd purchase
-(void)checkIAdPurchase
{
if([[NSUserDefaults standardUserDefaults] objectForKey:kInAppPurchaseNoAds] && self.bannerContainer){
[self hideBanner];
[self.bannerContainer removeFromSuperview];
self.bannerContainer.delegate = nil;
self.bannerContainer = nil;
}
}
#pragma mark IAd delegate
- (void)bannerViewDidLoadAd:(ADBannerView *)banner
{
[self showBanner];
}
- (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error
{
[self hideBanner];
}
#pragma mark Show and hide the banner
- (void)showBanner
{
if(!self.isBannerVisible){
[self.view layoutIfNeeded];
[UIView animateWithDuration:0.5
animations:^{
//Restore the constraint
self.mainViewBottomConstraint.constant = 50;
//Move the banner on
self.bannerContainer.frame = CGRectOffset(self.bannerContainer.frame, 0, -50);
[self.view layoutIfNeeded];
} completion:^(BOOL finished) {
self.isBannerVisible = YES;
}];
}
}
- (void)hideBanner
{
if(self.isBannerVisible){
[self.view layoutIfNeeded];
[UIView animateWithDuration:0.5
animations:^{
//Restore the constraint
self.mainViewBottomConstraint.constant = 0;
//Move the banner off
self.bannerContainer.frame = CGRectOffset(self.bannerContainer.frame, 0, self.bannerContainer.frame.size.height);
[self.view layoutIfNeeded];
} completion:^(BOOL finished) {
self.isBannerVisible = NO;
}];
}
}
Actually you are creating instance of iAd in AppDelegate and that's only one in the whole app.Now when you first time come to Vc then due to code in viewdidload it add to your Vc..
Now when you move to other view controller that viewcontroller also add iAd at viewdidload..You have only one object of iAd so it will add to at a time in One VC..So when you move to other Vc iAd will remove from that Vc and add to new VC..
Solution : You should call iAD subview code in ViewWillAppear...
How can i place an iAd Banner at the Top of the View in a SpriteKit Application?
The Banner is working perfectly but on the wrong position.
Here is the code for the iAd Banner, but the its always below:
ViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
// Configure the view.
SKView * skView = (SKView *)self.originalContentView;
skView.showsFPS = YES;
skView.showsNodeCount = YES;
// Create and configure the scene.
SKScene * scene = [MyScene sceneWithSize:skView.bounds.size];
scene.scaleMode = SKSceneScaleModeAspectFill;
self.canDisplayBannerAds = YES;
// Present the scene.
[skView presentScene:scene];
}
I can't tell from the code you posted how much you have implemented already, so for completeness...
Import iAd
#import iAd;
My preference has been to just create the ADBannerView class variable as a private instance iVar.
#implementation ViewController {
ADBannerView *_bannerView;
}
Then in viewDidLoad
-(void)viewDidLoad {
// iAd support
_bannerView = [[ADBannerView alloc] initWithAdType:ADAdTypeBanner];
_bannerView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
_bannerView.delegate = self;
[self.view addSubview:_bannerView];
}
For an iAd banner that displays across the top of the screen, this has been all that is required to get it to show up on screen.
For the delegate methods, my current strategy is to hide the banner when it doesn't have anything to display.
#pragma mark - iAd Methods
- (void)bannerViewDidLoadAd:(ADBannerView *)banner {
_bannerView.hidden = NO;
}
- (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error {
_bannerView.hidden = YES;
}
- (BOOL)bannerViewActionShouldBegin:(ADBannerView *)banner willLeaveApplication:(BOOL)willLeave {
return YES;
}
- (void)bannerViewActionDidFinish:(ADBannerView *)banner {
}
I am using SpriteBuilder (which integrates with Cocos2d v3.0). I built an app and now I want to put in an iAd at the very top that pops up when I call it, and hides when I tell it to. What's the simplest way to do this?
Keep in mind I am using SpriteBuilder with Cocos2d. And just because I am using SpriteBuilder does not mean I am not using Xcode 5 as well. I am fully involved in Xcode as well. SpriteBuilder does not write the code for me, I do that.
Add iAd framework to your dependencies.
In your header file for your game scene, add the ADBannerViewDelegate, for instance:
#interface MainScene : CCNode <CCPhysicsCollisionDelegate, ADBannerViewDelegate>
In your implementation file, add the instance variable _bannerView:
#implementation MainScene {
ADBannerView *_bannerView;
}
And finally, insert the the iAD code (with some cocos2d tweaks). Here's my implementation for a game in portrait mode with a top banner. There's no hide method, but it's pretty easy to implement.
# pragma mark - iAd code
-(id)init
{
if( (self= [super init]) )
{
// On iOS 6 ADBannerView introduces a new initializer, use it when available.
if ([ADBannerView instancesRespondToSelector:#selector(initWithAdType:)]) {
_adView = [[ADBannerView alloc] initWithAdType:ADAdTypeBanner];
} else {
_adView = [[ADBannerView alloc] init];
}
_adView.requiredContentSizeIdentifiers = [NSSet setWithObject:ADBannerContentSizeIdentifierPortrait];
_adView.currentContentSizeIdentifier = ADBannerContentSizeIdentifierPortrait;
[[[CCDirector sharedDirector]view]addSubview:_adView];
[_adView setBackgroundColor:[UIColor clearColor]];
[[[CCDirector sharedDirector]view]addSubview:_adView];
_adView.delegate = self;
}
[self layoutAnimated:YES];
return self;
}
- (void)layoutAnimated:(BOOL)animated
{
// As of iOS 6.0, the banner will automatically resize itself based on its width.
// To support iOS 5.0 however, we continue to set the currentContentSizeIdentifier appropriately.
CGRect contentFrame = [CCDirector sharedDirector].view.bounds;
if (contentFrame.size.width < contentFrame.size.height) {
_bannerView.currentContentSizeIdentifier = ADBannerContentSizeIdentifierPortrait;
} else {
_bannerView.currentContentSizeIdentifier = ADBannerContentSizeIdentifierLandscape;
}
CGRect bannerFrame = _bannerView.frame;
if (_bannerView.bannerLoaded) {
contentFrame.size.height -= _bannerView.frame.size.height;
bannerFrame.origin.y = contentFrame.size.height;
} else {
bannerFrame.origin.y = contentFrame.size.height;
}
[UIView animateWithDuration:animated ? 0.25 : 0.0 animations:^{
_bannerView.frame = bannerFrame;
}];
}
- (void)bannerViewDidLoadAd:(ADBannerView *)banner {
[self layoutAnimated:YES];
}
- (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error {
[self layoutAnimated:YES];
}