5

I am developing an application where i have to show iAds in all the pages of my application.. I created a subclass of UIView where i am initializing the the ADBannerView and its delegate methods.

But now if I add it in window in AppDelegate class it is giving me following error at run time "ADBannerView must be part of a view hierarchy managed by a UIViewController"..

I think this mean that I can use ADBanner only in UIViewController's subclass file??

if so then how can I make it global??

Thanks in Advance Shreya

Shreya
  • 198
  • 1
  • 12

1 Answers1

6

In AppDelegate class you can make a shared object.

- (ADBannerView *) sharedBannerView
{
    if (_sharedBannerView == nil)
    {
        Class classAdBannerView = NSClassFromString(@"ADBannerView");

        if (classAdBannerView != nil)
        {
            _sharedBannerView = [[classAdBannerView alloc] initWithFrame:CGRectMake(0, 480, 320, 50)];

            // pre 4.2 doesn't have the new AdBannerSize constants. 
            if (&ADBannerContentSizeIdentifierPortrait != NULL)
            {
                [_sharedBannerView setRequiredContentSizeIdentifiers:[NSSet setWithObjects:ADBannerContentSizeIdentifierPortrait, ADBannerContentSizeIdentifierLandscape, nil]];            
            }
            else
            {  
                [_sharedBannerView setRequiredContentSizeIdentifiers:[NSSet setWithObjects:ADBannerContentSizeIdentifier320x50, ADBannerContentSizeIdentifier480x32, nil]];            
            }
        }   
    }

    ((ADBannerView *)_sharedBannerView).backgroundColor = [UIColor whiteColor];

    return _sharedBannerView;
}

And add this shared object to the view wherever you need to display iAds. Hope you get it.

Neelam Verma
  • 3,232
  • 1
  • 22
  • 33
  • @user1036925: also take a look at this technical note from Apple: [TN2286: Implementing a Shared iAd Banner](https://developer.apple.com/library/ios/#technotes/tn2286/_index.html#//apple_ref/doc/uid/DTS40011212) – Rok Jarc Apr 19 '12 at 11:00