原生广告

前言

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

原生广告

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

步骤 1. 初始化原生广告

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

创建 OMNativeView 和 OMNative

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

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

@property (nonatomic, strong) OMNative *native;
@property (nonatomic, strong) OMNativeAd *nativeAd;
@property (nonatomic, strong) OMNativeView *nativeView;
@property (nonatomic, strong) UIImageView *iconView;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UILabel *bodyLabel;

- (void)viewDidLoad {
    [self.view addSubview:self.nativeView];
}

- (OMNativeView*)nativeView{
     if(!_nativeView){
        _nativeView = [[OMNativeView alloc]initWithFrame:CGRectMake(0,300, self.view.frame.size.width, 300)];
        _nativeView.mediaView = [[OMNativeMediaView 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: OMNativeAd?
private lazy var nativeView: OMNativeView = {
let view = OMNativeView(frame: CGRect(x: 0, y: 300, width: self.view.frame.size.width, height: 300))
view.mediaView = OMNativeMediaView(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: OMNative = {
self.view.addSubview(nativeView)
return native
}()

Native Delegate (OMNativeDelegate)

步骤 1. 添加Delegate

OpenMediation SDK会触发多个事件来通知您广告活动。要向您的用户显示广告,您需要先设置其代表,然后才能加载广告以接收广告状态。以下代码段演示了如何为OMNative 对象配置Delegate 并实现OMNativeDelegate 来接收广告事件。

1.添加以下代码,将您的Delegate添加到OMNative对象,并注册到接收到的事件。SDK会将下面第2节中列出的所有可能的事件通知委托。

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

[[OMNativeManager sharedInstance] addDelegate:self];
OMNativeManager.sharedInstance().add(self)

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

/// Invoked when the ad is available.
/// You can then show the ad.
- (void)omNative:(OMNative*)native didLoad:(OMNativeAd*)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)omNativeDidFailToLoad:(OMNative*)native withError:(NSError*)error{
     NSLog(@"NativeAd Did Fail");
}

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

/// Invoked when the ad finishes playing.
- (void)omNativeDidClick:(OMNative*)native{
     NSLog(@"NativeAd Did Click");
}
/// Invoked when the ad is available.
/// You can then show the ad.
func omNative(_ native: OMNative, didLoad nativeAd: OMNativeAd) {
     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 omNativeDidFail(toLoad native: OMNative, withError error: Error) {
     print("NativeAd Did Fail")
}

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

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

 

步骤 2. 加载原生广告

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

[[OMNativeManager sharedInstance] loadWithPlacementID:"YOUR_PLACEMENT_ID"];
OMNativeManager.sharedInstance().load(withPlacementID:"YOUR_PLACEMENT_ID")

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

步骤 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

注意: 如果您使用FacebookAudienceSDK V6.5.0+,请确保在展示Native广告时进行nativeAd的赋值操作,否则极易出现Facebook丢失展示和点击回调的情况。

这篇文章有帮助吗?
3 人中有 3 人觉得有帮助

评论

0 条评论

文章评论已关闭。