使用UITableViews在整个应用程序中使用ADBannerView的共享实例 [英] using shared instance of ADBannerView across app with UITableViews

查看:68
本文介绍了使用UITableViews在整个应用程序中使用ADBannerView的共享实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有多个UITableView的应用程序,并且正在实施iAD。根据Apple文档( http ://developer.apple.com/library/ios/#technotes/tn2286/_index.html#//apple_ref/doc/uid/DTS40011212 )我创建了一个共享横幅,该横幅属于我的应用程序委托和应用程序委托还是横幅的委托。这样效果很好,并且在加载横幅广告和用户切换屏幕之后,广告可以在各种视图控制器上很好地展示。

I have an app with multiple UITableViews and am in the process of implementing iADs. Per the Apple documentation (http://developer.apple.com/library/ios/#technotes/tn2286/_index.html#//apple_ref/doc/uid/DTS40011212) I have created a shared banner that belongs to my app delegate and the application delegate is also the banner's delegate. This works well and the ads show nicely across the various view controllers AFTER the banner is loaded and the user switches screens.

问题是,在之所以出现第一个viewController,是因为在加载横幅广告之前,出现了视图控制器的viweWillAppear方法(在这里我称之为 fixUpAdView方法)。

The problem is that there is no ad seen on the first viewController that comes up because the viweWillAppear method of the view controller (where I am calling my "fixUpAdView" method) comes before the banner is loaded.

我想我没有得到的部分是(来自Apple文档):
让您的应用程序委托告诉当前视图控制器是否应该显示或隐藏横幅。您可以使用UINavigationControllerDelegate或UITabBarControllerDelegate协议来推送横幅以进行显示。我了解我需要在我的bannerViewDidLoadAd和failToReceive方法中放一些东西,但是在执行方法上有些困惑。

I guess the part that I am not getting is this (from the apple documentation): "Have your application delegate tell the current view controller if it should show or hide the banner. You can use UINavigationControllerDelegate or UITabBarControllerDelegate protocol to push the banner to show it." I understand that I need to be putting something into my bannerViewDidLoadAd and failToReceive methods but am a bit confused as to how to do this.

我不希望广告展示在我所有的视图控制器上(只有6个),我的应用程序中也有几个模式视图(这些视图上没有广告)。

I do not want the ad to show on all of my view controllers (just 6 of them) and I also have several modal views in the app (no ad on any of these).

以下是其中一些我的代码:
在我的appDelegate中:

Here is some of my code: In my appDelegate:

- (void)bannerViewDidLoadAd:(ADBannerView *)banner {
    NSLog(@"bannerViewDidLoadAD");
    if (!_adBannerViewIsVisible) 
        _adBannerViewIsVisible = YES;

}


- (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error
{

    NSLog(@"BannerAd didfailtoreceive");
    if (_adBannerViewIsVisible)
        _adBannerViewIsVisible = NO;

}

- (ADBannerView *)sharedAdBannerView 
{
    if (_sharedAdBannerView == nil) {

        Class classAdBannerView = NSClassFromString(@"ADBannerView");

        if (classAdBannerView != nil) {
            _sharedAdBannerView = [[classAdBannerView alloc] initWithFrame:CGRectZero];
            [_sharedAdBannerView setRequiredContentSizeIdentifiers:[NSSet setWithObjects: 
                                                              ADBannerContentSizeIdentifier320x50, 
                                                              ADBannerContentSizeIdentifier480x32, nil]];
            [_sharedAdBannerView setCurrentContentSizeIdentifier:ADBannerContentSizeIdentifier320x50];            
            [_sharedAdBannerView setFrame:CGRectOffset([_sharedAdBannerView frame], 0, 
                                                 -(iAD_BANNER_HEIGHT))];
            [_sharedAdBannerView setDelegate:self];
        }
    }

    return _sharedAdBannerView;
}

在我看来,控制器:

- (void)viewWillAppear:(BOOL)animated {

    if ([[AppDelegate ad] shouldShowAds]) {

        if (!self.contentView) {
            self.contentView = [[UIView alloc] initWithFrame:[[self view] bounds]];
            [self.view addSubview:_contentView];
        }
        [self.contentView addSubview:topView];
        [self fixupAdView];
        [self.view addSubview:[[AppDelegate ad] sharedAdBannerView]];
    }
    [super viewWillAppear:NO];
}


#pragma mark
#pragma mark iADS

- (void)fixupAdView {

    if ([[AppDelegate ad] sharedAdBannerView] != nil) {        

        [[[AppDelegate ad] sharedAdBannerView] setCurrentContentSizeIdentifier:ADBannerContentSizeIdentifier320x50];
        [UIView beginAnimations:@"fixupViews" context:nil];

        if ([[AppDelegate ad] adBannerViewIsVisible]) {
            CGRect adBannerViewFrame = [[[AppDelegate ad] sharedAdBannerView] frame];
            adBannerViewFrame.origin.x = 0;
            adBannerViewFrame.origin.y = 0;
            [[[AppDelegate ad] sharedAdBannerView] setFrame:adBannerViewFrame];
            CGRect contentViewFrame = _contentView.frame;
            contentViewFrame.origin.y = iAD_BANNER_HEIGHT;
            contentViewFrame.size.height = self.view.frame.size.height - 
            iAD_BANNER_HEIGHT;
            _contentView.frame = contentViewFrame;
        } 
        else {
            CGRect adBannerViewFrame = [[[AppDelegate ad] sharedAdBannerView] frame];
            adBannerViewFrame.origin.x = 0;
            adBannerViewFrame.origin.y = -(iAD_BANNER_HEIGHT);
            [[[AppDelegate ad] sharedAdBannerView] setFrame:adBannerViewFrame];
            CGRect contentViewFrame = _contentView.frame;
            contentViewFrame.origin.y = 0;
            contentViewFrame.size.height = self.view.frame.size.height;
            _contentView.frame = contentViewFrame;            
        }
        [UIView commitAnimations];
    }   
}


推荐答案

使用NSNotificationCenter解决此问题的方法非常有用,现在我的iAd广告一加载就开始展示-是的!如果有人需要,请在此处输入我的额外代码:
(在我的appDelegate.m中)

Using NSNotificationCenter to solve this problem worked like a charm and now my iAds are coming up as soon as they are loaded - yay! If anyone else needs this here is the extra code I input: (in my appDelegate.m)

- (void)bannerViewDidLoadAd:(ADBannerView *)banner {
NSLog(@"bannerViewDidLoadAD");
if (!_adBannerViewIsVisible) {
    _adBannerViewIsVisible = YES;
    [[NSNotificationCenter defaultCenter] postNotificationName:@"adjustAdBannerView" object:nil];
}

}

- (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error

{

 NSLog(@"BannerAd didfailtoreceive");
if (_adBannerViewIsVisible) {
    _adBannerViewIsVisible = NO;
    [[NSNotificationCenter defaultCenter] postNotificationName:@"adjustAdBannerView" object:nil];
}

}

并在我的View Controller中(在viewWillAppear中):

and in my View Controller (in viewWillAppear):

[[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(fixupAdView)
                                                 name:@"adjustAdBannerView"
                                               object:nil];

这篇关于使用UITableViews在整个应用程序中使用ADBannerView的共享实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆