2

我做了相当多的阅读,发现以下几个问题: 使用 UITableViews 跨应用程序使用 ADBannerView 的共享实例 如何在许多视图控制器中制作 iAd 横幅的单个共享实例? AdBannerView 跨多个视图共享,包括 rootviewcontroller,如何?

我希望在 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。

我敢肯定我错过了一些非常基本的东西。

任何帮助将不胜感激......谢谢,詹姆斯

4

2 回答 2

0

你必须ADBannerView.h在你的AppDelegate.m. 顺便看看 Apple 示例项目“ iAd Suite

于 2014-12-05T17:19:19.467 回答
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 的国家/地区得到填写。

于 2015-01-15T10:43:51.577 回答