Skip to main content

Native

1. Methods that need to be rewritten

  • Developers need to inherit TradPlusBaseAdapter and rewrite related methods:
    • When developers call the loadAd API of TradPlusAdNative, the loadAdWithWaterfallItem: method of the custom Adapter will be called.
    • When developers call the isAdReady API of TradPlusAdNative, the isReady method of the custom Adapter will be called
  • When developers call showADWithRenderingViewClass:subview:sceneId: or showADWithNativeRenderer:subview:sceneId: API of TradPlusAdNative, the isReady and endRender:clickView: (self-rendering type) methods of the custom Adapter will be called in sequence
MethodParameter DescriptionReturn ValueFunction
- (void)loadAdWithWaterfallItem:(TradPlusAdWaterfallItem *)itemitem: Contains parameters sent by the server and locally configuredvoidUsed to obtain the parameters sent by the server and locally configured to implement the loading logic of custom ads
- (UIView *)endRender:(NSDictionary *)viewInfo clickView:(NSArray *)arrayviewInfo: rendering component dictionary list
clickView: clickable view array
idUsed for self-rendering type native ads registration components, click events, etc.
- (BOOL)isReady-----BoolUsed to determine whether a custom ad is expired before displaying the ad

2.Callback method description

MethodDescription
- (void)AdConfigErrorAd configuration information is wrong
- (void)AdLoadFinshAd loading is complete
- (void)AdLoadFailWithError:(NSError *)errorAd loading fails
error: error message
- (void)AdShowAd is displayed
- (void)AdShowFailWithError:(NSError *)errorAd display fails
error: error message
- (void)AdClickAd is clicked
- (void)AdCloseAd is closed

3. Integration Instructions

  1. Create a custom class that you registered in the TradPlus background and inherit TradPlusBaseAdapter
#import <TradPlusAds/TradPlusBaseAdapter.h>
@interface ClassName : TradPlusBaseAdapter

@end
  1. Implement the initialization, loading and other logic of the custom platform in the loadAdWithWaterfallItem: method
  • Get the background configuration parameters through the TradPlusAdWaterfallItem class, item.config

  • Initialize a custom advertising platform, set the advertising platform parameters and overseas platform privacy settings (such as CCPA, COPPA, GDPR) according to your own needs.

//example
- (void)loadAdWithWaterfallItem:(TradPlusAdWaterfallItem *)item
{
//Get background configuration information through item.config
NSString *placementId = item.config[@"placementId"];
if(placementId == nil)
{
//Configuration Error
[self AdConfigError]
return;
}
//Initialize third-party platforms, set parameters for advertising platforms, privacy settings for overseas platforms, etc.
GADRequest *request = [GADRequest request];
//Setting up GDPR
if (![MSConsentManager sharedManager].canCollectPersonalInfo)
{
GADExtras *extras = [[GADExtras alloc] init];
extras.additionalParameters = @{@"npa": @"1"};
[request registerAdNetworkExtras:extras];
}
//Set other parameters
GADNativeAdImageAdLoaderOptions *nativeAdImageLoaderOptions =
[[GADNativeAdImageAdLoaderOptions alloc] init];
nativeAdImageLoaderOptions.shouldRequestMultipleImages = NO;

GADNativeAdMediaAdLoaderOptions *nativeAdMediaAdLoaderOptions =
[[GADNativeAdMediaAdLoaderOptions alloc] init];
nativeAdMediaAdLoaderOptions.mediaAspectRatio = GADMediaAspectRatioAny;

GADNativeAdViewAdOptions *nativeAdViewAdOptions = [[GADNativeAdViewAdOptions alloc] init];
nativeAdViewAdOptions.preferredAdChoicesPosition = GADAdChoicesPositionTopRightCorner;
self.adLoader =
[[GADAdLoader alloc] initWithAdUnitID:placementId
rootViewController:nil
adTypes:@[ kGADAdLoaderAdTypeNative ]
options:@[ nativeAdImageLoaderOptions,
nativeAdViewAdOptions,
nativeAdMediaAdLoaderOptions
]];
self.adLoader.delegate = self;
[self.adLoader loadRequest:request];
}
  1. Call AdLoadFailWithError: or AdLoadFinsh according to the load result
  • Loading failed
//example
#pragma mark - GADAdLoaderDelegate
- (void)adLoader:(nonnull GADAdLoader *)adLoader
didFailToReceiveAdWithError:(nonnull NSError *)error
{
//Loading failed
[self AdLoadFailWithError:error];
}
  • Loading successful (self-rendering type)
    • Create TradPlusAdRes after successful loading
    • Bind the elements or third-party controls in the self-rendering material to the fields in TradPlusAdRes according to platform requirements
    • Bind TradPlusAdRes to self.waterfallItem.adRes
    • Call AdLoadFinsh to complete the loading process
//example
#pragma mark - GADNativeAdLoaderDelegate
- (void)adLoader:(nonnull GADAdLoader *)adLoader didReceiveNativeAd:(nonnull GADNativeAd *)nativeAd
{
self.nativeAd = nativeAd;
self.nativeAd.delegate = self;
//Create a new TradPlusAdRes
TradPlusAdRes *res = [[TradPlusAdRes alloc] init];
// Bind elements
res.title = self.nativeAd.headline;
res.body = self.nativeAd.body;
res.ctaText = self.nativeAd.callToAction;
if(self.nativeAd.icon != nil)
{
res.iconImage = self.nativeAd.icon.image;
}
self.mediaView = [[GADMediaView alloc] init];
self.mediaView.mediaContent = self.nativeAd.mediaContent;
res.mediaView = self.mediaView;
//Set Res. Used for native interface rendering
self.waterfallItem.adRes = res;
// Loading successfully
[self AdLoadFinsh];
}
  • Loading Successfully (Template Type)
    • Create TradPlusAdRes after loading successfully
    • Bind the adView of TradPlusAdRes to the returned third-party template view
    • Bind TradPlusAdRes to self.waterfallItem.adRes
    • Call AdLoadFinsh to complete the loading process
//example
#pragma mark - MTGNativeAdvancedAdDelegate
- (void)nativeAdvancedAdLoadSuccess:(MTGNativeAdvancedAd *)nativeAd
{
//Create a new TradPlusAdRes
TradPlusAdRes *res = [[TradPlusAdRes alloc] init];
self.adView = [self.advancedAd fetchAdView];
//Set the template view
res.adView = self.adView;
//Set Res
self.waterfallItem.adRes = res;
// Loading successfully
[self AdLoadFinsh];
}
  1. For self-rendering type, you need to register the rendering element in the endRender:clickView: method
  • Register each element according to the third-party platform rules

  • Template types do not need to implement this method

Example 1: According to AdMob's rules, AdMob's GADNativeAdView needs to be used to register and display the ad view.

	//example
- (UIView *)endRender:(NSDictionary *)viewInfo clickView:(NSArray *)array
{
GADNativeAdView *nativeAdView = [[GADNativeAdView alloc] init];
nativeAdView.nativeAd = self.nativeAd;
if([viewInfo valueForKey:kTPRendererAdView])
{
UIView *view = viewInfo[kTPRendererAdView];
nativeAdView.frame = view.bounds;
[nativeAdView addSubview:view];
}
if([viewInfo valueForKey:kTPRendererTitleLable])
{
UIView *view = viewInfo[kTPRendererTitleLable];
nativeAdView.headlineView = view;
}
if([viewInfo valueForKey:kTPRendererTextLable])
{
UIView *view = viewInfo[kTPRendererTextLable];
nativeAdView.bodyView = view;
}
if([viewInfo valueForKey:kTPRendererCtaLabel])
{
UIView *view = viewInfo[kTPRendererCtaLabel];
nativeAdView.callToActionView = view;
}
if([viewInfo valueForKey:kTPRendererIconView])
{
UIView *view = viewInfo[kTPRendererIconView];
nativeAdView.iconView = view;
}
if(self.mediaView != nil)
{
nativeAdView.mediaView = self.mediaView;
}
//Complete the setup and return to AdMob's GADNativeAdView
return nativeAdView;
}

Example 2: Facebook According to Facebook's rules, you only need to register the ad view and clickable view with it.

//example
- (UIView *)endRender:(NSDictionary *)viewInfo clickView:(NSArray *)array
{
UIView *adView = viewInfo[kTPRendererAdView];
[self.nativeAd registerViewForInteraction:adView
mediaView:self.mediaView
iconView:self.iconView
viewController:self.rootViewController
clickableViews:array];
//Complete the registration operation and return nil
return nil;
}
  • Description of commonly used key values in ViewInfo
keyDescription
kTPRendererAdViewAd view
kTPRendererTitleLableAd title
kTPRendererTextLableAd description
kTPRendererCtaLabelAd button
kTPRendererIconViewAd icon
kTPRendererMainImageViewAd main image
kTPRendererMediaViewMediaView
kTPRendererAdChoiceImageViewAdChoice icon
  1. Return whether the ad is expired in the isReady method
//example
- (BOOL)isReady
{
return (self.nativeAd != nil);
}
  1. According to the API of each advertising platform, execute relevant methods to notify developers
//example
#pragma mark - GADNativeAdDelegate
- (void)nativeAdDidRecordImpression:(nonnull GADNativeAd *)nativeAd;
{
//Ad display
[self AdShow];
}

- (void)nativeAdDidRecordClick:(nonnull GADNativeAd *)nativeAd
{
//Ad click
[self AdClick];
}

4.Others

Code Sample

How to obtain the overseas privacy permission setting parameters in TradPlusSDK

#import <TradPlusAds/MsCommon.h>
#import <TradPlusAds/MSConsentManager.h>
MethodDescription
[[NSUserDefaults standardUserDefaults] integerForKey:gTPCOPPAStorageKey]COPPA, Children's Online Privacy Protection Act
0=not set, 1=adult, 2=child
[[NSUserDefaults standardUserDefaults] integerForKey:gTPCCPAStorageKey]CCPA, California Consumer Privacy Act
0=not set, 1=do not report data, 2=report data
[MSConsentManager sharedManager].canCollectPersonalInfoGDPR, General Data Protection Regulation of the European Union (EU) and the European Economic Area (EEA)
yes=allow access to device data no=do not allow access to device data