RichMedia

Default Loading

Rich media ads support VAST ad formats. BDMRichMediaView works this BDMRichMediaRequest.
You can add rich media to view hierarchy at any moment of its lifecycle, but check that media view always has a reference to the correct view controller! If you want to pass media view through several view controllers, you should pass the actual view controller instance to media view anytime. If in some cases media view doesn't have a direct reference to view controller, it uses the top presented view controller in the app.

Rich media supports XCode interface builder system. You can add it to controller and associate view controller in storyboard with NIB file.

You can call -populateWithRequest: method with rich media request object as an argument.

You can also handle rich media events. In this case, some of your classes should implement BDMRichMediaDelegate protocol. An instance of this class can be passed to rich media view at any time of a rich media lifecycle.

import UIKit
import BidMachine

class RichMediaController: UIViewController {

    private var richMedia: BDMRichMediaView?
    
    func load() {
        let richMedia = BDMRichMediaView(frame: CGRect(x: 0, y: 0, width: 300, height: 250))
        let request = BDMRichMediaRequest()
        
        richMedia.delegate = self
        richMedia.populate(with: request)
        
        self.richMedia = richMedia
    }
    
    func present() {
        guard let richMedia = self.richMedia, richMedia.canShow else {
            return
        }
        
        self.view.addSubview(richMedia)
    }
}

extension RichMediaController: BDMRichMediaDelegate {
    
    func mediaViewReady(toPresent mediaView: BDMRichMediaView) {
        
    }
    
    func mediaView(_ mediaView: BDMRichMediaView, failedWithError error: Error) {
        
    }
    
    func mediaViewRecieveUserInteraction(_ mediaView: BDMRichMediaView) {
        
    }
}
#import "RichMediaController.h"
#import <BidMachine/BidMachine.h>

@interface RichMediaController ()<BDMRichMediaDelegate>

@property (nonatomic, strong) BDMRichMediaView *richMedia;

@end

@implementation RichMediaController

- (void)load {
    
    BDMRichMediaRequest *request = [BDMRichMediaRequest new];
    BDMRichMediaView *richMedia = [[BDMRichMediaView alloc] initWithFrame:(CGRect){.size = CGSizeMake(300, 250)}];
    
    richMedia.delegate = self;
    [richMedia populateWithRequest:request];
    
    self.richMedia = richMedia;
}

- (void)show {
    if ([self.richMedia canShow]) {
        [self.view addSubview:self.richMedia];
    }
}

#pragma mark - BDMRichMediaDelegate

- (void)mediaView:(nonnull BDMRichMediaView *)mediaView failedWithError:(nonnull NSError *)error {
    
}

- (void)mediaViewReadyToPresent:(nonnull BDMRichMediaView *)mediaView {
    
}

- (void)mediaViewRecieveUserInteraction:(nonnull BDMRichMediaView *)mediaView {
    
}

@end

Advanced Loading

You can create BDMRichMediaRequest and perform a request by calling the -performWithDelegate: method. Delegate can be nil. When you receive callback on "auction success", you can create (also you can defer with action) BDMRichMediaView instance and it should prepare creative for rendering.

🚧

Avoid multiple calling of -populateWithRequest: method on media view object. If you want to refresh the creative, the best practice is just to create a new media view instance.

import UIKit
import BidMachine

class AdvancedRichMediaController: UIViewController {

    private var richMedia: BDMRichMediaView?
    private var request: BDMRichMediaRequest?
    
    func load() {
        let request = BDMRichMediaRequest()
        request.perform(with: self)
        
        self.request = request
    }
    
    func present() {
        guard let richMedia = self.richMedia, richMedia.canShow else {
            return
        }
        
        self.view.addSubview(richMedia)
    }
}

extension AdvancedRichMediaController: BDMRequestDelegate {
    
    func requestDidExpire(_ request: BDMRequest) {
        
    }
    
    func request(_ request: BDMRequest, failedWithError error: Error) {
        
    }
    
    func request(_ request: BDMRequest, completeWith info: BDMAuctionInfo) {
        guard
            let request = request as? BDMRichMediaRequest
        else { return }
        
        let richMedia = BDMRichMediaView(frame: CGRect(x: 0, y: 0, width: 300, height: 250))
        richMedia.delegate = self
        richMedia.populate(with: request)
        
        self.richMedia = richMedia
    }
}

extension AdvancedRichMediaController: BDMRichMediaDelegate {
    
    func mediaViewReady(toPresent mediaView: BDMRichMediaView) {
        
    }
    
    func mediaView(_ mediaView: BDMRichMediaView, failedWithError error: Error) {
        
    }
    
    func mediaViewRecieveUserInteraction(_ mediaView: BDMRichMediaView) {
        
    }
}
#import "AdvancedRichMediaController.h"
#import <BidMachine/BidMachine.h>

@interface AdvancedRichMediaController ()<BDMRequestDelegate, BDMRichMediaDelegate>

@property (nonatomic, strong) BDMRichMediaView *richMedia;
@property (nonatomic, strong) BDMRichMediaRequest *request;

@end

@implementation AdvancedRichMediaController

- (void)load {
    BDMRichMediaRequest *request = [BDMRichMediaRequest new];
    [request performWithDelegate:self];
    
    self.request = request;
}

- (void)show {
    if ([self.richMedia canShow]) {
        [self.view addSubview:self.richMedia];
    }
}

#pragma mark - BDMRequestDelegate

- (void)request:(nonnull BDMRequest *)request failedWithError:(nonnull NSError *)error {
    
}

- (void)request:(nonnull BDMRequest *)request completeWithInfo:(nonnull BDMAuctionInfo *)info {
    if (![request isKindOfClass:BDMRichMediaRequest.class]) {
        return;
    }
    
    self.richMedia = [[BDMRichMediaView alloc] initWithFrame:(CGRect){.size = CGSizeMake(300, 250)}];
    self.richMedia.delegate = self;
    [self.richMedia populateWithRequest:(BDMRichMediaRequest *)request];
}

- (void)requestDidExpire:(nonnull BDMRequest *)request {
    
}

#pragma mark - BDMRichMediaDelegate

- (void)mediaView:(nonnull BDMRichMediaView *)mediaView failedWithError:(nonnull NSError *)error {
    
}

- (void)mediaViewReadyToPresent:(nonnull BDMRichMediaView *)mediaView {
    
}

- (void)mediaViewRecieveUserInteraction:(nonnull BDMRichMediaView *)mediaView {
    
}

@end

Delegated Methods

BDMRequestDelegate

extension YourViewController: BDMRequestDelegate {
    func request(_ request: BDMRequest, failedWithError error: Error) {
      /// Called in case auction was unsuccessful or some error
      /// occurs due to auction
    }
     
    func request(_ request: BDMRequest, completeWith info: BDMAuctionInfo) {
      /// Called when auction is successfully finished. BDMAuctionInfo is object
      /// that contains info about win bid
      /// You can populate ad object in this method, to make ad object prepare creative
    }
     
    func requestDidExpire(_ request: BDMRequest) {
        /// Called in case if loaded ad was expired
    }
}
#pragma mark - BDMRequestDelegate
 
- (void)request:(BDMRequest *)request failedWithError:(NSError *)error {
  /// Called in case auction was unsuccessful or some error
  /// occurs due to auction
}
     
- (void)request:(BDMRequest *)request completeWithInfo:(BDMAuctionInfo *)info {
  /// Called when auction is successfully finished. BDMAuctionInfo is object
  /// that contains info about win bid
  /// You can populate ad object in this method, to make ad object prepare creative
}
 
- (void)requestDidExpire:(BDMRequest *)request {
  /// Called in case if loaded ad was expired
}

BDMRichMediaDelegate

extension MediaVC: BDMRichMediaDelegate {
    func mediaViewReady(toPresent mediaView: BDMRichMediaView) {
        /// Called in case media view ready to present
        /// creative
    }
     
    func mediaViewRecieveUserInteraction(_ mediaView: BDMRichMediaView) {
        /// Called when media view handle touch event. After this callback BidMachine try to
        /// open product link in StoreKit, Safari view controller or
        /// external browser
    }
 
    func mediaView(_ mediaView: BDMRichMediaView, failedWithError error: Error) {
        /// Called in case media view failed to prepare
        /// creative
    }
}
#pragma mark - BDMRichMediaDelegate
 
- (void)mediaView:(BDMRichMediaView *)mediaView failedWithError:(NSError *)error {
    /// Called in case media view failed to prepare
    /// creative
}
 
- (void)mediaViewReadyToPresentAd:(BDMRichMediaView *)mediaView {
    /// Called in case media view ready to present
    /// creative
}
 
- (void)mediaViewRecieveUserInteraction:(BDMRichMediaView *)mediaView {
    /// Called when media view handle touch event. After this callback BidMachine try to
    /// open product link in StoreKit, Safari view controller or
    /// external browser
}

BDMAdEventProducerDelegate

extension YourViewController {

     func updateDelegate() {

         self.richMedia.producerDelegate = self
     }
}

extension YourViewController: BDMAdEventProducerDelegate {

     func didProduceImpression(_ producer: BDMAdEventProducer) {
        /// Called when impression event was triggered
        /// Called 1 time per ad
    }
    
    func didProduceUserAction(_ producer: BDMAdEventProducer) {
        /// Called when click event was triggered
        /// Called 1 time per ad
    }
}
#pragma mark - BDMAdEventProducerDelegate

self.richMedia.producerDelegate = self

- (void)didProduceImpression:(nonnull id<BDMAdEventProducer>)producer {
    /// Called when impression event was triggered
    /// Called 1 time per ad
}

- (void)didProduceUserAction:(nonnull id<BDMAdEventProducer>)producer {
    /// Called when click event was triggered
    /// Called 1 time per ad
}

Display control

To control playback use the methods

/// Call this method to pause media view
- (void)pause;
/// Call this method to resume media view
- (void)resume;
/// Call this method to mute media view
- (void)mute;
/// Call this method to unmute media view
- (void)unmute;