iosswiftmopub

MoPub for iOS: Interstitial close button not showing


I have a SwiftUI/SpriteKit project in which I'm trying to implement a MoPub interstitial using a test ad id found here.

The ad loads as expected, and a tap on the ad results in success, however there is no close button on the ad.

I've integrated the MoPub SDK via CocoaPods, like this:

pod 'mopub-ios-sdk'

I'm initializing the MoPub SDK in my AppDelegate's application(_:didFinishLaunchingWithOptions:) function, like this:

let sdkConfig = MPMoPubConfiguration(adUnitIdForAppInitialization: "MyIdHere")

sdkConfig.loggingLevel = .debug

MoPub.sharedInstance().initializeSdk(with: sdkConfig, completion: {
  DispatchQueue.main.async {
     //The SDK is initialized.
  }
})

I'm presenting the ad via the showAd function below:

class InterstitialAds: UIViewController, MPAdViewDelegate, MPInterstitialAdControllerDelegate {
    var moPubView: MPAdView?
    
    func viewControllerForPresentingModalView() -> UIViewController! {
        return self
    }
    
    
    func interstitialDidLoadAd(_ interstitial: MPInterstitialAdController) {
        
    }
    func interstitialDidFail(toLoadAd: MPInterstitialAdController, withError: Error){
        
    }
    func interstitialWillDismiss(_ interstitial: MPInterstitialAdController) {
        
    }
    func interstitialDidDismiss(_ interstitial: MPInterstitialAdController) {
        
    }
    func interstitialDidExpire(_ interstitial: MPInterstitialAdController) {
        
    }
    
    
    func showAd() {
        let topViewController = UIApplication.shared.windows.filter {$0.isKeyWindow}.first?.rootViewController
        
        self.modalPresentationStyle = .fullScreen
        
        topViewController?.present(self, animated: true) {
            let adId = "4f117153f5c24fa6a3a92b818a5eb630" //Test ad unit id

            self.moPubView = MPAdView.init(adUnitId: adId)

            let bounds = self.view.bounds
            var adFrame = CGRect.zero
            adFrame.size = CGSize(width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height)

            if let v = self.moPubView {
                v.frame = adFrame
                v.maxAdSize = kMPPresetMaxAdSizeMatchFrame
                v.delegate = self
                self.view.addSubview(v)
                v.loadAd()
            }
        }
    }
    

    override func viewDidLoad() {
        super.viewDidLoad()
    }
    
}

Question: What changes do I need to make to get the close button to show?

Thank you!


Solution

  • I managed to get this working by making a few changes:

    1. Replaced MPAdView with MPInterstitialAdController.
    2. Replaced MPAdView.init(adUnitId: adId) with MPInterstitialAdController(forAdUnitId: adId).
    3. Called show(from:) after calling loadAd()

    The close button now shows up as desired.