Skip to content

iOS SDK - MREC ads

MREC (medium rectangle) ads are 300×250 ads placed inside your app's content, such as in a list or feed, or on screens where the user pauses, like a level-complete screen. Like banners, they stay visible while users interact with your app.

Before you start, install and initialize the SDK.

1. Get the MREC ad object

Get the ad object for your ad unit ID and set its delegate. The SDK caches one instance per ad unit, so calling getMRECAd again with the same ID returns the same object.

private var mrec: MRECAd?

override func viewDidLoad() {
    super.viewDidLoad()

    mrec = A11.shared.getMRECAd(adUnitID: "adUnitId")
    mrec?.delegate = self
}

2. Implement the delegate

Set the delegate before calling load(on:), so you don't miss any events. When onMRECAdLoaded fires, call show(on:) and add adView to your view hierarchy. show(on:) sizes adView to the ad, so position adView by its frame after every call. The full example shows positionMREC().

extension MyViewController: MRECAdDelegate {
    func onMRECAdLoaded(adInfo: AdInfo) {
        mrec?.show(on: self)
        guard let adView = mrec?.adView else { return }
        if adView.superview == nil { // Add once, not on every refresh
            view.addSubview(adView)
        }
        // Reposition MREC ad
        positionMREC()
    }

    func onMRECAdLoadFailed(error: AdLoadError) {
        print("MREC load failed: \(error)")
    }

    func onMRECAdShowFailed(adInfo: AdInfo?, error: AdShowError) {
        print("MREC show failed: \(error)")
    }

    func onMRECAdShown(adInfo: AdInfo) {}
    func onMRECAdClicked(adInfo: AdInfo) {}
    func onMRECAdWillPresentModalView(adInfo: AdInfo) {}
    func onMRECAdDidDismissModalView(adInfo: AdInfo) {}
    func onMRECAdRevenue(adInfo: AdInfo) {}
}

3. Load the ad

Call load(on:) after setting the delegate, for example at the end of viewDidLoad().

mrec?.load(on: self)

Full example

class MyViewController: UIViewController {

    private var mrec: MRECAd?

    override func viewDidLoad() {
        super.viewDidLoad()

        mrec = A11.shared.getMRECAd(adUnitID: "adUnitId")
        mrec?.delegate = self
        mrec?.load(on: self)
    }

    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        positionMREC() // Keep it centered, for example after rotation
    }

    // Center on screen, using the size set by show(on:)
    func positionMREC() {
        mrec?.adView.center = CGPoint(x: view.bounds.midX, y: view.bounds.midY)
    }
}

extension MyViewController: MRECAdDelegate {
    func onMRECAdLoaded(adInfo: AdInfo) {
        mrec?.show(on: self)
        if let adView = mrec?.adView, adView.superview == nil { // Add once, not on every refresh
            view.addSubview(adView)
        }
        positionMREC()
    }

    func onMRECAdLoadFailed(error: AdLoadError) {
        print("MREC load failed: \(error)")
    }

    func onMRECAdShowFailed(adInfo: AdInfo?, error: AdShowError) {
        print("MREC show failed: \(error)")
    }

    func onMRECAdShown(adInfo: AdInfo) {}
    func onMRECAdClicked(adInfo: AdInfo) {}
    func onMRECAdWillPresentModalView(adInfo: AdInfo) {}
    func onMRECAdDidDismissModalView(adInfo: AdInfo) {}
    func onMRECAdRevenue(adInfo: AdInfo) {}
}

MREC refresh

Caution

MDN11 currently does not support auto-refresh, so you need to load and show new MRECs manually. Refresh only while the MREC is on screen. Stop refreshing when the app goes to the background or the user moves to a screen without the MREC, so you don't request ads nobody sees. If your current mediation platform (for example MAX, LevelPlay or AdMob) has auto-refresh turned on for MRECs, you can't use MDN11 for MRECs yet. Call that platform's SDK directly instead.