You are here: Developers > OpenX Mobile iOS SDK > Banner ad integration

Banner integration

Last updated on September 12, 2017

Note: If you are using Interface Builder to assist with your ad integration, set up OXMAdView in Interface Builder before following the steps below.

Adding a banner ad to your app

  1. In your view controller, import the SDK.

  2. Conform your view controller to the OXMAdViewDelegate protocol.

  3. Create a variable to reference OXMAdView and instantiate it.

  4. In your view controller, set parameters in the viewDidLoad method:

    1. Set the adUnitID.
    2. Set the domain.
    3. Assign the delegate and set it to self.
    4. (Recommended) Enrich the request by setting values such as userGender, userAnnualIncomeInUS, and userAge on the userParameters property.
    5. (Optional) Set the backgroundColor of the OMXAdView.
  5. Call load() on the OXMAdView.
  6. Implement the OXMAdViewDelegate methods.
  7. If you want to use flex ads to allow multiple ad sizes for an ad unitThe smallest inventory component that represents the space on a site where ads display., see Flex ads.

Swift code sample


import OpenXSDKCore
				
class MyViewController : UIViewController, OXMAdViewDelegate {
 
    var oxmAdView = OXMAdView()
				
    override func viewDidLoad() {
        self.oxmAdView.adUnitId = "123456789"
        self.oxmAdView.domain = "pub-d.openx.net"
        self.oxmAdView.delegate = self
        self.oxmAdView.flexAdSize = OXMFlexAdSize.Banner_320x50   
        self.view.addSubview(oxmAdView);
        self.oxmAdView.load()
    }
 
    //MARK: OXMAdViewDelegate
    // Called every time an ad has loaded and is ready for display.
    // If you experience an ad quality issue, you can identify the
    // ad by the transactionId in the adDetails object.			
    func adDidLoad(_ adView:OXMAdView, adDetails:OXMAdDetails) {
        NSLog("OpenX ad loaded: \(adView) Transaction ID = \(adDetails.transactionId)")
    }
 
    // Called whenever the load process fails to produce a viable ad.
    func adDidFailToLoad(_ adView:OXMAdView, error:NSError) {
        NSLog("adDidFailToLoad: \(adView), error: \(error)")
    }
 
    // Called after an ad has rendered to the device's screen.
    func adDidDisplay(_ adView:OXMAdView) {
        NSLog("adDidDisplay: \(adView)")
    }
 
    // Called once an ad has finished displaying all of its creatives.
    func adDidComplete(_ adView:OXMAdView) {
        NSLog("adDidComplete: \(adView)")
    }
 
    // Called when the user clicks on an ad and a click-through is 
    // about to occur. 
    func adWasClicked(_ adView:OXMAdView) {
        NSLog("adWasClicked: \(adView)")
    }
 
    // Called when the user closes a click-through.
    func adClickthroughDidClose(_ adView:OXMAdView) {
        NSLog("adClickthroughDidClose: \(adView)")
    }
 
 }

Objective-C code sample


// Contents of file: ViewController.h
#import <UIKit/UIKit.h>
#import <OpenXSDKCore/OpenXSDKCore.h>
  
@interface ViewController : UIViewController, OXMAdViewDelegate
  
@property IBOutlet OXMAdView* adView;
  
@end

 

  
// Contents of file: ViewController.m
#import "ViewController.h"
  
@interface ViewController ()
@end
  
@implementation ViewController
  
- (void)viewDidLoad {
    [super viewDidLoad];
    self.adView = [[OXMAdView alloc] init];
    self.adView.adUnitId = @"123456789";
    self.adView.domain = @"pub-d.openx.net";
    self.adView.delegate = self;
    [self.adView load];
}
 
#pragma mark OXMAdViewDelegate
// Called every time an ad has loaded and is ready for display.
// If you experience an ad quality issue, you can identify the ad by the transactionId in the
// adDetails object.
- (void)adDidLoad:(OXMAdView * _Nonnull)adView adDetails:(OXMAdDetails * _Nonnull)adDetails {
    NSLog(@"Openx ad loaded: %@ with transaction id: %@", adView, adDetails.transactionId);
}
 
// Called whenever the load process fails to produce a viable ad.
- (void)adDidFailToLoad:(OXMAdView * _Nonnull)adView error:(NSError * _Nonnull)error {
    NSLog(@"adDidFailToLoad: %@, error: %@", adView, error);
}
 
// Called after an ad has rendered to the device's screen.
- (void)adDidDisplay:(OXMAdView * _Nonnull)adView {
    NSLog(@"adDidDisplay: %@", adView);
}
 
// Called once an ad has finished displaying all of its creatives.
- (void)adDidComplete:(OXMAdView * _Nonnull)adView {
    NSLog(@"adDidComplete: %@", adView);
}
 
// Called when the user clicks on an ad and a click-through is about to occur.
- (void)adWasClicked:(OXMAdView * _Nonnull)adView {
    NSLog(@"adWasClicked: %@", adView);
}
 
// Called when the user closes a click-through.
- (void)adClickthroughDidClose:(OXMAdView * _Nonnull)adView {
    NSLog(@"adClickthroughDidClose: %@", adView);
}
 
@end

Feedback form