2

No answers yet, but i've got an update that seems to have fixed the problem, any ideas why it works now though?!

I've got it to work in the way I intended by doing the following to every class that needs to display an adBanner:

1. In the layoutForCurrentOrientation method I added the following:

adBanner.delegate = self;

[self.view addSubview:adBanner];

2. In the deAlloc method on each class, I removed the following:

[adBanner removeFromSuperview];

Original question:

I'm attempting to use the iAdSuite sample code from Apple to use a single adBanner instance shared across all my views.

The sample implementation is designed to show the adbanner on each view that is called by the rootViewController, however, I would like my app to have ads on the rootViewController view also.

In my amended code:-

When I fire up the app, no banner is shown on the rootView, even though a method is called to request an ad banner. The class is set as the delegate for the ad and the delegate methods are available. These are called and the log for (adBanner.bannerLoaded) is NO.

As it is a shared object, if I switch views to from the rootView the ad is displayed in the other view. When I return back to the rootView, the delegate method log shows that a banner is loaded, and it is positioned in a visible portion of the view. But the banner isn't visible.

In summary, i'm using the iAdSuite sample code for the AdBannerNavigation project, and trying to use it so that ad banners show on all views, including the rootViewController.

Any help appreciated!

The code i'm using is available here:

http://developer.apple.com/library/ios/#samplecode/iAdSuite/Introduction/Intro.html

My amended rootViewController.h:

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

@interface RootViewController : UITableViewController <ADBannerViewDelegate>

@end

My amended rootViewController.m

#import "RootViewController.h"
#import "TextViewController.h"
#import "MapViewController.h"

#import "AdBannerNavigationAppDelegate.h" 
// for SharedAdBannerView macro
// #define SharedAdBannerView ((AdBannerNavigationAppDelegate *)[[UIApplication sharedApplication] delegate]).adBanner

#import <iAd/iAd.h>


@interface RootViewController()

// Layout the Ad Banner and Content View to match the current orientation.
// The ADBannerView always animates its changes, so generally you should
// pass YES for animated, but it makes sense to pass NO in certain circumstances
// such as inside of -viewDidLoad.
- (void)layoutForCurrentOrientation:(BOOL)animated;

// A simple method that creates an ADBannerView
// Useful if you need to create the banner view in code
// such as when designing a universal binary for iPad
- (void)createADBannerView;

@end




- (void)viewDidLoad
{
    [super viewDidLoad];
    [self createADBannerView];
    [self layoutForCurrentOrientation:NO];
}


- (void)viewDidUnload
{

    ADBannerView *adBanner = SharedAdBannerView;
    adBanner.delegate = nil;
    [adBanner removeFromSuperview];

}


- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self layoutForCurrentOrientation:NO];
}


- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
    [self layoutForCurrentOrientation:YES];
}


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return YES;
}



- (void)dealloc
{

    ADBannerView *adBanner = SharedAdBannerView;
    adBanner.delegate = nil;
    [adBanner removeFromSuperview];

    [super dealloc];
}





- (void)createADBannerView
{

    ADBannerView *adBanner = SharedAdBannerView;


    NSString *contentSize;
    if (&ADBannerContentSizeIdentifierPortrait != nil)
    {
        contentSize = UIInterfaceOrientationIsPortrait(self.interfaceOrientation) ? ADBannerContentSizeIdentifierPortrait : ADBannerContentSizeIdentifierLandscape;
    }
    else
    {

        contentSize = UIInterfaceOrientationIsPortrait(self.interfaceOrientation) ? ADBannerContentSizeIdentifier320x50 : ADBannerContentSizeIdentifier480x32;
    }


    CGRect frame;
    frame.size = [ADBannerView sizeFromBannerContentSizeIdentifier:contentSize];
    frame.origin = CGPointMake(0.0f, CGRectGetMaxY(self.view.bounds));

    adBanner.frame = frame;


    adBanner.delegate = self;

    adBanner.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleTopMargin;

    adBanner.requiredContentSizeIdentifiers =
    (&ADBannerContentSizeIdentifierPortrait != nil) ?
    [NSSet setWithObjects:ADBannerContentSizeIdentifierPortrait, ADBannerContentSizeIdentifierLandscape, nil] : 
    [NSSet setWithObjects:ADBannerContentSizeIdentifier320x50, ADBannerContentSizeIdentifier480x32, nil];


    [self.view addSubview:adBanner];


   }






- (void)layoutForCurrentOrientation:(BOOL)animated
{
    ADBannerView *adBanner = SharedAdBannerView;



    CGFloat animationDuration = animated ? 0.2f : 0.0f;

    CGRect contentFrame = self.view.bounds;

    CGPoint bannerOrigin = CGPointMake(CGRectGetMinX(contentFrame), CGRectGetMaxY(contentFrame));
    CGFloat bannerHeight = 0.0f;


    if (UIInterfaceOrientationIsLandscape(self.interfaceOrientation))
        adBanner.currentContentSizeIdentifier = (&ADBannerContentSizeIdentifierLandscape != nil) ? ADBannerContentSizeIdentifierLandscape : ADBannerContentSizeIdentifier480x32;
    else
        adBanner.currentContentSizeIdentifier = (&ADBannerContentSizeIdentifierPortrait != nil) ? ADBannerContentSizeIdentifierPortrait : ADBannerContentSizeIdentifier320x50; 
    bannerHeight = adBanner.bounds.size.height;


    if (adBanner.bannerLoaded)
    {
        contentFrame.size.height -= bannerHeight;
        bannerOrigin.y -= bannerHeight;
    }
    else
    {
        bannerOrigin.y += bannerHeight;
    }


    [UIView animateWithDuration:animationDuration
                     animations:^{

                         adBanner.frame = CGRectMake(bannerOrigin.x, bannerOrigin.y, adBanner.frame.size.width, adBanner.frame.size.height);
                     }];


    NSLog(@"%f is y pos, height=%f, is it loaded...%@", adBanner.frame.origin.y, adBanner.frame.size.height, adBanner.bannerLoaded?@"YES":@"NO");

}



- (void)bannerViewDidLoadAd:(ADBannerView *)banner
{
    [self layoutForCurrentOrientation:YES];
}

- (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error
{
    [self layoutForCurrentOrientation:YES];
}

- (BOOL)bannerViewActionShouldBegin:(ADBannerView *)banner willLeaveApplication:(BOOL)willLeave
{
    return YES;
}

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

}


@end
Ray A
  • 307
  • 2
  • 14

0 Answers0