How To Integrate Mopub Banner Ads Swift

How To Integrate Mopub Banner Ads Swift

Before integrating interstitial ads into your app, create an ad account on Mopub’s website and download the SDK using Cocoapod.

pod 'mopub-ios-sdk' # Mopub SDK

For Ad adapters such as Facebook, Admob and Pangle mediation add:

pod 'MoPub-FacebookAudienceNetwork-Adapters' # facebook Mopub adapter
pod 'MoPub-AdMob-Adapters' # Admob Mopub adapters
pod 'MoPub-Pangle-Adapters' # Pangle Mopub adapters

For a full of available adapters, please visit here.

Run pod install in your terminal.

Testing Ads

It’s always advised you use a test ad unit when testing your apps. Testing your app using live ads could lead to your account being terminated or suspended. Mopub‘s available test ads:

FormatSizeAd unit ID
Banner320×500ac59b0996d947309c33f59d6676399f
Banner (MRect)300×2502aae44d2ab91424d9850870af33e5af7
Interstitial320×4804f117153f5c24fa6a3a92b818a5eb630
Rewarded Ad320×4808f000bd5e00246de9c789eed39ff6096
Rewarded Playable (MRAID)320×48098c29e015e7346bd9c380b1467b33850
NativeN/A76a3fefaced247959582d2d2df6f4757

Load Ad

The first step in integrating your ad is by initialising it in your App Delegate. To initialise your ads, import Mopub and configure your ad units.

import MoPubSDK // Mopub ad

// Uncomment this line for Adapters
// import MoPub_FacebookAudienceNetwork_Adapters // Facebook adapter
// import MoPub_Pangle_Adapters // Pangle mobile adapters
// import MoPub_AdMob_Adapters // Mopub adapters

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {


func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

   // Mopub ad network
   let sdkConfig = MPMoPubConfiguration(adUnitIdForAppInitialization: "YOUR_AD_UNIT")
   sdkConfig.loggingLevel = .none
   sdkConfig.allowLegitimateInterest = true

   /* Uncomment this line for Adapters
     let pangleConfig: NSMutableDictionary = [
          "app_id": "YOUR_APP_ID"
     ] 
     let facebookConfig: NSMutableDictionary = [
            "native_banner": "true"
     ]
     sdkConfig.additionalNetworks = [PangleAdapterConfiguration.self]
     sdkConfig.additionalNetworks = [AppLovinAdapterConfiguration.self]
     sdkConfig.additionalNetworks = [FacebookAdapterConfiguration.self]
     sdkConfig.mediatedNetworkConfigurations = [
          "PangleAdapterConfiguration": pangleConfig,
          "FacebookAdapterConfiguration": facebookConfig,
        ]
     */
        
     // Mopub Configuration
     MoPub.sharedInstance().grantConsent()
     MoPub.sharedInstance().allowLegitimateInterest = false
     MoPub.sharedInstance().initializeSdk(with: sdkConfig, completion: nil)
  }
}

Display Ad

The next step is to import the Mopub in your View controller and prepare your ad unit for later use. To do so, prepare the following variables:

import MoPubSDK // Mopub

class ViewController: UIViewController {


// For mobile ads
let adView = MPAdView(adUnitId: "YOUR_AD_UNIT")

}

Now it’s time to load your ad. You can load your ad when a user views the view controller for the first time and refresh it using “forceRereshAd()”.

override func viewDidLoad() {
        
   super.viewDidLoad()
   self.becomeFirstResponder()
      
    let regRect = CGRect(x: YOUR_AD_X-LOCATION, y: YOUR_AD_Y-LOCATION, width: self.view.frame.width, height: 50.0)
    self.adView?.loadAd()
    self.adView?.delegate = self
    self.adView?.frame = regRect
    self.adView?.backgroundColor = .lightGray
    self.view.addSubview(adView!)
}

override func viewDidAppear(_ animated: Bool) {
   // Refresh Ads using previous settings
   self.adView?.forceRefreshAd()
}

Return the view where the Banner Ad will show.

extension ViewController: MPAdViewDelegate {
    func viewControllerForPresentingModalView() -> UIViewController! {
        return self
    }
}

Going Live

Once you are ready to go live, change your app unit to the one you’ve created on Mopub.

To monitor your app’s monetisation, set targets and much more download our Motics app.

Leave a Reply

Your email address will not be published.