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 |
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.
// Import header file
#import <AnyThinkNative/AnyThinkNative.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 {
// We recommend using exponential backoff retry with a maximum delay of 8 seconds or maximum retry attempts of 3
if (self.retryAttempt >= 3) {
return;
}
self.retryAttempt++;
// Calculate delay time (exponential backoff algorithm)
NSInteger delaySec = pow(2, MIN(3, self.retryAttempt));
// Delayed retry
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 {
}
- Note: Before displaying ads, it is recommended to first understand Self-Rendering Ad Integration Notes to avoid display and click anomalies.
- Scene reach rate statistics, displayed in the backend's Data Reports -> Funnel Analysis Report -> Reach Ad Scene, called before 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. |
//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];
//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];
- (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;
}