原生广告

前言

在将广告单元集成到您的应用之前,您必须:

原生广告

原生广告是一种付费媒体形式,其中广告体验遵循放置它的用户体验的自然形式和功能。

步骤 1. 初始化原生广告

AdTiming SDK会触发多个事件以通知您原生广告的活动。 要显示原生广告,您需要创建一个全新的 AdTimingNative   对象,设置其委托并加载广告。 以下代码段演示了如何使用 AdTimingNative   类创建本地广告对象,以及如何实现 AdTimingNativeDelegate   来接收本地广告事件。

创建 AdTimingNativeView 和 AdTimingNative

添加以下代码以创建 AdTimingNative   对象和 AdTimingNativeView 对象,并添加您的Delegate来注册事件。 SDK将在 下面的部分 中将所有可能发生的事件通知委托 。

注意:  这里我们使用“ self”,这意味着回调函数是在当前对象中实现的。

@property (nonatomic, strong) AdTimingNative *native;
@property (nonatomic, strong) AdTimingNativeAd *nativeAd;
@property (nonatomic, strong) AdTimingNativeView *nativeView;
@property (nonatomic, strong) UIImageView *iconView;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UILabel *bodyLabel;

- (void)viewDidLoad {
    self.native = [[AdTimingNative alloc]initWithPlacementID:@"YOUR_PLACEMENT_ID"];
    self.native.delegate = self;
    [self.view addSubview:self.nativeView];
}

- (AdTimingNativeView*)nativeView{
     if(!_nativeView){
        _nativeView = [[AdTimingNativeView alloc]initWithFrame:CGRectMake(0,300, self.view.frame.size.width, 300)];
        _nativeView.mediaView = [[AdTimingNativeMediaView alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 250)];
        [_nativeView addSubview:_nativeView.mediaView];
        _iconView = [[UIImageView alloc]initWithFrame:CGRectMake(self.view.frame.size.width-40, 0, 40, 40)];
        [_nativeView addSubview:_iconView];
        _titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 270, self.view.frame.size.width, 15)];
        _titleLabel.font = [UIFont systemFontOfSize:13];
        [_nativeView addSubview:_titleLabel];
        _bodyLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 285, self.view.frame.size.width, 15)];
        _bodyLabel.font = [UIFont systemFontOfSize:13];
        [_nativeView addSubview:_bodyLabel];
        _nativeView.hidden = YES;
     }    
   return _nativeView;
}
var nativeAd: AdTimingNativeAd?
private lazy var nativeView: AdTimingNativeView = {
let view = AdTimingNativeView(frame: CGRect(x: 0, y: 300, width: self.view.frame.size.width, height: 300))
view.mediaView = AdTimingNativeMediaView(frame: CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: 300))
view.addSubview(view.mediaView)
view.addSubview(self.iconView)
view.addSubview(self.titleLabel)
view.addSubview(self.bodyLabel)
return view
}()

private lazy var native: AdTimingNative = {
let native = AdTimingNative(placementID: "YOUR_PLACEMENT_ID")
native.delegate = self
self.view.addSubview(nativeView)
return native
}()

Native Delegate (AdTimingNativeDelegate)

在代码中实现以下回调函数,以在收到广告活动时处理广告逻辑。 例如 ,在准备好Natie广告后, 调用   show 函数将广告投放给您的用户。

通过 AdTimingNativeDelegate 协议 声明的方法, 允许采用的委托响应来自 AdTimingNative 类的 消息 ,从而响应诸如是否已加载本机广告之类的操作。

/// Invoked when the ad is available.
/// You can then show the ad.
- (void)adtimingNative:(AdTimingNative*)native didLoad:(AdTimingNativeAd*)nativeAd{
     NSLog(@"NativeAd Did Load");
     self.nativeAd = nativeAd;
}

/// Invoked when the call to load an ad has failed.
/// Parameter error contains the reason for the failure.
- (void)adtimingNativeDidFailToLoad:(AdTimingNative*)native withError:(NSError*)error{
     NSLog(@"NativeAd Did Fail");
}

/// Invoked when the Ad begins to show.
- (void)adtimingNativeWillExposure:(AdTimingNative*)native{
     NSLog(@"NativeAd Will Exposure");
}

/// Invoked when the ad finishes playing.
- (void)adtimingNativeDidClick:(AdTimingNative*)native{
     NSLog(@"NativeAd Did Click");
}
/// Invoked when the ad is available.
/// You can then show the ad.
func adtimingNative(_ native: AdTimingNative, didLoad nativeAd: AdTimingNativeAd) {
     print(@"NativeAd Did Load")
     self.nativeAd = nativeAd
}

/// Invoked when the call to load an ad has failed.
/// Parameter error contains the reason for the failure.
func adtimingNativeDidFail(toLoad native: AdTimingNative, withError: error: Error) {
     print(@"NativeAd Did Fail")
}

/// Invoked when the Ad begins to show.
func adtimingNativeWillExposure(_ native: AdTimingNative) {
     print(@"NativeAd Will Exposure")
}

/// Invoked when the ad finishes playing.<
func adtimingNativeDidClick(_ native: AdTimingNative) {
     print(@"NativeAd Did Click")
}

 

步骤 2. 加载原生广告

调用 loadAd 方法以请求并缓存原生广告,然后再将其展示给用户。  强烈建议您在广告展示之前不久调用此方法。

[self.native loadAd];
self.native.loadAd()

注意: 该加载方法可以在任何时候被调用多次,但我们不建议很短的时间内连续请求。在短时间内发出许多请求毫无意义,因为在此期间库存的可用性不太可能发生很大变化。

步骤 3. 显示原生广告

调用以下方法向用户投放原生广告:

self.nativeView.hidden = NO;
self.nativeView.nativeAd = self.nativeAd;
self.iconView.image =[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:nativeAd.iconUrl]]];
self.titleLabel.text = nativeAd.title;
self.bodyLabel.text = nativeAd.body;
self.nativeView.isHidden = false
self.nativeView.nativeAd = self.nativeAd!
self.iconView.image = UIImage(data: try! Data(contentsOf: URL(string: self.nativeAd?.iconUrl ?? "")!))
self.titleLabel.text = self.nativeAd?.title
self.bodyLabel.text = self.nativeAd?.body
这篇文章有帮助吗?
1 人中有 1 人觉得有帮助

评论

0 条评论

登录写评论。