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

  • Objective-C
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
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.

  • Objective-C
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
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.

  • Objective-C
objc
if(!self.nativeAd.isReady)
{
    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
swift
if(!nativeAd.isReady())
{
    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

  • Objective-C
objc
self.nativeAd.isMute = YES;
  • Swift
swift
nativeAd.isMute = true

Examples on GitHub