Skip to content

Native

The native ads are a kind of ad that can be shown using the same visual style like the rest of the app, which allows a natural and non-intrusive integration with the user interface.

Unlike the other formats, the app provides the layout and the SDK fills it with the assets of the ad, no matter which demand source has filled it.

The first step is to create the view that will be used to display the ads. It has to be a WANativeAdView, or a subclass of it, where every asset that the app wants to show is connected to the corresponding outlet.

The easiest way is to create a XIB file, set WANativeAdView as the class of its root view and connect the outlets from the Interface Builder:

OutletTypeDescription
advertiserLabelUILabelName of the advertiser
bodyLabelUILabelDescription of the ad
callToActionButtonUIButtonButton that invites the user to interact
headlineLabelUILabelHeadline of the ad
iconImageViewUIImageViewIcon of the ad
mediaContainerUIViewContainer for the media content (image or video)
optionsContainerUIViewContainer for the options view of the ad
priceLabelUILabelPrice of the promoted product
ratingContainerUIViewContainer for the rating of the promoted product
storeLabelUILabelStore of the promoted product

Every outlet is optional, so you only need to connect the assets that your design displays.

To request a native ad, you need to make an integration by code as shown in the example below:

import UIKit
import WortiseSDK
class ViewController: UIViewController {
private var nativeAd: WANativeAd?
private var nativeAdLoader: WANativeAdLoader?
private var nativeAdView: WANativeAdView?
override func viewDidLoad() {
super.viewDidLoad()
...
let loader = WANativeAdLoader(
adUnitId: "Wortise Ad Unit ID", delegate: self
)
loader.rootViewController = self
nativeAdLoader = loader
loader.loadAd()
}
deinit {
nativeAd?.destroy()
nativeAdLoader?.destroy()
}
}
extension ViewController: WANativeDelegate {
func didLoad(nativeAd: WANativeAd) {
// Invoked when the ad has been loaded
self.nativeAd?.destroy()
self.nativeAd = nativeAd
// Instantiate the ad view and add it to the interface
let adView = Bundle.main.loadNibNamed(
"NativeAdView", owner: nil
)?.first as! WANativeAdView
view.addSubview(adView)
nativeAdView = adView
nativeAdLoader?.render(ad: nativeAd, into: adView)
}
func didClick(nativeAd: WANativeAd) {
// Invoked when the ad has been clicked
}
func didFailToLoad(nativeAd error: WAAdError) {
// Invoked when the ad could not be loaded
// (because of an error or no fill)
}
func didImpress(nativeAd: WANativeAd) {
// Invoked when the ad has generated an impression
}
func didPayRevenue(nativeAd: WANativeAd, data: WARevenueData) {
// Invoked when the ad has generated revenue
}
}

The render(ad:into:) method fills the outlets of the view with the assets of the ad and registers them to track the clicks and the impressions. It returns false when the ad could not be rendered.

The WANativeAd class exposes the assets of the ad, which can be used to customise the view before rendering it:

PropertyTypeDescription
advertiserString?Name of the advertiser
bodyString?Description of the ad
callToActionString?Text of the call to action
headlineString?Headline of the ad
iconImage?Icon of the ad
images[Image]Images of the ad
mediaContentMediaContent?Media content of the ad (image or video)
priceString?Price of the promoted product
ratingDouble?Rating of the promoted product
storeString?Store of the promoted product

The loadAd method also accepts a WARequestParameters instance, in the same way as the rest of the formats:

nativeAdLoader?.loadAd(parameters: WARequestParameters(agent: "your agent"))

If your app already integrated the deprecated WAGoogleNativeAd class, these are the equivalences with the new integration:

DeprecatedReplacement
WAGoogleNativeAd(adUnitId:delegate:)WANativeAdLoader(adUnitId:delegate:)
WAGoogleNativeDelegateWANativeDelegate
load()loadAd() (or loadAd(parameters:))
didLoad(nativeAd:googleNativeAd:)didLoad(nativeAd:) — a WANativeAd, no Google type
didFailToLoad(nativeAd:error:)didFailToLoad(nativeAd:)
didRecord(impression:)didImpress(nativeAd:)
options ([GADAdLoaderOptions])removed — the SDK configures the request
Your own GADNativeAdView and asset wiringWANativeAdView outlets + render(ad:into:)

In practice, the changes are:

  1. Drop the Google types. There is no NativeAd from Google in the callbacks anymore, so the app no longer imports GoogleMobileAds for native ads.
  2. Replace the class of the view. The root view of the layout becomes a WANativeAdView, connecting the outlets from the Ad view section instead of Google’s asset views.
  3. Render through the loader. Where the app used to configure a GADNativeAdView in the load callback, now it calls render(ad:into:).
  4. Media view. Remove the GADMediaView from the layout and connect a plain UIView as mediaContainer; the SDK inserts the right media view for whichever network filled the ad.
func didLoad(nativeAd: WAGoogleNativeAd, googleNativeAd: NativeAd) {
let adView = // your GADNativeAdView
adView.headlineView = headlineLabel
adView.callToActionView = callToActionButton
adView.mediaView = mediaView
// ... one line per asset ...
adView.nativeAd = googleNativeAd
}
func didLoad(nativeAd: WANativeAd) {
nativeAdLoader?.render(ad: nativeAd, into: adView)
}

Apps that still use WAGoogleNativeAd receive Google’s NativeAd instance in the didLoad(nativeAd:googleNativeAd:) callback, and are responsible for building the GADNativeAdView and registering every asset, as described in Google’s documentation.