Native Ads

Native ads are ad assets that are presented to users via UI components that are native to the platform. They're shown using the same classes you already use in your storyboards, and can be formatted to match your app's visual design. When a native ad loads, your app receives an ad object that contains its assets, and the app (rather than the SDK) is then responsible for displaying them. This differs from other ad formats, which don't allow you to customize the appearance of the ad.

This guide will show you how to use the Google Mobile Ads SDK to implement native ads in an iOS application, as well as some important things to consider along the way.

Broadly speaking, there two parts to succefully implementing native ads: loading an ad via the SDK and then displaying the ad content in your app. This page is concerned with using the SDK to load an ad.

Prerequisites

This guide assumes some working knowledge of the Google Mobile Ads SDK, so if you haven't already, consider running through our Get Started guide.

Always test with test ads

Before you begin, remember that when building and testing your apps, you should be sure that you're using test ads rather than live, production ads. Testing with production ads could lead to suspension of your account.

The easiest way to load test ads is to use our dedicated test ad unit ID for all native advanced ads on iOS:

/6499/example/native

It's been specially configured to return test ads for every request, and you're free to use it in your own apps while coding, testing, and debugging. Just make sure you replace it with your own ad unit ID before publishing your app.

For more information about how the Mobile Ads SDK's test ads work, see Test Ads.

Loading ads

There are two system-defined formats for native ads: app install and content.

Both types of ads are represented by one class: GADUnifiedNativeAd. An instance of this class contains the assets for the native ad. Note that depending on the type of ad represented by the GADUnifiedNativeAd, some fields will not be populated (i.e., they will be nil).

Native ads are loaded via GADAdLoader objects, which send messages to their delegates according to the GADAdLoaderDelegate protocol.

In addition to the system-defined native formats, Google Ad Manager publishers have the option of creating their own native ad formats by defining custom lists of assets. These are called custom native ad formats, and can be used for direct-sold native ads. Custom native ad formats enable publishers to pass arbitrary structured data to their apps. These ads are represented by the GADNativeCustomTemplateAd class.

Custom native ad formats are also referred to as custom template ads because publishers define their own "templates" (lists of asset names and types) for their custom native ad formats. Custom native ads and custom template ads are the same kind of ad.

Initialize the ad loader

Before you can load an ad, you have to initialize the ad loader. The following code demonstrates how to initialize a GADAdLoader:

Swift

adLoader = GADAdLoader(adUnitID: "/6499/example/native",
    rootViewController: self,
    adTypes: [ kGADAdLoaderAdTypeUnifiedNative ],
    options: [ ... ad loader options objects ... ])
adLoader.delegate = self

Objective-C

self.adLoader = [[GADAdLoader alloc]
      initWithAdUnitID:@"/6499/example/native"
    rootViewController:rootViewController
               adTypes:@[ ... ad type constants ... ]
               options:@[ ... ad loader options objects ... ]];
self.adLoader.delegate = self;

You'll need an ad unit ID (you can use the test ID), constants to pass in the adTypes array to specify which native formats you want to request, and any options you wish to set in the options parameter. The list of possible values for the options parameter can be found in the Setting Native Ad Options section .

The adTypes array should contain one or more of the following constants:

Implement the ad loader delegate

The ad loader delegate needs to implement protocols specific your ad type. For unified native ads:

  • GADUnifiedNativeAdLoaderDelegate This protocol includes a message that's sent to the delegate when a unified native ad has loaded:

    Swift

    public func adLoader(_ adLoader: GADAdLoader,
        didReceive nativeAd: GADUnifiedNativeAd)
    

    Objective-C

    - (void)adLoader:(GADAdLoader *)adLoader
        didReceiveNativeAd:(GADUnifiedNativeAd *)nativeAd;
    
  • GADNativeCustomTemplateAdLoaderDelegate. This protocol includes a message that's sent to the delegate when a custom template ad has loaded:

    Swift

    func adLoader(_ adLoader: GADAdLoader,
                    didReceive nativeCustomTemplateAd: GADNativeCustomTemplateAd)
    

    Objective-C

    - (void)adLoader:(GADAdLoader *)adLoader
        didReceiveNativeCustomTemplateAd:(GADNativeCustomTemplateAd *) nativeCustomTemplateAd;
    

Request the ad

Once your GADAdLoader is initialized, call its loadRequest: method to request an ad:

Swift

adLoader.load(DFPRequest())

Objective-C

[self.adLoader loadRequest:[DFPRequest request]];

The loadRequest: method in GADAdLoader accepts the same DFPRequest objects as banners and interstitials. You can use request objects to add targeting information, just as you would with other ad types.

A single GADAdLoader can make multiple requests, but only if they're done one at a time. When reusing a GADAdLoader, make sure you wait for each request to finish before calling loadRequest: again to begin the next. If you need to request multiple ads in parallel, you can always use multiple GADAdLoader objects.

When to request ads

Apps displaying native ads are free to request them in advance of when they'll actually be displayed. In many cases, this is the recommended practice. An app displaying a list of items with native ads mixed in, for example, can load native ads for the whole list, knowing that some will be shown only after the user scrolls the view and some may not be displayed at all.

While prefetching ads is a great technique, it's important that you don't keep old ads around forever without displaying them. Any native ad objects that have been held without display for longer than an hour should be discarded and replaced with new ads from a new request.

Handling failed requests

The above protocols extend the GADAdLoaderDelegate protocol, which defines a message sent when ads fail to load. You can use the GADRequestError object to determine the cause of the error.

Swift

public func adLoader(_ adLoader: GADAdLoader,
    didFailToReceiveAdWithError error: GADRequestError)

Objective-C

- (void)adLoader:(GADAdLoader *)adLoader
    didFailToReceiveAdWithError:(GADRequestError *)error;

Get notified of native ad events

To be notified of events related to the native ad interactions, set the delegate property of the native ad:

Swift

nativeAd.delegate = self

Objective-C

nativeAd.delegate = self;

Then implement GADUnifiedNativeAdDelegate to receive the following delegate calls:

Swift

func nativeAdDidRecordImpression(_ nativeAd: GADUnifiedNativeAd) {
  // The native ad was shown.
}

func nativeAdDidRecordClick(_ nativeAd: GADUnifiedNativeAd) {
  // The native ad was clicked on.
}

func nativeAdWillPresentScreen(_ nativeAd: GADUnifiedNativeAd) {
  // The native ad will present a full screen view.
}

func nativeAdWillDismissScreen(_ nativeAd: GADUnifiedNativeAd) {
  // The native ad will dismiss a full screen view.
}

func nativeAdDidDismissScreen(_ nativeAd: GADUnifiedNativeAd) {
  // The native ad did dismiss a full screen view.
}

func nativeAdWillLeaveApplication(_ nativeAd: GADUnifiedNativeAd) {
  // The native ad will cause the application to become inactive and
  // open a new application.
}

Objective-C

- (void)nativeAdDidRecordImpression:(GADUnifiedNativeAd *)nativeAd {
  // The native ad was shown.
}

- (void)nativeAdDidRecordClick:(GADUnifiedNativeAd *)nativeAd {
  // The native ad was clicked on.
}

- (void)nativeAdWillPresentScreen:(GADUnifiedNativeAd *)nativeAd {
  // The native ad will present a full screen view.
}

- (void)nativeAdWillDismissScreen:(GADUnifiedNativeAd *)nativeAd {
  // The native ad will dismiss a full screen view.
}

- (void)nativeAdDidDismissScreen:(GADUnifiedNativeAd *)nativeAd {
  // The native ad did dismiss a full screen view.
}

- (void)nativeAdWillLeaveApplication:(GADUnifiedNativeAd *)nativeAd {
  // The native ad will cause the application to become inactive and
  // open a new application.
}

Display your ad

Once you have loaded an ad, all that remains is to display it to your users. Head over to our Native Advanced guide to see how.

Send feedback about...

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