跳转到主要内容
PRODUCT DOCUMENTS

快速找到所需文档,高效完成接入与排障

浏览产品文档与友盟 Skill,展开目录并阅读正文。

banner广告

简介

banner自渲染广告是对模板渲染广告的补充及升级,可自行定义广告布局样式和展示场景。【渲染方式:自渲染】

主要API

使用说明:通过设置UMUnionNativeAdDelegate,UMUnionNativeAdViewDelegate代理,获取广告相关素材以及点击等回调。

自渲染广告类型说明

typedef NS_ENUM(NSInteger, UMUnionNativeAdType) {
    /**横幅(banner)广告*/
    UMUnionNativeAdTypeDefault = 0
};

UMUnionNativeAd事件回调说明

@protocol UMUnionNativeAdDelegate <NSObject>

/**
 *  广告数据回调
 *  nativeAdDataModel:广告数据
 *  error:错误信息
 */
- (void)nativeAdLoaded:(UMUnionNativeAdDataModel * _Nullable)nativeAdDataModel error:(NSError * _Nullable)error;
@end

在实现上述事件回调之前,请务必先设置delegate:

nad.delegate = self;

UMUnionNativeAd调用接口说明

方法

说明

- (instancetype)initWithSlotId:(NSString *)slotId type:(UMUnionNativeAdType)type;

UMUnionBannerAd实例构造方法,slotId为对应广告位id,type为自渲染广告类型

- (void)loadAd;

广告请求

UMUnionNativeBannerAdView事件回调说明

@protocol UMUnionNativeAdViewDelegate <NSObject>

@optional
/**
 *  广告曝光回调
 *  nativeAdView UMUnionNativeBannerAdView 实例
 */
- (void)nativeAdViewExpose:(UMUnionNativeBannerAdView *)nativeAdView;

/**
 *  广告点击回调
 *  nativeAdView UMUnionNativeBannerAdView 实例
 */
- (void)nativeAdViewDidClick:(UMUnionNativeBannerAdView *)nativeAdView;

/**
 *  广告错误事件回调
 *  error 失败信息
 */
- (void)nativeAdViewWithError:(NSError *)error;

/**
 *  视频广告播放状态回调
 *  nativeAdView UMUnionNativeBannerAdView 实例
 *  status 广告播放状态
 */
- (void)nativeAdView:(UMUnionNativeBannerAdView *)nativeAdView mediaPlayerStatus:(UMUnionMediaPlayerStatus)status;

/**
 *  广告详情页即将打开
 *  nativeAdView UMUnionNativeBannerAdView 实例
*/
- (void)nativeAdViewDetailViewWillPresent:(UMUnionNativeBannerAdView *)nativeAdView;

/**
 *  广告详情页关闭
 *  nativeAdView UMUnionNativeBannerAdView 实例
 */
- (void)nativeAdViewDetailViewClosed:(UMUnionNativeBannerAdView *)nativeAdView;

@end

在实现上述事件回调之前,请务必先设置delegate:

adView.delegate = self;

UMUnionNativeBannerAdView调用接口说明

方法

说明

@property (nonatomic, strong, readonly) UMUnionNativeAdDataModel *dataModel;

绑定的数据对象

@property (nonatomic, strong, readonly) UMUnionMediaView *mediaView;

视频广告View,绑定数据对象后自动生成

- (void)bindDataModel:(UMUnionNativeAdDataModel *)dataModel

clickableViews:(NSArray<UIView *> * __nonnull)clickableViews;

自渲染绑定模型,dataModel:数据对象,必传字段,clickableViews:可点击的视图数组,此数组内的广告元素才可以响应广告对应的点击事件

UMUnionNativeAdDataModel数据对象说明

@interface UMUnionNativeAdDataModel : NSObject

/**
 广告标题
 */
@property (nonatomic, copy, readonly) NSString *title;
/**
 广告内容描述
 */
@property (nonatomic, copy, readonly) NSString *content;
/**
 广告App 图标Url
 */
@property (nonatomic, copy, readonly) NSString *iconUrl;

/**
 iconUrl图片 宽度
 */
@property (nonatomic, readonly) NSInteger iconWidth;

/**
iconUrl图片 高度
 */
@property (nonatomic, readonly) NSInteger iconHeight;

/**
 广告大图Url
 */
@property (nonatomic, copy, readonly) NSString *imageUrl;

/**
 imageUrl图片 宽度
 */
@property (nonatomic, readonly) NSInteger imageWidth;

/**
imageUrl图片 高度
 */
@property (nonatomic, readonly) NSInteger imageHeight;

/**
 广告价格:单位分
*/
@property (nonatomic, strong, readonly) NSNumber *eCPM;

/**
 是否为视频广告
 */
@property (nonatomic, readonly) BOOL isVideoAd;

/**
 video 宽度
 */
@property (nonatomic, readonly) NSInteger videoWidth;

/**
video 高度
 */
@property (nonatomic, readonly) NSInteger videoHeight;
@end

注:eCPM:出价策略有两种:1、PD约定价格,此时该字段返回固定值 -1 ,结算以合同约定的价格为准(可能是CPM固定价,可能是CPM阶梯价,也可能是CPC/CPS等);2、RTB价格,此时该字段返回实时出价,结算以此价格为依据。

接入代码示例

@interface NativeBannerAdViewController ()<UMUnionNativeAdDelegate,UMUnionNativeAdViewDelegate>

@property (nonatomic, strong) UMUnionNativeAd *nativeAd;
@property (nonatomic, strong) UMUnionNativeAdDataModel *model;
@property (nonatomic, strong) UMUnionNativeBannerAdView *nadView;

@end
- (IBAction)onLoadAd:(id)sender {
    UMUnionNativeAd *nad = [[UMUnionNativeAd alloc] initWithSlotId:YOUR_SLOT_ID type:UMUnionNativeAdTypeBigImg];
    nad.delegate = self;
    [nad loadAd];
    self.nativeAd = nad;
}
- (void)nativeAdLoaded:(UMUnionNativeAdDataModel *)nativeAdDataModel error:(NSError *)error{
    if (!error && nativeAdDataModel) {
        self.model = nativeAdDataModel;
    }
}
- (IBAction)onShowAd:(id)sender {
    if (self.model) {
        UMUnionNativeBannerAdView *adView = [[UMUnionNativeBannerAdView alloc] init];
        adView.delegate = self;
        adView.viewController = self;
        adView.bounds = CGRectMake(0, 0, 320, 230);
        adView.center = CGPointMake(self.view.center.x, 350);
        adView.backgroundColor = [UIColor lightGrayColor];
        [self.view addSubview:adView];
        //icon
        UIImageView *icon = [[UIImageView alloc] init];
        icon.frame = CGRectMake(5, 5, 40, 40);
        [icon sd_setImageWithURL:[NSURL URLWithString:self.model.iconUrl]];
        [adView addSubview:icon];
        //title
        UILabel *titleL = [[UILabel alloc] init];
        titleL.frame = CGRectMake(50, 5, adView.bounds.size.width - 55, 20);
        titleL.text = self.model.title;
        [adView addSubview:titleL];
        //content
        UILabel *desL = [[UILabel alloc] init];
        desL.frame = CGRectMake(50, 25, adView.bounds.size.width - 55, 20);
        desL.text = self.model.content;
        [adView addSubview:desL];
        if (self.model.isVideoAd) {
            //视频
            adView.mediaView.frame = CGRectMake(0, 50, adView.bounds.size.width, 180);
            //播放
            UIButton *playB = [[UIButton alloc] init];
            playB.frame = CGRectMake(60, 15, 50, 20);
            [adView addSubview:playB];
            [playB setTitle:@"play" forState:UIControlStateNormal];
            [playB addTarget:self action:@selector(onPlay) forControlEvents:UIControlEventTouchUpInside];
            //暂停
            UIButton *pauseB = [[UIButton alloc] init];
            pauseB.frame = CGRectMake(115, 15, 50, 20);
            [adView addSubview:pauseB];
            [pauseB setTitle:@"pause" forState:UIControlStateNormal];
            [pauseB addTarget:self action:@selector(onPause) forControlEvents:UIControlEventTouchUpInside];
            [adView bindDataModel:self.model clickableViews:@[icon]];
            //静音
            [adView.mediaView muteEnable:YES];
            UISwitch* newsSwitch = [[UISwitch alloc]init];
            [newsSwitch setOn:YES];
            newsSwitch.frame = CGRectMake(170, 15, 50, 20);
            [adView addSubview:newsSwitch];
            [newsSwitch addTarget:self action:@selector(switchChange:)forControlEvents:UIControlEventValueChanged];
        }else{
            //图片
            UIImageView *img = [[UIImageView alloc] init];
            img.frame = CGRectMake(0, 50, adView.bounds.size.width, 180);
            [img sd_setImageWithURL:[NSURL URLWithString:self.model.imageUrl]];
            [adView addSubview:img];
            [adView bindDataModel:self.model clickableViews:@[icon,img]];
        }
        
        UIButton *btnC = [[UIButton alloc] initWithFrame:CGRectMake(adView.frame.size.width - 20, 0, 20, 20)];
        [adView addSubview:btnC];
        [btnC addTarget:self action:@selector(onClose) forControlEvents:UIControlEventTouchUpInside];
        NSBundle *bundle = [NSBundle bundleForClass:[self class]];
        UIImage *cancelImg = [UIImage imageNamed:@"UMUnionSDKResource.bundle/umeng_union_close" inBundle:bundle compatibleWithTraitCollection:nil];
        [btnC setImage:cancelImg forState:UIControlStateNormal];
        self.nadView = adView;
    }
}