Wiki
Clone wikiNative Ads Sample / NativeAd AATKit 3 migration (beta)
NativeAd AATKit 3 migration (beta)
Index:
- NativeAd Integration
- Check the sample app
Create a native placement
to create a Native placement call the following:
Instead of:
Objective-C:
[AATKit createNativeAdPlacement:@"NativeAdPlacement" supportsMainImage: YES];
AATKit.createNativeAdPlacement(@"NativeAdPlacement", supportsMainImage: true)
use
Objective-C:
[AATSDK createNativeAdPlacementWithName:placementName supportsMainImage:YES statisticsListener:self];
AATSDK.createNativeAdPlacement(name: "NativeAdPlacement", supportsMainImage: true)
Start loading native ads
Native ads have to be loaded via:
func reloadPlacement(placement: AATPlacement, forceLoad: Bool = false) -> Bool
Example: Instead of:
Objective-C:
[AATKit reloadPlacement:self.nativePlacement];
AATKit.reload(nativePlacement)
use:
Objective-C:
[AATSDK reloadPlacementWithPlacement:self.nativeAdPlacement forceLoad:YES];
AATSDK.reloadPlacement(placement: nativeAdPlacement)
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.
Instead of:
Objective-C:
[AATKit getNativeAdForPlacement:placement]
AATKit.getNativeAd(for: placement)
use:
Objective-C:
[AATSDK getNativeAdWithPlacement:self.nativeAdPlacement];
AATSDK.getNativeAd(placement: nativeAdPlacement)
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 contains graphical elements like a UIImageView, UILabels, and a UIButton. These elements are filled with the native assets at runtime.
native Banner example
Instead of:
Objective-C:
#!objective-c self.nativeTitleLabel.text = [AATKit getNativeAdTitle:ad]; self.nativeBodyLabel.text = [AATKit getNativeAdDescription:ad];
#!swift nativeTitleLabel.text = AATKit.getNativeAdTitle(nativeAd) nativeBodyLabel.text = AATKit.getNativeAdDescription(nativeAd)
use:
Objective-C:
#!objective-c self.nativeTitleLabel.text = [AATSDK getNativeAdTitleWithNativeAd:self.nativeAd]; self.nativeBodyLabel.text = [AATSDK getNativeAdDescriptionWithNativeAd:self.nativeAd];
#!swift nativeTitleLabel.text = AATSDK.getNativeAdTitle(nativeAd: nativeAd) nativeBodyLabel.text = AATKit.getNativeAdDescription(nativeAd)
Instances of UIImages have to created by using the URL provided by the AATKit.
Example:
Instead Of:
Objective-C:
#!objective-c NSString *mainImageURL = [AATKit getNativeAdImageURL:nativeAd]; NSData *mainImageData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:mainImageURL]]; UIImage *mainImage = [UIImage imageWithData:mainImageData];
#!swift let mainImageURL = AATKit.getNativeAdIconURL(nativeAd) 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 }
Use:
Objective-C:
#!objective-c NSString *mainImageURL = [AATSDK getNativeAdImageUrlWithNativeAd:self.nativeAd]; NSData *mainImageData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:mainImageURL]]; UIImage *mainImage = [UIImage imageWithData:mainImageData];
#!swift let mainImageURL = AATSDK.getNativeAdImageUrl(nativeAd: nativeAd) 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.
Instead of:
Objective-C:
#!objective-c [AATKit setViewController:self forNativeAd:nativeAd];
#!swift AATKit.setViewController(self, for: nativeAd)
Use:
Objective-C:
#!objective-c // call this method to notify AATKit that a new controller will appear [AATSDK controllerViewWillAppearWithController:self]; // call this method to notify AATKit that a controller will disappear [AATSDK controllerViewWillDisappearWithController:self];
#!swift // call this method to notify AATKit that a new controller will appear AATSDK.controllerViewWillAppear(controller: self) // call this method to notify AATKit that a controller will disappear AATSDK.controllerViewWillDisappear(controller: self)
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 (sub class 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 static func attachNativeAdToLayout(nativeAd: AATNativeAdData, layout: UIView, mainImageView: UIView?, iconView: UIView?, ctaView: UIView?)
Example:
Instead of:
Objective-C:
#!objective-c [AATKit setTrackingView:adView forNativeAd:nativeAd mainImageView:imageView iconView:iconView];
#!swift AATKit.setTrackingView(adView, for: nativeAd, mainImageView: imageView, iconView: iconView)
Use:
Objective-C:
#!objective-c [AATSDK attachNativeAdToLayoutWithNativeAd:self.nativeAd layout:self.containerView mainImageView:self.mainImageView iconView:self.iconImageView ctaView:self.ctaLabel];
#!swift AATSDK.attachNativeAdToLayout(nativeAd: nativeAd, layout: nativeAdContainerView, mainImageView: self.adMainImageView, iconView: self.adIconImageView, ctaView: self.adCTALabel)
The tracking view also must be removed from the native ad, when the native ad isn't needed anymore or replaced.
#!swift func detachNativeAdFromLayout(nativeAd: AATNativeAdData)
Example:
Instead of:
Objective-C:
#!objective-c [AATKit removeTrackingViewForNativeAd:self.nativeAd];
#!swift AATKit.removeTrackingView(for: nativeAd)
Use:
Objective-C:
#!objective-c [AATSDK detachNativeAdFromLayoutWithNativeAd:self.nativeAd];
#!swift AATSDK.detachNativeAdFromLayout(nativeAd: nativeAd)
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 it's 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:
#!objective-c func reportAdSpaceForPlacement(placement: AATPlacement)
Instead of:
Objective-C:
#!objective-c [AATKit appHasAdSpaceForNativePlacement: nativeAdPlacement];
#!swift AATKit.appHasAdSpace(forNativePlacement: nativeAdPlacement)
Objective-C:
#!objective-c [AATSDK reportAdSpaceForPlacementWithPlacement:self.nativeAdPlacement];
#!swift AATSDK.reportAdSpaceForPlacement(placement: placement)
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 to 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 func getNetwork() -> 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:
Instead of:
Objective-C:
#!objective-c [AATKit getNativeAdNetwork: placement];
#!swift AATKit.getNativeAdNetwork(nativeAd)
Use:
Objective-C:
#!objective-c [self.nativeAd getNetwork];
#!swift nativeAd.getNetwork()
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 isFrequencyCapReachedForPlacement(placement: AATPlacement) -> Bool
Get the amount of native ads that are currently loading
In order monitor how many native ads are currently loading on a given placement the following method can be used:
#!swift func currentlyLoadingNativeAdsOnPlacement(placement: AATPlacement) -> 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 is provided. The rating struct can be retrieved via:
#!swift func getNativeAdRating(nativeAd: AATNativeAdData) -> 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 request via the AATKit header - AATKit.h. The following methods are available:
#!swift func getNativeAd(placement: AATPlacement) -> AATNativeAdData?
#!swift func getNativeAdTitle(nativeAd: AATNativeAdData) -> String?
#!swift func getNativeAdDescription(nativeAd: AATNativeAdData) -> String?
#!swift func getNativeAdImageUrl(nativeAd: AATNativeAdData) -> String?
#!swift func getNativeAdIconUrl(nativeAd: AATNativeAdData) -> String?
#!swift func getNativeAdCallToAction(nativeAd: AATNativeAdData) -> String?
- getNativeAdCallToAction get the call to action string
#!swift func attachNativeAdToLayout(nativeAd: AATNativeAdData, layout: UIView, mainImageView: UIView?, iconView: UIView?, ctaView: UIView?)
#!swift func detachNativeAdFromLayout(nativeAd: AATNativeAdData)
#!swift func getNativeAdBrandingLogo(nativeAd: AATNativeAdData) -> UIView?
#!swift func getNativeAdAdvertiser(nativeAd: AATNativeAdData) -> String?
- returns the name of a certain advertiser. This field is mandatory in certain ad networks, like Flurry.
#!swift func isNativeAdExpired(nativeAd: AATNativeAdData) -> Bool func isNativeAdReady(nativeAd: AATNativeAdData) -> Bool
#!swift func reportAdSpaceForPlacement(placement: AATPlacement)
#!swift func currentlyLoadingNativeAdsOnPlacement(placement: AATPlacement) -> Int
#!swift func getNativeAdRating(nativeAd: AATNativeAdData) -> AATNativeAdRating?
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 method
func createNativeAdPlacement(name: String, supportsMainImage: Bool, statisticsListener: AATStatisticsDelegate? = nil) -> AATPlacement?
for more info about it please refer to AATStatisticsDelegate Guide
SampleApp
Updated