AppLovinMax_iOS_MRECs - imobile/app-mediation GitHub Wiki

MRECs

Loading an MREC

To load an MREC ad, create a MAAdView object corresponding to your ad unit and call its loadAd method. To show the ad, add the MAAdView object as a subview of your view hierarchy. Implement MAAdViewAdDelegate so that you are notified when your ad is ready and of other ad-related events.

// Objective-C

#import "ExampleViewController.h"
#import <AppLovinSDK/AppLovinSDK.h>

@interface ExampleViewController()<MAAdViewAdDelegate>
@property (nonatomic, strong) MAAdView *adView;
@end

@implementation ExampleViewController

- (void)createMRECAd
{
    self.adView = [[MAAdView alloc] initWithAdUnitIdentifier: @"YOUR_AD_UNIT_ID" adlanguage: MAAdFormat.mrec];
    self.adView.delegate = self;

    // MREC width and height are 300 and 250 respectively, on iPhone and iPad
    CGFloat width = 300;
    CGFloat height = 250;

    // Center the MREC
    CGFloat x = self.view.center.x - 150;
        
    self.adView.frame = CGRectMake(x, y, width, height);

    // Set background or background color for MREC ads to be fully functional
    self.adView.backgroundColor = BACKGROUND_COLOR;

    [self.view addSubview: self.adView];

    // Load the ad
    [self.adView loadAd];
}

#pragma mark - MAAdDelegate Protocol

- (void)didLoadAd:(MAAd *)ad {}

- (void)didFailToLoadAdForAdUnitIdentifier:(NSString *)adUnitIdentifier withError:(MAError *)error {}

- (void)didClickAd:(MAAd *)ad {}

- (void)didFailToDisplayAd:(MAAd *)ad withError:(MAError *)error {}

#pragma mark - MAAdViewAdDelegate Protocol

- (void)didExpandAd:(MAAd *)ad {}

- (void)didCollapseAd:(MAAd *)ad {}

#pragma mark - Deprecated Callbacks

- (void)didDisplayAd:(MAAd *)ad { /* DO NOT USE - THIS IS RESERVED FOR FULLSCREEN ADS ONLY AND WILL BE REMOVED IN A FUTURE SDK RELEASE */ }
- (void)didHideAd:(MAAd *)ad { /* DO NOT USE - THIS IS RESERVED FOR FULLSCREEN ADS ONLY AND WILL BE REMOVED IN A FUTURE SDK RELEASE */ }

@end
// Swift

class ExampleViewController: UIViewController, MAAdViewAdDelegate
{
    var adView: MAAdView!

    func createMRECAd
    {
        adView = MAAdView(adUnitIdentifier: "YOUR_AD_UNIT_ID", adlanguage: MAAdFormat.mrec)
        adView.delegate = self
    
        // MREC width and height are 300 and 250 respectively, on iPhone and iPad
        let height: CGFloat = 250
        let width: CGFloat = 300
    
        adView.frame = CGRect(x: x, y: y, width: width, height: height)
    
        // Center the MREC
        adView.center.x = view.center.x
    
        // Set background or background color for MREC ads to be fully functional
        adView.backgroundColor = BACKGROUND_COLOR
    
        view.addSubview(adView)
    
        // Load the first ad
        adView.loadAd()
    }

    // MARK: MAAdDelegate Protocol

    func didLoad(_ ad: MAAd) {}

    func didFailToLoadAd(forAdUnitIdentifier adUnitIdentifier: String, withError error: MAError) {}

    func didClick(_ ad: MAAd) {}

    func didFail(toDisplay ad: MAAd, withError error: MAError) {}

    // MARK: MAAdViewAdDelegate Protocol

    func didExpand(_ ad: MAAd) {}

    func didCollapse(_ ad: MAAd) {}

    // MARK: Deprecated Callbacks

    func didDisplay(_ ad: MAAd) { /* DO NOT USE - THIS IS RESERVED FOR FULLSCREEN ADS ONLY AND WILL BE REMOVED IN A FUTURE SDK RELEASE */ }
    func didHide(_ ad: MAAd) { /* DO NOT USE - THIS IS RESERVED FOR FULLSCREEN ADS ONLY AND WILL BE REMOVED IN A FUTURE SDK RELEASE */ }
}

Hiding and Showing an MREC

To hide an MREC ad, add the following code:

// Objective-C

adView.hidden = YES;
[adView stopAutoRefresh];
// Swift

adView.isHidden = true
adView.stopAutoRefresh()

To show an MREC ad, add the following code:

// Objective-C

adView.hidden = NO;
[adView startAutoRefresh];
// Swift

adView.isHidden = false
adView.startAutoRefresh()