Combining Custom-Rendered Native Ad and Banner Ad Requests

With a few changes to your code, you can combine native and banner ads in your ad requests.

Prerequisites

  • Version 7.20.0 or higher of the Google Mobile Ads SDK
  • Complete the Get Started guide

Custom-rendered native ads are loaded via GADAdLoader objects. The GADAdLoader object can also be configured to make ad requests that can result in either a banner or native ad. Adding kGADAdLoaderAdTypeGAMBanner to the adTypes array parameter, along with native ad types such as kGADAdLoaderAdTypeNativeAppInstall and kGADAdLoaderAdTypeNativeContent when creating the GADAdLoader object specifies that banner ads should compete with native ads to fill the request.

Swift

adLoader = GADAdLoader(adUnitID: "/6499/example/nativeandbanner",
    rootViewController: self,
    adTypes: [ …, GADAdLoaderAdType.dfpBanner,... ],
    options: [ ... ad loader options objects ... ])
adLoader.delegate = self

Objective-C

self.adLoader = [[GADAdLoader alloc]
      initWithAdUnitID:@"/6499/example/nativeandbanner"
    rootViewController:rootViewController
               adTypes:@[ …, kGADAdLoaderAdTypeGAMBanner,... ]
               options:@[ ... ad loader options objects ... ]];
self.adLoader.delegate = self;

GAMBannerAdLoaderDelegate

When requesting banner ads via the GADAdLoader, the ad loader delegate must conform to the GAMBannerAdLoaderDelegate protocol. This protocol includes a message that's sent when a banner ad has loaded:

Swift

public func adLoader(_ adLoader: GADAdLoader,
    didReceive GAMBannerView: GAMBannerView)

Objective-C

- (void)adLoader:(GADAdLoader *)adLoader didReceiveGAMBannerView:(GAMBannerView *)bannerView;

The ad loader delegate must also specify which banner ad sizes should be requested by responding to the validBannerSizesForAdLoader message as shown below.

Swift

public func validBannerSizes(for adLoader: GADAdLoader) -> [NSValue] {
  return [NSValueFromGADAdSize(kGADAdSizeBanner),
    NSValueFromGADAdSize(kGADAdSizeMediumRectangle),
    NSValueFromGADAdSize(GADAdSizeFromCGSize(CGSize(width: 120, height: 20)))]
}

Objective-C

- (NSArray *)validBannerSizesForAdLoader:(GADAdLoader *)adLoader {
  return @[
    @(kGADAdSizeBanner),
    @(kGADAdSizeMediumRectangle),
    @(GADAdSizeFromCGSize(CGSizeMake(120, 20)))
  ];
}

GAMBannerViewOptions

Publishers can set specific options for banner behavior by creating a GAMBannerViewOptions object, setting its properties, and passing it in the options array during GADAdLoader initialization.

appEventDelegate
Sets a GADAppEventDelegate for the returned GAMBannerView. App events are messages sent from a creative's JavaScript to the displaying app.
enableManualImpressions
Enables manual impression reporting for Google Ad Manager reservations. Apps using manual impressions can determine for themselves when an impression should be recorded, and do so by calling recordManualImpression as shown below.

Swift

bannerView.recordImpression()

Objective-C

[self.bannerView recordImpression];
adSizeDelegate
Set a GADAdSizeDelegate to be informed of the size of the GAMBannerView returned in a multi-size ad request. The code snippet below demonstrates how to instantiate a GAMBannerViewOptions object and set an adSizeDelegate.

Swift

let bannerViewOptions = GAMBannerViewOptions()
bannerViewOptions.adSizeDelegate = self

Objective-C

GAMBannerViewOptions *bannerViewOptions = [[GAMBannerViewOptions alloc] init];
bannerViewOptions.adSizeDelegate = self;