iOS SDK Usage

Banner&MREC Ads

Once the initialization of the SDK is complete, you can get and load banner and mrec ads from the Empower AppLovin SDK.

Loading a Banner or MREC

Banners

class ExampleViewController: UIViewController, MAAdViewAdDelegate, DTBAdCallback
{
var adView: MAAdView!
var amazonAdSlotId: String!
var adFormat: MAAdFormat!
func createBannerAd()
{
adView = MAAdView(adUnitIdentifier: "«ad-unit-ID»")
adView.delegate = self
// Banner height on iPhone and iPad is 50 and 90, respectively
let height: CGFloat = (UIDevice.current.userInterfaceIdiom == .pad) ? 90 : 50
// Stretch to the width of the screen for banners to be fully functional
let width: CGFloat = UIScreen.main.bounds.width
adView.frame = CGRect(x: x, y: y, width: width, height: height)
// Set background or background color for banners to be fully functional
adView.backgroundColor = «background-color»
view.addSubview(adView)
if UIDevice.current.userInterfaceIdiom == .pad {
amazonAdSlotId = "«Amazon-leader-slot-ID»"
adFormat = MAAdFormat.leader
} else {
amazonAdSlotId = "«Amazon-banner-slot-ID»"
adFormat = MAAdFormat.banner
}
let rawSize = adFormat.size
let size = DTBAdSize(bannerAdSizeWithWidth: Int(rawSize.width),
height: Int(rawSize.height),
andSlotUUID: amazonAdSlotId)!
let adLoader = DTBAdLoader()
adLoader.setAdSizes([size])
adLoader.loadAd(self)
}
// MARK: DTBAdCallback
func onSuccess(_ adResponse: DTBAdResponse!)
{
// 'adView' is your instance of MAAdView
adView.setLocalExtraParameterForKey("amazon_ad_response", value: adResponse)
adView.loadAd()
}
func onFailure(_ error: DTBAdError, dtbAdErrorInfo: DTBAdErrorInfo!)
{
// 'adView' is your instance of MAAdView
adView.setLocalExtraParameterForKey("amazon_ad_error", value:dtbAdErrorInfo)
adView.loadAd()
}
// MARK: MAAdDelegate Protocol
func didLoad(_ ad: MAAd) {}
func didFailToLoadAd(forAdUnitIdentifier adUnitIdentifier: String, withError error: MAError) {}
func didClick(_ ad: MAAd) {}
func didFail(toDisplay ad: MAAd, withError error: MAError) {}
// MARK: MAAdViewAdDelegate Protocol
func didExpand(_ ad: MAAd) {}
func didCollapse(_ ad: MAAd) {}
// MARK: Deprecated Callbacks
func didDisplay(_ ad: MAAd) { /* DO NOT USE - THIS IS RESERVED FOR FULLSCREEN ADS ONLY AND WILL BE REMOVED IN A FUTURE SDK RELEASE */ }
func didHide(_ ad: MAAd) { /* DO NOT USE - THIS IS RESERVED FOR FULLSCREEN ADS ONLY AND WILL BE REMOVED IN A FUTURE SDK RELEASE */ }
}

MRECs

class ExampleViewController: UIViewController, MAAdViewAdDelegate, DTBAdCallback
{
var adView: MAAdView!
var amazonAdSlotId: String!
func createMRECAd()
{
adView = MAAdView(adUnitIdentifier: "«ad-unit-ID»", adFormat: MAAdFormat.mrec)
adView.delegate = self
// MREC width and height are 300 and 250 respectively, on iPhone and iPad
let height: CGFloat = 250
let width: CGFloat = 300
adView.frame = CGRect(x: x, y: y, width: width, height: height)
// Center the MREC
adView.center.x = view.center.x
// Set background or background color for MREC ads to be fully functional
adView.backgroundColor = «background-color»
view.addSubview(adView)
let adLoader = DTBAdLoader()
adLoader.setAdSizes([DTBAdSize(bannerAdSizeWithWidth: 300,
height: 250,
andSlotUUID: amazonAdSlotId)!])
adLoader.loadAd(self)
}
// MARK: DTBAdCallback Protocol
func onSuccess(_ adResponse: DTBAdResponse!)
{
// 'adView' is your instance of MAAdView
adView.setLocalExtraParameterForKey("amazon_ad_response", value: adResponse)
adView.loadAd()
}
func onFailure(_ error: DTBAdError, dtbAdErrorInfo: DTBAdErrorInfo!)
{
// 'adView' is your instance of MAAdView
adView.setLocalExtraParameterForKey("amazon_ad_error", value:dtbAdErrorInfo)
adView.loadAd()
}
// MARK: MAAdDelegate Protocol
func didLoad(_ ad: MAAd) {}
func didFailToLoadAd(forAdUnitIdentifier adUnitIdentifier: String, withError error: MAError) {}
func didClick(_ ad: MAAd) {}
func didFail(toDisplay ad: MAAd, withError error: MAError) {}
// MARK: MAAdViewAdDelegate Protocol
func didExpand(_ ad: MAAd) {}
func didCollapse(_ ad: MAAd) {}
// MARK: Deprecated Callbacks
func didDisplay(_ ad: MAAd) { /* DO NOT USE - THIS IS RESERVED FOR FULLSCREEN ADS ONLY AND WILL BE REMOVED IN A FUTURE SDK RELEASE */ }
func didHide(_ ad: MAAd) { /* DO NOT USE - THIS IS RESERVED FOR FULLSCREEN ADS ONLY AND WILL BE REMOVED IN A FUTURE SDK RELEASE */ }
}

App Open Ads

@UIApplicationMain
class AppDelegate: UIApplicationDelegate
{
private lazy var appOpenAd = MAAppOpenAd(adUnitIdentifier: "«ad-unit-ID»")
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool
{
ALSdk.shared().initializeSdk { (configuration: ALSdkConfiguration) in
appOpenAd.delegate = self
appOpenAd.load()
}
return true
}
func applicationDidBecomeActive(_ application: UIApplication)
{
showAdIfReady()
}
private func showAdIfReady()
{
if ALSdk.shared().isInitialized
{
return
}
if appOpenAd.isReady
{
appOpenAd.show(forPlacement: "«test-placement-ID»")
}
else
{
appOpenAd.load()
}
}
}
extension AppDelegate : MAAdDelegate
{
func didLoad(_ ad: MAAd) {}
func didFailToLoadAd(forAdUnitIdentifier adUnitIdentifier: String, withError error: MAError) {}
func didDisplay(_ ad: MAAd) {}
func didClick(_ ad: MAAd) {}
func didHide(_ ad: MAAd)
{
appOpenAd.load()
}
func didFail(toDisplay ad: MAAd, withError error: MAError)
{
appOpenAd.load()
}
}

Interstitial Ads

Load an Interstitial Ad

import AppLovinSDK
import DTBiOSSDK
class ExampleViewController: UIViewController, MAAdDelegate, DTBAdCallback
{
var interstitialAd: MAInterstitialAd!
private static var interstitialAd = MAInterstitialAd(adUnitIdentifier: "«MAX-inter-ad-unit-ID»")
private static var isFirstLoad = true
func createInterstitialAd()
{
interstitialAd.delegate = self
if self.isFirstLoad {
self.isFirstLoad = false
let adLoader = DTBAdLoader()
adLoader.setAdSizes([DTBAdSize(interstitialAdSizeWithSlotUUID: "«Amazon-inter-slot-ID»")!])
adLoader.loadAd(self)
} else {
self.interstitialAd.load()
}
}
// MARK: DTBAdCallback Protocol
func onSuccess(_ adResponse: DTBAdResponse!)
{
// 'interstitialAd' is your instance of MAInterstitialAd
self.interstitialAd.setLocalExtraParameterForKey("amazon_ad_response", value: adResponse)
self.interstitialAd.load()
}
func onFailure(_ error: DTBAdError, dtbAdErrorInfo: DTBAdErrorInfo!)
{
// 'interstitialAd' is your instance of MAInterstitialAd
self.interstitialAd.setLocalExtraParameterForKey("amazon_ad_error", value: dtbAdErrorInfo)
self.interstitialAd.load()
}
// MARK: MAAdDelegate Protocol
func didLoad(_ ad: MAAd)
{
// Interstitial ad is ready to be shown. 'interstitialAd.isReady' will now return 'true'
}
func didFailToLoadAd(forAdUnitIdentifier adUnitIdentifier: String, withError error: MAError)
{
// Interstitial ad failed to load
// AppLovin recommends that you retry with exponentially higher delays up to a maximum delay (in this case 64 seconds)
}
func didDisplay(_ ad: MAAd) {}
func didClick(_ ad: MAAd) {}
func didHide(_ ad: MAAd)
{
// Interstitial ad is hidden. Pre-load the next ad
interstitialAd.load()
}
func didFail(toDisplay ad: MAAd, withError error: MAError)
{
// Interstitial ad failed to display. AppLovin recommends that you load the next ad.
interstitialAd.load()
}
}

Showing an Interstitial Ad

if interstitialAd.isReady
{
interstitialAd.show()
}

Rewarded Ads

Load a Rewarded Ad

class ExampleViewController : UIViewController, MARewardedAdDelegate
{
var rewardedAd: MARewardedAd!
var retryAttempt = 0.0
func createRewardedAd()
{
rewardedAd = MARewardedAd.shared(withAdUnitIdentifier: "«ad-unit-ID»")
rewardedAd.delegate = self
// Load the first ad
rewardedAd.load()
}
// MARK: MAAdDelegate Protocol
func didLoad(_ ad: MAAd)
{
// Rewarded ad is ready to show. '[self.rewardedAd isReady]' now returns 'YES'.
// Reset retry attempt
retryAttempt = 0
}
func didFailToLoadAd(forAdUnitIdentifier adUnitIdentifier: String, withError error: MAError)
{
// Rewarded ad failed to load
// AppLovin recommends that you retry with exponentially higher delays up to a maximum delay (in this case 64 seconds).
retryAttempt += 1
let delaySec = pow(2.0, min(6.0, retryAttempt))
DispatchQueue.main.asyncAfter(deadline: .now() + delaySec) {
self.rewardedAd.load()
}
}
func didDisplay(_ ad: MAAd) {}
func didClick(_ ad: MAAd) {}
func didHide(_ ad: MAAd)
{
// Rewarded ad is hidden. Pre-load the next ad
rewardedAd.load()
}
func didFail(toDisplay ad: MAAd, withError error: MAError)
{
// Rewarded ad failed to display. AppLovin recommends that you load the next ad.
rewardedAd.load()
}
// MARK: MARewardedAdDelegate Protocol
func didRewardUser(for ad: MAAd, with reward: MAReward)
{
// Rewarded ad was displayed and user should receive the reward
}
}

Showing a Rewarded Ad

if rewardedAd.isReady
{
rewardedAd.show()
}