请输入
菜单

原生自渲染(UnifiedNative)

原生模版广告接入

1. 原生自渲染广告介绍

原生自渲染广告是在列表页或轮播图上显示的广告类型,媒体可自行渲染广告样式。
注意:主图或视频元素必须显示在广告上

2. 原生自渲染广告集成说明

  1. 原生对象可以重复请求,广告请求成功后,媒体自行存储 AMPSUnifiedNativeManager 对象,如果未存储,重新 loadAd 后,上一个 AMPSUnifiedNativeAd 对象会被移除;
  2. 原生重复拉取过多后。媒体可根据自身情况手动进行移除,防止内存占用过大。
  • 注:调用方法时,尽量在主线程调用,防止异步回到主线程增加请求耗时。

3. 原生自渲染广告API说明

3.1.1 AMPSUnifiedNativeManager 属性说明

属性 说明
adConfiguration 广告配置对象
delegate 广告代理声明
adArray 广告视图数组

3.1.2 AMPSUnifiedNativeManager 方法说明

方法 说明
initWithAdConfiguration: 创建初始化对象adConfiguration 广告配置
loadUnifiedNativeManager 广告请求
removeUnifiedNativeManager 销毁广告对象

3.1.3 AMPSUnifiedNativeManagerDelegate 代理说明

方法 说明
ampsNativeAdLoadSuccess: 原生广告加载成功
ampsNativeAdLoadFail: error: 原生广告加载失败

3.2.1 AMPSUnifiedNativeView 属性说明

属性 说明
viewController 广告点击弹出落地需要使用的控制器
delegate 广告视图代理声明
mediaView 视频广告的媒体View,绑定数据对象后自动生成,可自定义布局
nativeAd 广告数据模型

3.2.2 AMPSUnifiedNativeView 方法说明

方法 说明
refreshData: 刷新广告数据
registerClickableViews: 注册点击事件
unregisterNativeAdDataObject 解绑广告数据源

3.2.3 AMPSUnifiedNativeViewDelegate 代理说明

方法 说明
ampsNativeAdRenderSuccess: 原生广告视图渲染成功
ampsNativeAdRenderFail: error: 原生广告视图渲染失败
ampsNativeAdExposured: 原生广告视图曝光
ampsNativeAdDidClick: 原生广告视图点击
ampsNativeAdDidClose: 原生广告视图关闭

3.2.4 AMPSUnifiedNativeAd 属性

方法 说明
nativeMode 广告类型
title 广告标题
desc 广告描述
iconUrl 应用类广告App 图标Url
imageUrl 广告大图Url
actionText 广告对应的按钮展示文案, 此字段可能为空
adLogoUrl 广告logo url, 此字段可能为空
imageUrls 三小图广告的图片Url集合

4. 原生广告代码示例

广告加载与显示:

objc 复制代码
#import <AdScopeFoundation/AdScopeFoundation.h>
#import <AMPSAdSDK/AMPSAdSDK.h>
#import "AdScopeDemoStatusObject.h"
#import "AdScopeDemoMacro.h"

@interface AMPSADNUnifiedNativeController () <AMPSUnifiedNativeManagerDelegate, AMPSUnifiedNativeViewDelegate>

@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) UILabel *resultsLabel;

@property (nonatomic, strong) AMPSUnifiedNativeManager *nativeManager;
@property (nonatomic, strong) AMPSUnifiedNativeAd *nativeAd;
@property (nonatomic, strong) AMPSUnifiedNativeView *nativeView;

@end

@implementation AMPSADNUnifiedNativeController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.scrollView];
    [self setScrollViewAutoLayout];
    
    UIButton *requestBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [requestBtn setTitleColor:[UIColor adScopeColorWithHexString:@"#000000"] forState:UIControlStateNormal];
    [requestBtn setTitle:@"请求广告" forState:UIControlStateNormal];
    requestBtn.frame = CGRectMake(15, 15, self.view.frame.size.width-30, 40);
    requestBtn.layer.masksToBounds = YES;
    requestBtn.layer.borderWidth = 1;
    requestBtn.layer.borderColor = [UIColor adScopeColorWithHexString:@"999999"].CGColor;
    requestBtn.layer.cornerRadius = 3;
    [requestBtn addTarget:self action:@selector(requestBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.scrollView addSubview:requestBtn];
    
    UIButton *showBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [showBtn setTitleColor:[UIColor adScopeColorWithHexString:@"#000000"] forState:UIControlStateNormal];
    [showBtn setTitle:@"显示广告" forState:UIControlStateNormal];
    showBtn.frame = CGRectMake(requestBtn.adScopeFrameLeft, requestBtn.adScopeFrameBottom+15, requestBtn.adScopeFrameWidth, 40);
    showBtn.layer.masksToBounds = YES;
    showBtn.layer.borderWidth = 1;
    showBtn.layer.borderColor = [UIColor adScopeColorWithHexString:@"999999"].CGColor;
    showBtn.layer.cornerRadius = 3;
    [showBtn addTarget:self action:@selector(showBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.scrollView addSubview:showBtn];
    
    self.resultsLabel = [[UILabel alloc]initWithFrame:CGRectMake(showBtn.adScopeFrameLeft, showBtn.adScopeFrameBottom+15, showBtn.adScopeFrameWidth, 30)];
    self.resultsLabel.textColor = [UIColor redColor];
    self.resultsLabel.textAlignment = NSTextAlignmentCenter;
    self.resultsLabel.text = @"广告请求日志";
    [self.scrollView addSubview:self.resultsLabel];
}

- (void)requestBtnClick {
    AMPSAdConfiguration *cfg = [[AMPSAdConfiguration alloc]init];
    cfg.spaceId = kAdScopeDemoADNSDKUnifiedNativeID;
    cfg.adSize = CGSizeMake(self.view.frame.size.width, 0);
    self.nativeManager = [[AMPSUnifiedNativeManager alloc]initWithAdConfiguration:cfg];
    self.nativeManager.delegate = self;
    [self.nativeManager loadUnifiedNativeManager];
    self.resultsLabel.text = @"请求中。。。";
}

- (void)showBtnClick {
    [self.view endEditing:YES];
    self.nativeView.adScopeFrameTop = self.resultsLabel.adScopeFrameBottom + 50;
    [self.scrollView addSubview:self.nativeView];
}

- (void)ampsNativeAdLoadSuccess:(AMPSUnifiedNativeManager *)nativeManager {
    CookieADNLog(@"AMPS-LoadSuccess");
    if (nativeManager.adArray.count > 0) {
        self.nativeAd = [nativeManager.adArray adScopeSafeObjectAtIndex:0];
        self.nativeView = [[AMPSUnifiedNativeView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 300)];
        self.nativeView.delegate = self;
        self.nativeView.viewController = self;
        self.nativeView.backgroundColor = [UIColor adScopeColorWithHexString:@"#EEEEEE"];
        [self.nativeView refreshData:self.nativeAd];
        UIImageView *adImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.nativeView.adScopeFrameWidth, self.nativeView.adScopeFrameWidth/16*9)];
        NSURL *imageUrl = [NSURL URLWithString:self.nativeAd.imageUrl];
        NSData *imageData = [NSData dataWithContentsOfURL:imageUrl];
        adImageView.image = [UIImage imageWithData:imageData];
        
        [self.nativeView addSubview:adImageView];
        
        UIButton *actionBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [actionBtn setTitleColor:[UIColor adScopeColorWithHexString:@"#000000"] forState:UIControlStateNormal];
        [actionBtn setTitle:@"查看详情" forState:UIControlStateNormal];
        actionBtn.frame = CGRectMake(adImageView.adScopeFrameRight - 115, adImageView.adScopeFrameBottom + 15, 100, 35);
        actionBtn.layer.masksToBounds = YES;
        actionBtn.layer.borderWidth = 1;
        actionBtn.titleLabel.font = [UIFont systemFontOfSize:16.0f];
        actionBtn.layer.borderColor = [UIColor adScopeColorWithHexString:@"000000"].CGColor;
        actionBtn.layer.cornerRadius = 17;
        [self.nativeView addSubview:actionBtn];
        
        UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(adImageView.adScopeFrameLeft + 15, adImageView.adScopeFrameBottom + 15, self.nativeView.adScopeFrameWidth - 200, 35)];
        titleLabel.font = [UIFont systemFontOfSize:18.0f];
        titleLabel.textColor = [UIColor blackColor];
        titleLabel.textAlignment = NSTextAlignmentLeft;
        titleLabel.text = self.nativeAd.title;
        [self.nativeView addSubview:titleLabel];
        
        [self.nativeView registerClickableViews:@[actionBtn]];
        
        UIButton *closeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        closeBtn.backgroundColor = [UIColor adScopeColorWithHexString:@"#30999999"];
        [closeBtn setTitleColor:[UIColor adScopeColorWithHexString:@"#000000"] forState:UIControlStateNormal];
        [closeBtn setTitle:@"✕" forState:UIControlStateNormal];
        closeBtn.frame = CGRectMake(adImageView.adScopeFrameRight - 35, adImageView.adScopeFrameTop + 15, 20, 20);
        closeBtn.layer.masksToBounds = YES;
        closeBtn.layer.borderWidth = 1;
        closeBtn.titleLabel.font = [UIFont systemFontOfSize:14.0f];
        closeBtn.layer.borderColor = [UIColor adScopeColorWithHexString:@"#666666"].CGColor;
        closeBtn.layer.cornerRadius = 10;
        [closeBtn addTarget:self action:@selector(closeBtnClick) forControlEvents:UIControlEventTouchUpInside];
        [self.nativeView addSubview:closeBtn];
    }
}

- (void)closeBtnClick {
    self.resultsLabel.text = @"广告关闭";
    [self.nativeView removeFromSuperview];
    [self.nativeView unregisterNativeAdDataObject];
    self.nativeView = nil;
    self.nativeAd = nil;
}

- (void)ampsNativeAdLoadFail:(AMPSUnifiedNativeManager *)nativeManager error:(NSError *_Nullable)error {
    CookieADNLog(@"AMPS-ampsNativeAdLoadFail:%@", error);
    self.resultsLabel.text = @"请求失败";
}

- (void)ampsNativeAdRenderSuccess:(AMPSUnifiedNativeView *)nativeView {
    CookieADNLog(@"AMPS-ampsNativeAdRenderSuccess");
    self.resultsLabel.text = @"渲染成功";
}

- (void)ampsNativeAdRenderFail:(AMPSUnifiedNativeView *)nativeView error:(NSError *_Nullable)error {
    CookieADNLog(@"AMPS-ampsNativeAdRenderFail:%@", error);
}

- (void)ampsNativeAdExposured:(AMPSUnifiedNativeView *)nativeView {
    CookieADNLog(@"AMPS-ampsNativeAdExposured");
}

- (void)ampsNativeAdDidClick:(AMPSUnifiedNativeView *)nativeView {
    CookieADNLog(@"AMPS-ampsNativeAdDidClick");
}

- (void)ampsNativeAdDidClose:(AMPSUnifiedNativeView *)nativeView {
    CookieADNLog(@"AMPS-ampsNativeAdDidClose");
}

#pragma mark 页面视图
- (UIScrollView *)scrollView {
    if (!_scrollView) {
        _scrollView = [[UIScrollView alloc]init];
        _scrollView.backgroundColor = [UIColor whiteColor];
        _scrollView.scrollEnabled = YES;
        _scrollView.contentSize = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height-kAdScopeDemoNavHeight-30);
        _scrollView.translatesAutoresizingMaskIntoConstraints = NO;
    }
    return _scrollView;
}

- (void)setScrollViewAutoLayout {
    NSDictionary *views = @{@"scrollView":self.scrollView};
    NSArray *hConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[scrollView]-0-|" options:NSLayoutFormatAlignAllCenterY metrics:nil views:views];
    [self.view addConstraints:hConstraints];
    NSArray *vConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[scrollView]-0-|" options:NSLayoutFormatAlignAllCenterX metrics:nil views:views];
    [self.view addConstraints:vConstraints];
}

@end

5. 广告竞价逻辑

objc 复制代码
/**
 本次广告价格,单位分
 */
- (NSInteger)eCPM;

/**
 @pararm winInfo 竞胜信息,字典类型
 AMPS_WIN_PRICE :竞胜价格 (单位: 分),必填
 AMPS_WIN_ADNID :竞胜渠道ID,必填
 AMPS_HIGHRST_LOSS_PRICE :失败渠道中最高价格,必填
 AMPS_EXPECT_PRICE :期望价格,选填
 */
- (void)sendWinNotificationWithInfo:(NSDictionary *)winInfo;

/**
 @pararm lossInfo 竞败信息,字典类型
 AMPS_WIN_PRICE :竞胜价格 (单位: 分),必填
 AMPS_WIN_ADNID :竞胜渠道ID,必填
 AMPS_HIGHRST_LOSS_PRICE :失败渠道中最高价格,必填
 AMPS_LOSS_REASON :失败原因,必填
 AMPS_EXPECT_PRICE :期望价格,选填
 */
- (void)sendLossNotificationWithInfo:(NSDictionary *)lossInfo;

类型相关的枚举请参考:AMPSAdSDKDefines

6. 原生自渲染广告注意事项

  • 调用方法时,尽量在主线程调用,防止异步回到主线程增加请求耗时。
  • 原生自渲染广告 viewController 必须传。
  • 原生自渲染广告调用 refreshData 前必须设置 delegate。
  • 原生自渲染广告主图或视频素材不允许隐藏或不显示。
上一个
原生模版(NativeExpress)
下一个
插屏(Interstitial)
最近修改: 2025-06-13