Native

Before You Start

Before integrating ads unit in your app, you must:

  • In the OpenMediation UI, create an application and create an ad placement. You can follow guides here.
  • Follow our steps to iOS SDK Integration by integrating the OpenMediation SDK into your project.
  • If you use mediation, go to Add Mediation Networks and make sure you have added the ad networks' SDKs and the corresponding adapters you wish to work with in your app.

Native Ad

Native ad is a form of paid media where the ad experience follows the natural form and function of the user experience in which it is placed.

Step 1. Initialize Native Ad

The OpenMediation SDK fires several events to inform you of Native Ad activity. To display Native Ads, you need to create a brand new OMNative object, set up its delegate and load the ads. The following snippet demonstrates how to use the OMNative class to create Native Ad objects and implement the OMNativeDelegate to receive Native Ad events.

Create OMNativeView and OMNative

Add the following code to create a OMNative object and a OMNativeView object, and add your Delegate to register for the events. The SDK will notify the Delegate of all possible events listed in section below.

Note: Here we use "self" which means the callback function is implemented in the current object.

@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)

Step 1. Add the Delegate

The OpenMediation SDK fires several events to inform you of Ad activity. To show Ad to your user, you need to set up its Delegate before loading the ads to receive Ad status. The following snippets demonstrates how to configure Delegate for OMNative object and implement the OMNativeDelegate to receive Ad events.

1. Add the following code to add your Delegate to OMNative object and register to the received events. The SDK will notify the Delegate of all possible events listed in section 2 below.

Note: Here we use "self" which means the callback function is implemented in the current object.

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

2. The methods declared by the OMNativeDelegate protocol allow the adopting delegate to respond to messages from the OMNative class and thus respond to operations such as whether the native ad has been loaded.

/// 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")
}

 

Step 2. Load Native Ad

Invoke loadAd method to request and cache the Native Ad before it is going to be shown to users. It is strongly recommended to invoke this method a short while before the ad is to be shown.

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

Notes: The load method can be called multiple times at any time, but we do not recommend continuous requests within a short period of time. Making many requests in a short period of time makes no sense because the inventory's availability is unlikely to change much over this period. 

Step 3. Show Native Ad

Invoke the following method to serve a Native ad to your users:

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

Note: If you use FacebookAudienceSDK V6.5.0+, please make sure to assign the value of nativeAd when displaying the native ad, otherwise Facebook will easily lose the display and click callback.

Was this article helpful?
3 out of 3 found this helpful

Comments

0 comments

Article is closed for comments.