Wiki

Clone wiki

Native Ads Sample / Home

NativeAd Integration

AATKit 2

Create a native placement

to create a Native placement call the following:

Objective-C:

[AATSDK createNativeAdPlacementWithName:@"NativeAdPlacement" supportsMainImage:YES];
Swift:
AATSDK.createNativeAdPlacement(name: "NativeAdPlacement", supportsMainImage: true)

Start loading native ads

Native ads have to be loaded via:

func reload() -> Bool
Provide the placement reference for loading one or even multiple native ads. This is different from banner and fullscreen ads, where only one ad could be loaded. Since native ads can occur in large numbers, it's possible to preload and cache multiple native ads via one native placement. Due to the fact that preloading of multiple ads is possible, plus the requested amount may vary a lot, automatic reloading is NOT available. The logic of loading a native ad has to be handled by the programmer.

Example:

Objective-C:

[self.nativeAdPlacement reload];
Swift:
placement.reload()

Retrieve the native ad

(after callback [AATKit haveAd:])

Since multiple native ads can be retrieved from a native placement, the placement can be asked multiple times for a native ad.

Objective-C:

 [self.nativeAdPlacement getNativeAd];
Swift:
placement.getNativeAd()

NativeAdFromPlacement.png

The method returns a native ad, which contains all the assets of a native ad. This object represents a native ad from a certain ad network.

Retrieve the native assets from the AATKit

These so-called native assets can be used to assemble your native ad, according to your app's look and feel. The following image shows an example native ad and the corresponding AATKit methods for retrieving each asset.

native In-Feed example

AATKit_native_ad_interface.png

In order to assemble your native ad, it's common to create one or more xib files that contain graphical elements like a UIImageView, UILabels, and a UIButton. These elements are filled with the native assets at runtime.

Xib_Example.png

native Banner example

AATKit_native_ad_interface_banner.png

Objective-C:

#!objective-c
self.nativeTitleLabel.text = self.nativeAd.title;
self.nativeBodyLabel.text = self.nativeAd.adDescription;
Swift:
#!swift
nativeTitleLabel.text = nativeAd.title
nativeBodyLabel.text = nativeAd.adDescription

Instances of UIImages have to be created by using the URL provided by the AATKit.

Example:

Objective-C:

#!objective-c
NSString *mainImageURL   = self.nativeAd.imageUrl
NSData  *mainImageData   = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:mainImageURL]];
UIImage *mainImage       = [UIImage imageWithData:mainImageData];
Swift:
#!swift
let mainImageURL = nativeAd.imageUrl
guard let urlString = mainImageURL,
    let url = URL(string: urlString) else {
        return
}
guard let data = try? Data(contentsOf: url),
    let image = UIImage(data: data) else {
    return
}
DispatchQueue.main.async {
    imageView.image = image
}

Please note: When using a UITextView instead of a UILabel, make sure that editing is disabled on this element. Otherwise, a click on the UITextView instance might not be recognised by the ad network SDKs. You can do this in your interface builder:

Screen Shot 2016-06-29 at 15.12.35.png


Displaying an ad and Tracking view and ViewController

When assembling your native ad, it's also necessary to provide a tracking view to the native ad and a view controller.

Objective-C:

#!objective-c
  // call this method to notify placement that a new controller will appear
 [AATSDK controllerViewDidAppearWithController:self];

// call this method to notify placement that a controller will disappear
 [AATSDK controllerViewWillDisappear];
Swift:
#!swift
    // call this method to notify placement that a new controller will appear
    AATSDK.controllerViewDidAppear(controller: self)

    // call this method to notify placement that a controller will disappear
    AATSDK.controllerViewWillDisappear()

A view controller is used by the different ad networks in order to show a larger ad, or something alike when a click is registered on the native ad.

Tracking view: A tracking view is a UIView instance that is used by the ad network to track impressions and clicks. This means the instance is checked on runtime whether it's visible on the screen and an impression is counted accordingly. A click is also registered on the instance provided but doesn't have to be a UIButton (subclass of UIView) in order for the click to be registered. It's up to you to decide whether the tracking is big (the whole UIView) or small (a small button). Keep in mind that the ad networks track impressions based on a few conditions being met. A tracking view has to be visible for at least a few seconds on the screen (usually 1-2 seconds), plus a larger portion of the UIView instance has to be visible (usually ~50% of the view has to be visible). Tracking an impression and registering clicks is thus completely dependent on a tracking view being assigned to the native ad that has been provided by the AATKit.

#!Swift
func attachToView(_ view: UIView, mainImageView: UIView?, iconView: UIView?, ctaView: UIView?)

Example:

Objective-C:

#!objective-c
        [self.nativeAd attachToView:self.containerView
                      mainImageView:self.mainImageView
                           iconView:self.iconImageView
                            ctaView:self.ctaLabel];
Swift:
#!swift
        nativeAd.attachToView(nativeAdContainerView,
                              mainImageView: adMainImageView,
                              iconView: adIconImageView,
                              ctaView: adCTALabel)

The tracking view also must be removed from the native ad, when the native ad isn't needed anymore or replaced.

#!swift
func detachFromLayout()

Example:

Objective-C:

#!objective-c
[self.nativeAd detachFromLayout];
Swift:
#!swift
nativeAd.detachFromLayout()


Ad space counting

Since the assembled native is now completely in the hands of the programmer, displaying also has to be done programmatically without involving the AATKit. Through this, the AATKit is NOT able to count ad spaces on its own. Therefore, the programmer has to communicate when a native ad is supposed to be displayed. This desire to show an ad is called an ad space by AddApptr. Communicating and creating an ad space is done via:

func reportAdSpace() -> Bool
Example:

Objective-C:

#!objective-c
[self.nativeAdPlacement reportAdSpace];
Swift:
#!swift
nativePlacement.reportAdSpace()
WARNING If ad spaces are not communicated to the AATKit, the statistics for ad spaces can NOT be shown in the AddApptr dashboard. =================


Additional information

Retrieve the ad network that serves the native ad

In order to comply with the native ad guidelines of various ad networks, it is possible to retrieve the ad network that services a native ad. For this, the following method can be used.

#!swift
var network: AATAdNetwork?

The following is an excerpt of the enumeration 'enum AATAdNetwork' and shows only the ad networks that are currently supported for serving native ads.

#!objective-c
enum AATAdNetwork {
    case ADMOB
    case APPLOVIN
    case APPNEXUS
    case FACEBOOK
   ...
};

Example:

Objective-C:

#!objective-c
self.nativeAd.network
Swift:
#!swift
nativeAd.network

Sizes of the main image and icon

The programmer is supposed to create a UIImage instances, download the native images and get any image dimension through this instance.

Manual and automatic loading of a native ad

Loading a native ad has to be done manually by calling reloadPlacement on the AATKit class. Native ads do NOT support automatic loading. This is due to the fact that it's possible to load multiple native ads via one placement at the same time. This behaviour is new, compared to banner and fullscreen placement which can only hold one ad temporarily. A warning method is logged to the console if the method startAutoReload is called with a native placement on the AATKit.

Frequency capping of native ads

If you want to use frequency capping for native ads, it's necessary to use the method following method in order to ask the AATKit whether the frequency capping is currently reached and act accordingly.

#!swift
 func isFrequencyCapReached() -> Bool
Frequency capping is supposed to limit the display of ads within certain boundaries, like 'per session', or 'once an hour'. Since the programmer can access the native ad before actually displaying it, he can also hold multiple native ads and display them as desired. The AATKit is unable to prevent displaying native ads, which is why the AATKit cannot ensure that the frequency capping is satisfied. In order to provide a solution, the programmer has to ask the AATKit whether any, frequency capping is reached and prevent displaying a native ad accordingly.

Get the number of native ads that are currently loading

In order to monitor how many native ads are currently loading on a given placement the following method can be used:

#!swift
    func getNumberOfCurrentlyLoadingNativeAds() -> Int
This method can be used in order to manage the request interval of native ads from a given placement.

Get the app rating if a native ad advertises an app

Some native ads do advertise apps on the app store. In that case, they might come with a rating you want to display. In order to do that the rating value and scale are provided. The rating struct can be retrieved via:

#!swift
var rating: AATNativeAdRating?

The rating struct:

#!swift
@objc
public class AATNativeAdRating: NSObject {
    let value: Double
    let scale: Double
}

Native Ad assets accessor methods

Native ads and their assets can be requested via the AATNativeAdData. The following methods are available:

AATNativeAdData

#!swift
var title: String?
* Get the title of a native ad

#!swift
var adDescription: String? 
* Get the description body of a native ad

#!swift
var imageUrl: String?
* imageUrl get the URL of the main ad image. Downloading the image have to be handled by the publisher. NOTE: The URL may be an HTTP URL, an HTTPS URL or a local File URL.

#!swift
 var iconUrl: String?
* iconUrl get the URL of the ad icon image. Downloading the image have to be handled by the publisher. NOTE: The URL may be an HTTP URL, an HTTPS URL or a local File URL.

#!swift
var callToAction: String?
  • callToAction get the call to action string

#!swift
func attachToView(_ view: UIView, mainImageView: UIView?, iconView: UIView?, ctaView: UIView?)
* Each native ad has to get a tracking view assigned. This tracking view is necessary to detect impressions and clicks on the native ad. Passing the proper icon and main image views is necessary for native ads from some ad networks to work.

#!swift
func detachFromLayout()
* Remove the tracking view, which is required by some ad networks.

#!swift
var brandingLogo: UIView?
* returns a special logo that is required to be displayed by some ad networks. This logo is supposed to mark the respective native ad as being from a certain ad network.

#!swift
var advertiser: String?
  • returns the name of a certain advertiser. This field is mandatory in certain ad networks, like Flurry.

#!swift
func isExpired() -> Bool
func isReady() -> Bool
* check whether a native ad is ready or expired. Some ad networks do offer distinct methods. If they don't one is the inverse of the other.

#!swift
 var rating: AATNativeAdRating?
* returns the rating struct of a native ad. If no rating struct is available, an empty one is returned.

AATNativeAdPlacement

#!swift
func reportAdSpace() -> Bool
* This method is supposed to be called by the publisher when he intends to display a native ad. The intention of showing an ad is called an adSpace in AddApptr terms. For banner, this is the case when the corresponding load method is called, because loading leads to an inevitable display of this ad. For fullscreen ads, the intention of showing and interstitial is expressed by calling the showPlacement method (fullscreen only). When it comes to native ads, the intention of displaying an ad cannot be derived from the normal workflow of the ad. Even when the publisher prepares an ad, i.e. fill his UI - container with native assets, it doesn't mean that the native ad is going to be displayed. It can mean that, but it doesn't have to, it depends on how the publisher is going to use the native ad. Hence, for native ads, we're in a very different scenario. Since we do want ad spaces to be counted for our statistics, we need some mechanism that allows the AATKit to count ad spaces. For that reason, we decided to offer a method (appHasAdSpaceForNativePlacement) to express and communicate that the app does now have "free" ad space available, which the publisher would like to have filled. Calling this method should be done as part of the normal workflow, independent of whether a native ad is actually available or not. This would express the intent to display an ad and whether the ad space can be filled is the responsibility of the AATKit. The method is currently built in such a way, that it also accepts a reference to a container instance. When a container instance is passed in, the implementation figures out to which placement the container belongs and counts the ad space accordingly.

#!swift
func getNativeAd() -> AATNativeAdData?
* returns the instance of the native ad for this native ad placement.

#!swift
func getNumberOfCurrentlyLoadingNativeAds() -> Int
* returns the amount of native ads that are currently loading on a particular placement.

Listening to statistics events

AATStatisticsDelegate is an optional delegate you can implement if you need to get notified about statistics events. and pass it to placement while creating it using this

nativePlacement.statisticsDelegate = self

for more info about it please refer to AATStatisticsDelegate Guide

SampleApp

Updated