In the Get Started guide, you learned how to deploy the SDK and display a test banner ad. This guide goes into further customizations that are possible with banner ads.
To see implementations of these banner ad customizations, download the iOS API Demo app in Objective-C or Swift.
Banner size
Google Mobile Ads supports the following banner sizes:
Size (WxH) | Description | Availability | AdSize constant |
---|---|---|---|
320x50 | Standard banner | Phones and tablets | kGADAdSizeBanner |
320x100 | Large banner | Phones and tablets | kGADAdSizeLargeBanner |
300x250 | IAB medium rectangle | Phones and tablets | kGADAdSizeMediumRectangle |
468x60 | IAB full-size banner | Tablets | kGADAdSizeFullBanner |
728x90 | IAB leaderboard | Tablets | kGADAdSizeLeaderboard |
Screen width x 32|50|90 | Smart banner | Phones and tablets | kGADAdSizeSmartBannerPortrait kGADAdSizeSmartBannerLandscape
|
Smart Banners
Smart Banners are ad units that render screen-wide banner ads on any screen size across different devices in either orientation. Smart Banners help deal with increasing screen fragmentation across different devices by "smartly" detecting the width of the phone in its current orientation, and making the ad view that size.
Three ad heights (in density-independent pixel [dp]) are implemented in Smart Banners:
32
: device screen height <= 40050
: 400 < device screen height <= 72090
: device screen height > 720
For some devices, such as phones, the height of the device varies with its orientation. Typically, Smart Banner ads on phones are full width x 50dp in portrait and full width x 32dp in landscape, while on tablets, ads are full width x 90dp in both orientations.
When an image ad isn't large enough to take up the entire allotted space, the image is centered and the space on either side is filled in.
To use Smart Banners, you need to use the constant
kGADAdSizeSmartBannerPortrait
or kGADAdSizeSmartBannerLandscape
, like so:
Objective-C
// Use kGADAdSizeSmartBannerLandscape if your app is running in landscape.
GADBannerView *bannerView = [[GADBannerView alloc] initWithAdSize:kGADAdSizeSmartBannerPortrait];
Swift
// Use kGADAdSizeSmartBannerLandscape if your app is running in landscape.
let bannerView = GADBannerView(adSize: kGADAdSizeSmartBannerPortrait)
See the AdMob Banner Sizes example for an implementation of Smart Banners in the iOS API Demo app.
Ad lifecycle event callbacks
You may optionally track ad lifecycle events like request failures or
"click-through" by implementing all or part of GADBannerViewDelegate
.
The following code snippet shows a sample implementation:
Objective-C
@protocol GADBannerViewDelegate <NSObject>
@optional
- (void)adViewDidReceiveAd:(GADBannerView *)bannerView;
- (void)adView:(GADBannerView *)bannerView
didFailToReceiveAdWithError:(GADRequestError *)error;
- (void)adViewWillPresentScreen:(GADBannerView *)bannerView;
- (void)adViewDidDismissScreen:(GADBannerView *)bannerView;
- (void)adViewWillDismissScreen:(GADBannerView *)bannerView;
- (void)adViewWillLeaveApplication:(GADBannerView *)bannerView;
@end
Swift
public protocol GADBannerViewDelegate : NSObjectProtocol {
optional public func adViewDidReceiveAd(bannerView: GADBannerView!)
optional public func adView(bannerView: GADBannerView!,
didFailToReceiveAdWithError error: GADRequestError!)
optional public func adViewWillPresentScreen(bannerView: GADBannerView!)
optional public func adViewWillDismissScreen(bannerView: GADBannerView!)
optional public func adViewDidDismissScreen(bannerView: GADBannerView!)
optional public func adViewWillLeaveApplication(bannerView: GADBannerView!)
}
These methods may be implemented in a separate object like a view controller as shown in the following example:
Objective-C
@import GoogleMobileAds;
@interface ViewController : UIViewController <GADBannerViewDelegate> {
}
@end
Swift
import GoogleMobileAds
class ViewController: UIViewController, GADBannerViewDelegate {
}
Or they may be implementd as part of a GADBannerView
subclass
as shown in the following example:
Objective-C
@import GoogleMobileAds;
@interface MyBannerView : GADBannerView <GADBannerViewDelegate> {
}
@end
Swift
import GoogleMobileAds
class MyBannerView: GADBannerView, GADBannerViewDelegate {
}
Remember to set the delegate before making the request for an ad:
Objective-C
self.bannerView.delegate = self;
Swift
bannerView.delegate = self
adViewDidReceiveAd:
This callback is sent when loadRequest:
has succeeded. This is
a good opportunity to add the sender to the view hierarchy if it
has been hidden until now; for example:
Objective-C
- (void)adViewDidReceiveAd:(GADBannerView *)bannerView {
bannerView.hidden = NO;
}
Swift
func adViewDidReceiveAd(bannerView: GADBannerView!) {
bannerView.hidden = false
}
adView:didFailToReceiveAdWithError:
This callback is sent when loadRequest:
has failed,
typically because of network failure,
an app configuration error,
or a lack of ad inventory.
You may wish to log these events for debugging:
Objective-C
- (void)adView:(GADBannerView *)adView didFailToReceiveAdWithError:(GADRequestError *)error {
NSLog(@"adView:didFailToReceiveAdWithError: %@", error.localizedDescription);
}
Swift
func adView(bannerView: GADBannerView!,
didFailToReceiveAdWithError error: GADRequestError!) {
print("adView:didFailToReceiveAdWithError: \(error.localizedDescription)")
}
adViewWillPresentScreen:
This callback is sent immediately before the user is presented with
a full-screen ad UI in response to their touching the sender. At this
point, you should pause any animations, timers, or other activities
that assume user interaction and save app state, much like on
UIApplicationDidEnterBackgroundNotification
. Typically, the user
simply browses the full-screen ad and dismisses it, generating
adViewDidDismissScreen:
and returning control to your app. If the
banner's action was either Click-to-App-Store or Click-to-iTunes or the user
presses Home within the ad, however, your app will be backgrounded and
potentially terminated.
In these cases under iOS 4.0+, the next method invoked will be your root
view controller's applicationWillResignActive:
, followed by
adViewWillLeaveApplication:
.
adViewDidDismissScreen:
- Sent when the user has exited the sender's full-screen UI.
adViewWillDismissScreen:
- Sent immediately before the sender's full-screen UI is dismissed, restoring
your app and the root view controller. At this point, you should restart any
foreground activities paused as part of
adViewWillPresentScreen:
.
adViewWillLeaveApplication:
- Sent just before the app gets backgrounded or terminated as a result
of the user touching a Click-to-App-Store or Click-to-iTunes banner. The normal
UIApplicationDelegate
notifications likeapplicationDidEnterBackground:
arrive immediately before this.
Do not request an ad in applicationWillEnterForeground:
, as the request will
be ignored. Place the request in applicationDidBecomeActive:
instead.
Objective-C delegates are not retained and may be messaged asynchronously before the delegating object is finally deallocated.
See the AdMob Ad Delegate example for an implementation of ad lifecycle event callbacks in the iOS API Demo app.
What's next
- To learn how best to implement banner ads in a compliant manner, check out Banner ad guidance.
- To learn about full-screen interstitial ads, check out the Interstitial Ads guide.
- To see how you can target ads, check out the Targeting guide.
- To explore more capabilities of banner ads, check out the Ad Events guide.