using shared instance of ADBannerView across app with UITableViews

不想你离开。 提交于 2019-12-21 17:48:05

问题


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.

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.

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.

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

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

In my view controller:

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

回答1:


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

}

and in my View Controller (in viewWillAppear):

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



回答2:


I've been struggling with this myself, and this and many other answers have been very helpful. However they all seem to relay on the someViewController.m code interacting with the AppDelegate, which seems the wrong way around.

My solution is to have the App Delegate pass the adBannerView object to the subViewControllers like so:

- (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error
{
    if (VERBOSE) NSLog(@"sharedAdBannerView %@ failed with error: %@", banner, error);
    if (self.adBannerIsVisible)
    {
        [someViewController setAdBannerViewIsVisible:NO];
        [someViewController setAdBannerView:nil];
        [otherViewController setAdBannerViewIsVisible:NO];
        [otherViewController setAdBannerView:nil];
        [[NSNotificationCenter defaultCenter] postNotificationName:@"adjustAdBannerView" object:nil];
    }
}

- (void)bannerViewDidLoadAd:(ADBannerView *)banner
{
    if (VERBOSE) NSLog(@"sharedAdBannerView %@ loaded", banner);
    if (!self.adBannerIsVisible)
    {
        [someViewController setAdBannerViewIsVisible:YES];
        [someViewController setAdBannerView:sharedAdBannerView];
        [otherViewController setAdBannerViewIsVisible:YES];
        [otherViewController setAdBannerView:sharedAdBannerView];
        [[NSNotificationCenter defaultCenter] postNotificationName:@"adjustAdBannerView" object:nil];
    }
}

And then the someViewController can have code to show or remove the adBannerView object from view.

Would there be issues with the same AdBannerView being used in several different views?



来源:https://stackoverflow.com/questions/10433565/using-shared-instance-of-adbannerview-across-app-with-uitableviews

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!