2

I have done a fair bit of reading around on have found a few SO questions below: using shared instance of ADBannerView across app with UITableViews How to make a single shared instance of iAd banner throughout many view controllers? AdBannerView shared across multiple views, including the rootviewcontroller, how?

I would like to have a single ADBannerView that is loaded in my AppDelegate class in the didFinishLoading method. The hope is that this ADBannerView can then be retrieved by all of the relevant ViewControllers (by importing the AppDelegate.h) and then just displayed accordingly when the user moves between VC's.

The code that I have implemented is the following:

AppDelegate.h @interface AppDelegate : UIResponder

@property (strong, nonatomic) IBOutlet ADBannerView *adView;
@property (nonatomic, assign) BOOL bannerIsVisible;

- (ADBannerView *)sharedAdBannerView;

AppDelegate.m

ADBannerView *adView;
BOOL bannerIsVisible;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions(NSDictionary *)launchOptions
{   
    // create the iAdBannerView
    adView = [[ADBannerView alloc] initWithFrame:CGRectZero];
    adView.frame = CGRectOffset(adView.frame, 0, -50);
    adView.requiredContentSizeIdentifiers = [NSSet setWithObject:ADBannerContentSizeIdentifier320x50];
    adView.currentContentSizeIdentifier = ADBannerContentSizeIdentifier320x50;
    adView.delegate=self;
    self.bannerIsVisible=NO;

    // Override point for customization after application launch.
    return YES;
}

#pragma begin of iAdBannerView Delegate Methods
- (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error
{

    if (self.bannerIsVisible)
    {
        [UIView beginAnimations:@"animateAdBannerOff" context:NULL];
        banner.frame = CGRectOffset(banner.frame, 0, -50);
        [UIView commitAnimations];
        self.bannerIsVisible = NO;
    }

}

- (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;
    }

}

- (ADBannerView *)sharedAdBannerView
{
    return adView;
}

I have also tried a variation using the [NSNotificationCenter defaultCenter] postNotificationName: methods but in the end I am faced with the same issue:

Problem: In the ViewController I want to display the ADBannerView how do I retrieve it from the AppDelegate?

I have currently:

ADBannerView *banner = [[UIApplication sharedApplication] sharedAdBannerView];
[self.view addSubview:banner];

But I am getting a multitude of errors and cannot seem to get around it.

Error: No visible @interface for UIApplication declares the sector sharedAdBannerView.

I'm sure I'm missing something pretty fundamental.

Any help would be greatly appreciated... Thanks, James

Community
  • 1
  • 1
JamesLCQ
  • 341
  • 6
  • 12

2 Answers2

0

You've to import ADBannerView.h in your AppDelegate.m. By the way, have a look at Apple sample project "iAd Suite"

unixo
  • 548
  • 6
  • 16
0

Creating a shared iAdBannerView is one of these topics, were you can read for days and in the end either give up or write the few lines of code necessary. I've chosen the second way and created this few lines of code.

import UIKit
import iAd

// Used thrughout the applcation to show banner when possible
private let applicationADBannerView = ADBannerView(adType: ADAdType.Banner)

// Simple base class for controllers to share one banner view.
// To use this class, inherit a view controller from it and add
// a UIView as container for the banner. The size of the
// container is used as banner size
internal class BaseViewControllerWithBanner: UIViewController, ADBannerViewDelegate {
    private var bannerContainer: UIView?

    internal func viewDidLoad(bannerContainer: UIView) {
        super.viewDidLoad()
        self.bannerContainer = bannerContainer
    }

    internal func bannerView(banner: ADBannerView!, didFailToReceiveAdWithError error: NSError!) {
        log(error)
        applicationADBannerView.delegate = nil
        applicationADBannerView.removeFromSuperview()
    }

    internal override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)

        applicationADBannerView.delegate = self
        applicationADBannerView.frame = bannerContainer?.frame ?? CGRectZero
        bannerContainer?.addSubview(applicationADBannerView)
    }

    internal override func viewWillDisappear(animated: Bool) {
        super.viewWillDisappear(animated)

        applicationADBannerView.delegate = nil
        applicationADBannerView.removeFromSuperview()
    }
}

To use this class simply inherit and override the viewDidLoad method like in this example.

import UIKit

internal class BanneredViewController: BaseViewControllerWithBanner {
    @IBOutlet weak var bannerViewContainer: UIView! // Position and link this in the xib or storyboard

    internal override func viewDidLoad() {
        log("BanneredViewController: viewDidLoad")
        super.viewDidLoad(bannerViewContainer)
        ... do all other stuff ...
    }
}

And it works perfectly: 100% of all Requests get filled in all countries, that support iAd's.

jboi
  • 11,324
  • 4
  • 36
  • 43