Menu

Native Ads

1. Integration Recommendations

Type Description Notes Demo Sample Code Location
Self-Render Ad materials returned by third-party ad platforms are assembled by developers. The type selection in the third-party ad platform backend must be consistent with the Taku backend, otherwise it will cause errors. Self-Rendering Ad Integration Notes • Single Self-Render: SelfRenderVC.m
• Feed Stream List: FeedSelfRenderVC.m
Template Render Third-party ad platforms return rendered views that you can directly add to a container for display. • Template ads from different ad platforms have their own ad aspect ratios, which you can view in these ad platform backends.
• Please try to select templates with the same or similar aspect ratios in the ad platform backend, and pass in the corresponding aspect ratio dimensions in the code to load and display ads for the best display effect.
• Single Template Render: ExpressVC.m
• Feed Stream List: FeedTemplateVC.m

2. Loading Ads

The extra dictionary in this step supports passing in relevant parameters for configuring ads and your custom parameters. You can view more information about extra parameters here.

objc Copy
// Import header file
#import <AnyThinkSDK/AnyThinkSDK.h>

//Add protocol <ATNativeADDelegate>

@interface SelfRenderVC () <ATNativeADDelegate>

@property (strong, nonatomic) ATNativeADView  * adView;
@property (strong, nonatomic) SelfRenderView  * selfRenderView;
@property (nonatomic, strong) ATNativeAdOffer * nativeAdOffer;
// Retry attempt counter
@property (nonatomic, assign) NSInteger          retryAttempt;
@end

@implementation SelfRenderVC

//Placement ID
#define Native_SelfRender_PlacementID @"b680af2c216f73"

//Scene ID, optional, can be generated in the backend. Pass empty string if not available
#define Native_SelfRender_SceneID @""
 
#pragma mark - Load Ad
/// Load ad
- (void)loadAd {

    NSMutableDictionary * loadConfigDict = [NSMutableDictionary dictionary];
    
    //Set the size for requesting ads
    [loadConfigDict setValue:[NSValue valueWithCGSize:CGSizeMake(SelfRenderViewWidth, SelfRenderViewHeight)] forKey:kATExtraInfoNativeAdSizeKey];

    [[ATAdManager sharedManager] loadADWithPlacementID:Native_SelfRender_PlacementID extra:loadConfigDict delegate:self];
}

#pragma mark - Placement Delegate Callbacks
/// Placement loading completed
/// - Parameter placementID: Placement ID
- (void)didFinishLoadingADWithPlacementID:(NSString *)placementID { 
    // Reset retry attempts
    self.retryAttempt = 0;
}
 
/// Placement loading failed
/// - Parameters:
///   - placementID: Placement ID
///   - error: Error information
- (void)didFailToLoadADWithPlacementID:(NSString *)placementID error:(NSError *)error { 
    if (self.retryAttempt >= 3) {
       return;
    }
    self.retryAttempt++;
    
    // Calculate delay time: power of 2, maximum 8 seconds
    NSInteger delaySec = pow(2, MIN(3, self.retryAttempt));

    // Delayed retry loading ad
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, delaySec * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
        [self loadAd];
    });
}

/// Received display revenue
/// - Parameters:
///   - placementID: Placement ID
///   - extra: Extra information dictionary
- (void)didRevenueForPlacementID:(NSString *)placementID extra:(NSDictionary *)extra {
}

/// Native ad has been displayed
/// - Parameters:
///   - adView: Ad view object
///   - placementID: Placement ID
///   - extra: Extra information
- (void)didShowNativeAdInAdView:(ATNativeADView *)adView placementID:(NSString *)placementID extra:(NSDictionary *)extra {
}

/// Native ad close button was tapped
/// - Parameters:
///   - adView: Ad view object
///   - placementID: Placement ID
///   - extra: Extra information
- (void)didTapCloseButtonInAdView:(ATNativeADView *)adView placementID:(NSString *)placementID extra:(NSDictionary *)extra {
    // Destroy ad
    [self removeAd];
    // Preload next ad
    [self loadAd];
}

/// Native ad started playing video
/// - Parameters:
///   - adView: Ad view object
///   - placementID: Placement ID
///   - extra: Extra information dictionary
- (void)didStartPlayingVideoInAdView:(ATNativeADView *)adView placementID:(NSString *)placementID extra:(NSDictionary *)extra {
}

/// Native ad video playback ended
/// - Parameters:
///   - adView: Ad view object
///   - placementID: Placement ID
///   - extra: Extra information dictionary
- (void)didEndPlayingVideoInAdView:(ATNativeADView *)adView placementID:(NSString *)placementID extra:(NSDictionary *)extra {
}

/// Native ad was clicked by user
/// - Parameters:
///   - adView: Ad view object
///   - placementID: Placement ID
///   - extra: Extra information dictionary
- (void)didClickNativeAdInAdView:(ATNativeADView *)adView placementID:(NSString *)placementID extra:(NSDictionary *)extra {
}
 
/// Native ad opened or jumped to deep link page
/// - Parameters:
///   - adView: Ad view object
///   - placementID: Placement ID
///   - extra: Extra information
///   - success: Whether successful
- (void)didDeepLinkOrJumpInAdView:(ATNativeADView *)adView placementID:(NSString *)placementID extra:(NSDictionary *)extra result:(BOOL)success {
}
 
/// Native ad entered full-screen video playback, usually automatically jumps to a playback landing page after clicking video mediaView
/// - Parameters:
///   - adView: Ad view object
///   - placementID: Placement ID
///   - extra: Extra information
- (void)didEnterFullScreenVideoInAdView:(ATNativeADView *)adView placementID:(NSString *)placementID extra:(NSDictionary *)extra{
}

/// Native ad exited full-screen video playback
/// - Parameters:
///   - adView: Ad view object
///   - placementID: Placement ID
///   - extra: Extra information
- (void)didExitFullScreenVideoInAdView:(ATNativeADView *)adView placementID:(NSString *)placementID extra:(NSDictionary *)extra {
}
 
/// Native ad exited detail page
/// - Parameters:
///   - adView: Ad view object
///   - placementID: Placement ID
///   - extra: Extra information
- (void)didCloseDetailInAdView:(ATNativeADView *)adView placementID:(NSString *)placementID extra:(NSDictionary *)extra {
}

3. Displaying Ads

Render Type Implementation Steps
Self-Render 1. Assign values to the self-render view based on the obtained nativeAd, create ATNativeADView object, check if there is mediaView, and register click events.
2. Call the loadPrepareInfo method of ATNativePrepareInfo to bind self-render controls, bind controls that need to be displayed, others that don't need to be displayed don't need to be bound.
3. Call the rendererWithConfiguration: selfRenderView: nativeADView: method of ATNativeAdOffer to complete the rendering steps.
Template Render 1. Create ATNativeADView object based on the obtained offer and newly created ATNativeADConfiguration object configuration
2. Call the rendererWithConfiguration: selfRenderView: nativeADView: method of ATNativeAdOffer to complete the rendering steps, pass nil for selfRenderView.

3.1 Self-Render Ad Display

objc Copy
    //Scene statistics function, optional integration
    [[ATAdManager sharedManager] entryNativeScenarioWithPlacementID:Native_SelfRender_PlacementID scene:Native_SelfRender_SceneID];

    //Check if ready
    if (![[ATAdManager sharedManager] nativeAdReadyForPlacementID:Native_SelfRender_PlacementID]) {
        [self loadAd];
        return;
    }
    
    // Initialize config configuration
    ATNativeADConfiguration *config = [[ATNativeADConfiguration alloc] init];
    // Pre-layout for native ads
    config.ADFrame = CGRectMake(0, 0, SelfRenderViewWidth, SelfRenderViewHeight);
    // Pre-layout for video player, recommend to layout again after adding to custom view
    config.mediaViewFrame = CGRectMake(0, 0, SelfRenderViewMediaViewWidth, SelfRenderViewMediaViewHeight);
    //Set delegate
    config.delegate = self;
    config.rootViewController = self;
    //Make ad View container fit the ad
    config.sizeToFit = YES;

    //[Manual Layout Method] Precisely set logo size and position, choose one implementation with the [Masonry Method] below
    config.logoViewFrame = CGRectMake(kScreenW-50-10, SelfRenderViewHeight-50-10, 50, 50);
    
    //Set ad platform logo position preference (some ad platforms cannot be precisely set, then set through the code below, recommend to match the logoViewFrame position above as much as possible)
    [ATAPI sharedInstance].preferredAdLogoPosition = ATAdLogoPositionBottomRightCorner;
    
    // Get offer ad object, consumes one ad cache after retrieval
    ATNativeAdOffer *offer = [[ATAdManager sharedManager] getNativeAdOfferWithPlacementID:Native_SelfRender_PlacementID scene:Native_SelfRender_SceneID];
    NSDictionary *offerInfoDict = [Tools getOfferInfo:offer];
    self.nativeAdOffer = offer;
    
    // Create self-render view, and assign values based on offer information content
    SelfRenderView *selfRenderView = [[SelfRenderView alloc] initWithOffer:offer];
    
    // Create ad nativeADView
    // Get native ad display container view
    ATNativeADView *nativeADView = [[ATNativeADView alloc] initWithConfiguration:config currentOffer:offer placementID:Native_SelfRender_PlacementID];
    
    //Create container array for clickable components
    NSMutableArray *clickableViewArray = [NSMutableArray array];
    
    // Get mediaView, need to add to self-render view manually, must call
    UIView *mediaView = [nativeADView getMediaView];
    if (mediaView) {
        // Assign and layout, the set method here includes layout
        selfRenderView.mediaView = mediaView;
    }
    
    // Set UI controls that need to register click events, it's best not to add the entire parent view of the feed to click events, otherwise clicking the close button might also trigger the feed click event.
    // Close button (dislikeButton) does not need to register click events
    [clickableViewArray addObjectsFromArray:@[selfRenderView.iconImageView,
                                              selfRenderView.titleLabel,
                                              selfRenderView.textLabel,
                                              selfRenderView.ctaLabel,
                                              selfRenderView.mainImageView]];
    
    // Register click events for UI controls
    [nativeADView registerClickableViewArray:clickableViewArray];
    
    //Bind components
    ATNativePrepareInfo *info = [ATNativePrepareInfo loadPrepareInfo:^(ATNativePrepareInfo * prepareInfo) {
        prepareInfo.textLabel = selfRenderView.textLabel;
        prepareInfo.advertiserLabel = selfRenderView.advertiserLabel;
        prepareInfo.titleLabel = selfRenderView.titleLabel;
        prepareInfo.ratingLabel = selfRenderView.ratingLabel;
        prepareInfo.iconImageView = selfRenderView.iconImageView;
        prepareInfo.mainImageView = selfRenderView.mainImageView;
        prepareInfo.logoImageView = selfRenderView.logoImageView;
        prepareInfo.ctaLabel = selfRenderView.ctaLabel;
        prepareInfo.dislikeButton = selfRenderView.dislikeButton;
        prepareInfo.mediaView = selfRenderView.mediaView;
    }];
    [nativeADView prepareWithNativePrepareInfo:info];
    
    //Render ad
    [offer rendererWithConfiguration:config selfRenderView:selfRenderView nativeADView:nativeADView];

    self.adView = nativeADView;
    
    //Display ad
    AdDisplayVC *showVc = [[AdDisplayVC alloc] initWithAdView:nativeADView offer:offer adViewSize:CGSizeMake(SelfRenderViewWidth, SelfRenderViewHeight)];
    [self.navigationController pushViewController:showVc animated:YES];

3.2 Template Ad Display

objc Copy
    //Scene statistics function, optional integration
    [[ATAdManager sharedManager] entryNativeScenarioWithPlacementID:Native_Express_PlacementID scene:Native_Express_SceneID];

    //Check if ready
    if (![[ATAdManager sharedManager] nativeAdReadyForPlacementID:Native_Express_PlacementID]) {
        [self loadAd];
        return;
    }
    
    // Initialize config configuration
    ATNativeADConfiguration *config = [[ATNativeADConfiguration alloc] init];
    // Set size for template ad nativeADView, usually the size set when requesting ads
    config.ADFrame = CGRectMake(0, 0, ExpressAdWidth, ExpressAdHeight);
    //Set delegate
    config.delegate = self;
    //Set display root controller
    config.rootViewController = self;
    // Enable template ad adaptive height, when the actual returned ad size is inconsistent with the size set when requesting ads, the SDK will automatically adjust the nativeADView size to the actual returned ad size.
    config.sizeToFit = YES;

    // Get offer ad object, consumes one ad cache after retrieval
    ATNativeAdOffer *offer = [[ATAdManager sharedManager] getNativeAdOfferWithPlacementID:Native_Express_PlacementID scene:Native_Express_SceneID];
    
    // Create ad nativeADView
    ATNativeADView *nativeADView = [[ATNativeADView alloc] initWithConfiguration:config currentOffer:offer placementID:Native_Express_PlacementID];

    //Render ad
    [offer rendererWithConfiguration:config selfRenderView:nil nativeADView:nativeADView];
 
    self.adView = nativeADView;
    
    //Display ad
    AdDisplayVC *showVc = [[AdDisplayVC alloc] initWithAdView:nativeADView offer:offer adViewSize:CGSizeMake(ExpressAdWidth, ExpressAdHeight)];
    [self.navigationController pushViewController:showVc animated:YES];

4. Destroying Ads

objc Copy
- (void)removeAd {
    if (self.adView && self.adView.superview) {
        [self.adView removeFromSuperview];
    }
    [self.adView destroyNative];
    self.adView = nil;
    // Destroy offer timely for self-render
    [self.selfRenderView destory];
    self.selfRenderView = nil;
}

5. API说明

类名/文件名 简介
ATAdManager 广告的基础操作类,包括广告加载、过滤广告、场景统计、比价工具等功能。
ATAdManager (Native) 针对原生广告的操作拓展,提供广告展示、检查缓存、检查广告是否就绪、场景统计等功能,其中有Extra键的定义。
ATAdLoadingDelegate 广告的基础代理回调声明,包括广告位与广告源级别的加载成功或失败回调,以及竞价广告源的竞价结束与竞价失败回调。
ATNativeADDelegate 针对原生广告类型的代理回调,包括展示、点击和关闭等。
ATSDKGlobalSetting 通用设置类,提供形如清除广告内存中的缓存、自定义流量分组设置、测试模式、设置第三方广告SDK相关信息等功能,还声明了一些通用的属性。
ATNativeADConfiguration 原生广告配置类,用于设置广告的显示尺寸、播放行为、渲染方式等参数。
ATNativeAdOffer 原生广告素材供应类,用于封装单个原生广告的相关信息(如广告内容、网络平台ID、供应信息等)。
ATNativeADView 原生广告视图类,负责原生广告的渲染和交互控制,提供视频播放控制、广告展示管理、点击追踪、特效动画(如摇晃和滑动)等,提供自渲染相关支持。
ATNativePrepareInfo 原生广告准备信息类,用于配置原生广告各个UI组件的引用,包括文本标签、图片视图、媒体容器、交互按钮等,开发者通过这个类来关联自定义UI组件与广告内容。

6. 进阶设置

广告场景:可帮助开发者了解广告场景维度的展示、点击和到达广告场景时的广告Ready率等数据。

预置策略:可通过配置预置策略,提高首次冷启动的广告加载效果。

Previous
Custom Banner Ads
Next
Self-Rendering Ad Integration Notes
Last modified: 2026-07-09Powered by