来自AppDelegate的共享ADBannerView - 如何从委托中获取广告

时间:2013-04-17 20:26:27

标签: iphone ios objective-c appdelegate adbannerview

我已经做了很多阅读,在下面找到了几个SO问题: 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?

我想在didFinishLoading方法中的AppDelegate类中加载一个ADBannerView。希望这个ADBannerView可以被所有相关的ViewControllers检索(通过导入AppDelegate.h),然后当用户在VC之间移动时相应地显示。

我实施的代码如下:

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

我还尝试过使用[NSNotificationCenter defaultCenter] postNotificationName:方法的变体,但最后我遇到了同样的问题:

问题: 在ViewController中我想显示ADBannerView如何从AppDelegate中检索它?

我目前:

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

但是我遇到了很多错误,似乎无法绕过它。

错误:UIApplication没有可见的@interface声明扇区sharedAdBannerView。

我确信我错过了一些非常基本的东西。

非常感谢任何帮助......谢谢,詹姆斯

2 个答案:

答案 0 :(得分:0)

您必须在ADBannerView.h中导入AppDelegate.m。 顺便说一句,看看Apple示例项目" iAd Suite"

答案 1 :(得分:0)

创建共享iAdBannerView是其中一个主题,您可以阅读几天,最后放弃或编写必要的几行代码。我选择了第二种方法并创建了几行代码。

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

要使用此类,只需继承并覆盖此示例中的viewDidLoad方法。

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 ...
    }
}

它运作完美:100%的所有请求都填写了所有支持iAd的国家/地区。