Banner View Concepts

A banner view periodically retrieves advertisements from the iAd App Network and displays them to the user. Your app dedicates a small portion of a user interface screen to a banner view, as shown in Figure 1-1.

Figure 1-1  An advertisement in a banner view

Advertisements define an action that takes place when the user taps on the banner. For example, an advertisement could launch another app or temporarily cover your app’s user interface to present an interactive advertisement. Figure 1-2 shows the transition caused by a user tapping the display.

Figure 1-2  A banner action

The user always remains in control of the process; only the user decides when they want to see the content associated with the banner advertisement.

Banner Views Require a View Controller

Any user interface screen that includes a banner view must be managed by a view controller (a class that subclasses UIViewController). This allows a triggered action to cover your user interface with an additional advertising screen. Whenever a banner view is visible, it must be part of a view hierarchy that is attached to the view property of a view controller. The simplest way to accomplish this is to instantiate the view as part of the nib file used to instantiate the view controller’s interface.

Creating a Banner View

Listing 1-1 shows the simplest code that a view controller might use to programmatically create a banner view in portrait mode.

Listing 1-1  Programmatically creating a portrait banner view

ADBannerView *adView = [[ADBannerView alloc] initWithFrame:CGRectZero];
adView.currentContentSizeIdentifier = ADBannerContentSizeIdentifierPortrait;
[self.view addSubview:adView];

Banner View Sizes

iAd supports different banner sizes for portrait and landscape apps. The exact size of advertisements depends on the device the banner is being shown on. On an iPhone, a portrait advertisement is 320 x 50 points and 480 x 32 points for a landscape advertisement. On an iPad, a portrait advertisement is 768 x 66 points and 1024 x 66 points for a landscape advertisement. In the future, additional sizes may be exposed by iAd.

../Art/ad_sizes.jpg

To ensure that advertisements are displayed properly, a banner view must always be sized to match one of the built-in advertising sizes. The ADBannerView class enforces this by preventing you from changing the frame directly. Instead, you change a banner view’s frame by setting the currentContentSizeIdentifier property. Changing the value stored in this property resizes the banner view’s frame to the match the size for the provided identifier. Before you can set a particular size identifier, the size identifier must also be included in the set of size identifiers included in the requiredContentSizeIdentifiers property. For example, Listing 1-2 shows how your view controller could programmatically create a landscape banner view.

Listing 1-2  Programmatically creating a landscape banner view

ADBannerView *adView = [[ADBannerView alloc] initWithFrame:CGRectZero];
adView.requiredContentSizeIdentifiers = [NSSet setWithObject:ADBannerContentSizeIdentifierLandscape];
adView.currentContentSizeIdentifier = ADBannerContentSizeIdentifierLandscape;
[self.view addSubview:adView];

If your app needs the exact size of the advertisement to use at runtime, it calls the sizeFromBannerContentSizeIdentifier: class method, passing in either ADBannerContentSizeIdentifierLandscape or ADBannerContentSizeIdentifierPortrait.

Thread Safety

A banner view is a user interface element. As with other user interface elements, you should only reference it from your app’s main thread.