How To Integrate Mopub Interstitial 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:
Format | Size | Ad unit ID |
---|---|---|
Banner | 320×50 | 0ac59b0996d947309c33f59d6676399f |
Banner (MRect) | 300×250 | 2aae44d2ab91424d9850870af33e5af7 |
Interstitial | 320×480 | 4f117153f5c24fa6a3a92b818a5eb630 |
Rewarded Ad | 320×480 | 8f000bd5e00246de9c789eed39ff6096 |
Rewarded Playable (MRAID) | 320×480 | 98c29e015e7346bd9c380b1467b33850 |
Native | N/A | 76a3fefaced247959582d2d2df6f4757 |
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"
]
let googleConfig: NSMutableDictionary = [
"native_banner": "true"
]
sdkConfig.additionalNetworks = [PangleAdapterConfiguration.self]
sdkConfig.additionalNetworks = [AppLovinAdapterConfiguration.self]
sdkConfig.additionalNetworks = [FacebookAdapterConfiguration.self]
sdkConfig.additionalNetworks = [GoogleAdMobAdapterConfiguration.self]
sdkConfig.mediatedNetworkConfigurations = [
"PangleAdapterConfiguration": pangleConfig,
"FacebookAdapterConfiguration": facebookConfig,
"GoogleAdMobAdapterConfiguration": googleConfig
]
*/
// 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
var interstitial: MPInterstitialAdController!
var adIntersUnit = "YOUR_AD_UNIT"
// To show your ad every 7th time the user views the page
var adState = 7
var adCnt = 0
}
Now it’s time to load your ad. You can load your ad when a user views the view controller for the first time or show it after x number of times. The latter is the best option.
override func viewDidLoad() {
super.viewDidLoad()
self.becomeFirstResponder()
// Instantiate the interstitial ad.
self.interstitial = MPInterstitialAdController(forAdUnitId: self.adIntersUnit)
self.interstitial.delegate = self
// Prepare ad for use later
self.interstitial.loadAd()
}
To show ads on the nth time (i.e when a user views the view controller every 7th time), put this in your viewDidAppear.
override func viewDidAppear(_ animated: Bool) {
// if the view has appeared n number of times reference adCnt
if self.adState % self.adCnt == 0 {
self.callWhenYouNeedInterstitial()
self.adState = self.adState + 1
} else {
self.adState = self.adState + 1
}
}
Now your ad is ready to be shown. A rule of thumb is to check if an ad is present, if not, request an ad to show for next time and continue the cycle.
func callWhenYouNeedInterstitial() {
// load interstitial ad with desired ad size
if self.interstitial.ready == true {
self.interstitial.show(from: self)
self.interstitial.loadAd()
} else {
self.interstitial.loadAd()
}
}
That’s it, run your app in Xcode.
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.
