Skip to content

Native Ads

Native ads allow you to present your ads in a way that blends seamlessly with your existing design, providing users with a unified experience.

Once a native ad loaded, you can access to an ad’s individual assets and then display them according to your UI layout.

The following sections show you how to load and then show a native ad.

Load an ad

To load a native ad, instantiate an CTNativeAd object that corresponds to your ad unit and call its loadAd method

objc
#import <CartySDK/CartySDK.h>

self.nativeAd = [[CTNativeAd alloc] init];
self.nativeAd.placementid = @"<your-placement-id>";
self.nativeAd.rootViewController = self;
self.nativeAd.delegate = self;
[self.nativeAd loadAd];
swift
import CartySDK

nativeAd = CTNativeAd()
nativeAd.placementid = "<your-placement-id>"
nativeAd.rootViewController = self
nativeAd.delegate = self
nativeAd.load()

Register for Callbacks

Implement CTNativeAdDelegate to listen to ad events.

objc
- (void)CTNativeAdDidLoad:(nonnull CTNativeAd *)ad { 
    NSLog(@"Ad loaded");
}

- (void)CTNativeAdLoadFail:(nonnull CTNativeAd *)ad withError:(nonnull NSError *)error { 
    NSLog(@"Ad load fail: %@",error.localizedDescription);
}

- (void)CTNativeAdDidShow:(nonnull CTNativeAd *)ad { 
	NSLog(@"Ad did show");
}

- (void)CTNativeAdShowFail:(nonnull CTNativeAd *)ad withError:(nonnull NSError *)error { 
    NSLog(@"Ad show fail: %@",error.localizedDescription);
}

- (void)CTNativeAdDidClick:(nonnull CTNativeAd *)ad {
    NSLog(@"Ad did click");
}
swift
func ctNativeAdDidLoad(_ ad: CTNativeAd) {
    print("Ad loaded")
}
    
func ctNativeAdLoadFail(_ ad: CTNativeAd, withError error: any Error) {
    print("Ad load fail \(error.localizedDescription)")
}
    
func ctNativeAdDidShow(_ ad: CTNativeAd) {
    print("Ad did show")
}
    
func ctNativeAdShowFail(_ ad: CTNativeAd, withError error: any Error) {
    print("Ad show fail \(error.localizedDescription)")
}
    
func ctNativeAdDidClick(_ ad: CTNativeAd) {
    print("Ad did click")
}

Native ad assets

After the ad loaded, the ad resources can be obtained from the native object.

assetsType
titleNSString
descNSString
ctaTextNSString
iconImageURLNSString
adChoiceViewUIView
mediaViewUIView

Display the ad

To display native ads, after completing your UI layout, you need to call the registerContainer of CTNativeAd to register the ad container and clickable elements.

objc
if(!self.nativeAd.isReady)
{
    return;
}

// native template ad type
if(self.native.isTemplate)
{
    UIView *templateView = self.native.templateView;
    [self.adView addSubview:templateView];
    [self.native registerContainer:templateView withClickableViews:nil];
    return;
}

//set Title
self.nativeView.titleLabel.text = self.nativeAd.title;
//set Desc
self.nativeView.textLabel.text = self.nativeAd.desc;
//set CTA
self.nativeView.ctaLabel.text = self.nativeAd.ctaText;
//add mediaView
[self.nativeView.mainView addSubview:self.nativeAd.mediaView];
//add adChoiceView
[self.nativeView.adChoiceView addSubview:self.nativeAd.adChoiceView];
//set icon image
if(self.nativeAd.iconImageURL){
  dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:self.nativeAd.iconImageURL]];
        dispatch_async(dispatch_get_main_queue(), ^{
            self.nativeView.iconImageView.image = [UIImage imageWithData:data];
        });
    });
}

//show ad
[self.adView addSubview:self.nativeView];
//register nativeView and clickable elements
[self.nativeAd registerContainer:self.nativeView withClickableViews:@[self.nativeView.titleLabel,self.nativeView.iconImageView,self.nativeView.mainImageView,self.nativeView.ctaLabel]];
swift
if(!nativeAd.isReady())
{
    return
}

// native template ad type
if(nativeAd.isTemplate)
{
    adView?.addSubview(nativeAd.templateView!)
    nativeAd.registerContainer(nativeAd.templateView!, withClickableViews: nil)
    return;
}

//set Title
nativeView!.titleLabel!.text = nativeAd.title
//set Desc
nativeView!.textLabel!.text = nativeAd.desc
//set CTA
nativeView!.ctaLabel!.text = nativeAd.ctaText
//add mediaView
nativeView!.mainView!.addSubview(nativeAd.mediaView!)
//add adChoiceView
nativeView!.adChoiceView!.addSubview(nativeAd.adChoiceView!)
//set icon image
if(nativeView!.iconImageView != nil && nativeAd.iconImageURL != nil)
{
    fetchRemoteImage(urlStr: nativeAd.iconImageURL!)
}

//show ad
adView?.addSubview(nativeView!)
//register nativeView and clickable areas
nativeAd.registerContainer(nativeView!, withClickableViews: array)

Other

Mute

Make sure make this call before load

objc
self.nativeAd.isMute = YES;
swift
nativeAd.isMute = true

Client-side bidding

After the ad loads successfully, you can obtain ecpm via the following API.

objc
self.nativeAd.ecpm
swift
nativeAd.ecpm

After winning the ad bid, call bidWin and pass in the second-highest bid from this bid.

objc
[self.nativeAd bidWin:@"second_pirce"];
swift
nativeAd.bidWin("second_pirce");

After an ad is lost, bidWin is called with the highest bid received in the auction.

objc
[self.nativeAd bidLoss:@"top_price"];
swift
nativeAd.bidLoss("top_price")

Examples on GitHub