5

I am trying to remove an iAd view from the view hierarchy. My implementation successfully removes the iAd banner from the view, but I continue to receive the following error:

Unhandled error (no delegate or delegate does not implement didFailToReceiveAdWithError:)

According to Apple documentation, removing the iAd view is reasonable in cases where the user navigates away from a screen that displays an iAd and you don't expect them to return to that screen for a while:

If the user navigates from a screen of content with a banner view to a screen that does
not have a banner view, and you expect them to be on that screen for a long period of 
time, remove the banner view from the view hierarchy, set its delegate to nil and 
release it before transitioning to the new screen of content. More generally, avoid 
keeping a banner view around when it is invisible to the user.

So, I removed the banner view and set the delegate to nil, and this results in the banner disappearing from the view. However, I then start receiving the above mentioned error. Its not really clear how to do what Apple suggests. Here is what I have done. My use of iAd is in a class that is not a UIViewController (i.e. this is a cocos2d project). Hence, I utilize the RootViewController. In the header file of my class, I have:

RootViewController *viewController;
AppDelegate *app;
BannerViewController *bannerViewController;

In my class implementation file, I initialize the bannerViewController as follows:

app = (AppDelegate *)[[UIApplication sharedApplication] delegate];
viewController = [(AppDelegate *)[[UIApplication sharedApplication] delegate] viewController];

bannerViewController = [[BannerViewController alloc] initWithContentViewController:viewController];
app.window.rootViewController = bannerViewController;

When I am ready to remove the iAd banner permanently, I attempt to remove the banner view from the view hierarchy and set its delegate to nil as follows:

if (bannerViewController) {
    [viewController removeFromParentViewController];
    bannerViewController._bannerView.delegate = nil;
    [bannerViewController._bannerView removeFromSuperview];
    [bannerViewController release];
    bannerViewController = nil;
}

The bannerViewController is from Apples iAd Suite. The initialization and construction of the view hierarchy is as follows:

@interface BannerViewController () <ADBannerViewDelegate>

@end

@implementation BannerViewController {
    ADBannerView *_bannerView;
    UIViewController *_contentController;   // RootViewController
}

- (instancetype)initWithContentViewController:(UIViewController *)contentController
{

    self = [super init];
    if (self != nil) {
        // On iOS 6 ADBannerView introduces a new initializer, use it when available.
        if ([ADBannerView instancesRespondToSelector:@selector(initWithAdType:)]) {
            _bannerView = [[ADBannerView alloc] initWithAdType:ADAdTypeBanner];
        } 
        else {
            _bannerView = [[ADBannerView alloc] init];
        }
        _contentController = contentController; 
        _bannerView.delegate = self;
    }
    return self;
}

- (void)loadView
{
    UIView *contentView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    [contentView addSubview:_bannerView];

    // Setup containment of the _contentController.
    [self addChildViewController:_contentController];
    [contentView addSubview:_contentController.view];
    [_contentController didMoveToParentViewController:self];

    self.view = contentView;

}

My RootViewController handles the observers for iAd BannerViewActionNotification's.

What I have noticed is that even though I release the bannerViewController, the dealloc method is not called. Like I said, the iAd banner does disappear from the view, but the error messages keep coming. This suggests that I have not properly disconnected from iAd and continue to receive ad notifications.

So, what am I doing wrong? How should I be removing the banner view from the view hierarchy per Apple's recommendation.

JeffB6688
  • 3,782
  • 5
  • 38
  • 58
  • did you ever get this working? If so, can you share the code that properly removes the AdBannerView? – AdamT May 12 '15 at 09:55

0 Answers0