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.native = [[OMNative alloc]initWithPlacementID:@"YOUR_PLACEMENT_ID"];
    self.native.delegate = self;
    [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 = {
let native = OMNative(placementID: "YOUR_PLACEMENT_ID")
native.delegate = self
self.view.addSubview(nativeView)
return native
}()

 

Native Delegate (OMNativeDelegate)

Implement the following callback function in your code to process ad logic when receiving the activity of your ad. For example invoke show function to serve Ad to your users when the Native Ad is ready. 

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.

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

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
Was this article helpful?
3 out of 3 found this helpful

Comments

0 comments

Article is closed for comments.