Wiki
Clone wikiNative Ads Sample / Home
NativeAd Integration
- NativeAd Integration
- AATKit 2
- Create a native placement
- Start loading native ads
- Retrieve the native ad
- (after callback [AATKit haveAd:])
- Retrieve the native assets from the AATKit
- native In-Feed example
- native Banner example
- Displaying an ad and Tracking view and ViewController
- Ad space counting
- Additional information
- Retrieve the ad network that serves the native ad
- Sizes of the main image and icon
- Manual and automatic loading of a native ad
- Frequency capping of native ads
- Get the number of native ads that are currently loading
- Get the app rating if a native ad advertises an app
- Native Ad assets accessor methods
- AATNativeAdData
- AATNativeAdPlacement
- Listening to statistics events
- SampleApp
AATKit 2
Create a native placement
to create a Native placement call the following:
Objective-C:
[AATSDK createNativeAdPlacementWithName:@"NativeAdPlacement" supportsMainImage:YES];
AATSDK.createNativeAdPlacement(name: "NativeAdPlacement", supportsMainImage: true)
Start loading native ads
Native ads have to be loaded via:
func reload() -> Bool
Example:
Objective-C:
[self.nativeAdPlacement reload];
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];
placement.getNativeAd()
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
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.
native Banner example
Objective-C:
#!objective-c self.nativeTitleLabel.text = self.nativeAd.title; self.nativeBodyLabel.text = self.nativeAd.adDescription;
#!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 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:
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 // 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 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 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
Objective-C:
#!objective-c [self.nativeAdPlacement reportAdSpace];
#!swift nativePlacement.reportAdSpace()
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 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
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
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?
#!swift var adDescription: String?
#!swift var imageUrl: String?
#!swift var iconUrl: String?
#!swift var callToAction: String?
- callToAction get the call to action string
#!swift func attachToView(_ view: UIView, mainImageView: UIView?, iconView: UIView?, ctaView: UIView?)
#!swift func detachFromLayout()
#!swift var brandingLogo: UIView?
#!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
#!swift var rating: AATNativeAdRating?
AATNativeAdPlacement
#!swift func reportAdSpace() -> Bool
#!swift func getNativeAd() -> AATNativeAdData?
#!swift func getNumberOfCurrentlyLoadingNativeAds() -> Int
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