Mars SDK - Interstitial Ad Unit

Add Interstitial

To load and show interstitial ads, you should initialize, configure, and add the MediafyInterstitialAdUnit object to the application. Call the loadAd() method to fetch the ad. Once it is successfully loaded, invoke the show() method at any appropriate point of the app flow. The SDK will present the full-screen ad.

private var interstitialAdUnit: MediafyInterstitialAdUnit!

private func loadAd() {
    interstitialAdUnit = MediafyInterstitialAdUnit()
        
    // 2. Configure the MediafyInterstitialAdUnit
    interstitialAdUnit.delegate = self
    interstitialAdUnit.adFormats = [.banner]
    interstitialAdUnit.adSizes = [CGSize(width: 320, height: 480)]
    
    // 3. Load the interstitial ad
    interstitialAdUnit.loadAd()
}

Configure Interstitial

Configuration properties for MediafyAdView are:

  • adUnitID - an ad placement identifier generated on the Mars publisher UI.
  • adSizes - desired ad sizes for this placement. It can have many sizes.
  • delegate - subscriber to the ad’s lifecycle events.

Ensure you provide sizes for your ad unit in code or via settings on the publisher's UI. It's a critical property.

If you need to integrate video ads or multiformat ads, you should set the adFormats property to the respective value:        

// Make an ad request for video ad
interstitialAdUnit.adFormats = [.video]

// Make an ad request for both video and banner ads (default behavior)
interstitialAdUnit.adFormats = [.video, .banner]

// Make an ad request for banner ad 
interstitialAdUnit.adFormats = [.banner]

Once the ad is loaded, you can invoke the show() method at any appropriate point of the app flow to present the fullscreen ad. To know when the ad is loaded, you should implement MediafyInterstitialAdUnitDelegate protocol and subscribe to the ad events in its methods.

Manage the Ad State

When the delegate’s method interstitialDidReceiveAd is called, it means that the SDK has loaded the ad. Starting from this point, you can use the show() method to display the full-screen ad.

extension MediafyBannerInterstitialViewController: MediafyInterstitialAdUnitDelegate {
    
    func interstitialDidReceiveAd(_ interstitial: MediafyInterstitialAdUnit) {
        // Called when ad is loaded
        
        // Show the full screen ad
        interstitial.show(from: self)
    }
    
    func interstitial(
        _ interstitial: MediafyInterstitialAdUnit,
        didFailToReceiveAdWithError error: (any Error)?
    ) {
        // Called when Mediafy SDK failed to load ad
        print("Did fail to receive ad with error: \(String(describing: error?.localizedDescription))")
    }
    
    func interstitialWillPresentAd(_ interstitial: MediafyInterstitialAdUnit) {
        // Called when interstitial is about to be presented
    }
    
    func interstitialDidDismissAd(_ interstitial: MediafyInterstitialAdUnit) {
        // Called when interstitial is dismissed
    }
    
    func interstitialDidClickAd(_ interstitial: MediafyInterstitialAdUnit) {
        // Called when interstitial was clicked
    }
    
    func interstitialWillLeaveApplication(_ interstitial: MediafyInterstitialAdUnit) {
        // Called when the application is about to enter the background
    }
}

Contact us today

to enjoy endless opportunities!

Contact Us
+

Contact Us