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 later 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 kGADAdLoaderAdTypeDFPBanner 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:@[ …, kGADAdLoaderAdTypeDFPBanner,... ]
               options:@[ ... ad loader options objects ... ]];
self.adLoader.delegate = self;

DFPBannerAdLoaderDelegate

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

Swift

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

Objective-C

- (void)adLoader:(GADAdLoader *)adLoader didReceiveDFPBannerView:(DFPBannerView *)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)))
  ];
}

DFPBannerViewOptions

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

appEventDelegate
Sets a GADAppEventDelegate for the returned DFPBannerView. 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 DFPBannerView returned in a multi-size ad request. The code snippet below demonstrates how to instantiate a DFPBannerViewOptions object and set an adSizeDelegate.

Swift

let bannerViewOptions = DFPBannerViewOptions()
bannerViewOptions.adSizeDelegate = self

Objective-C

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

Send feedback about...

Mobile Ads SDK for iOS
Mobile Ads SDK for iOS
Need help? Visit our support page.