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
- Swift
- Objective-C
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, respectivelylet height: CGFloat = (UIDevice.current.userInterfaceIdiom == .pad) ? 90 : 50// Stretch to the width of the screen for banners to be fully functionallet width: CGFloat = UIScreen.main.bounds.widthadView.frame = CGRect(x: x, y: y, width: width, height: height)// Set background or background color for banners to be fully functionaladView.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.sizelet size = DTBAdSize(bannerAdSizeWithWidth: Int(rawSize.width),height: Int(rawSize.height),andSlotUUID: amazonAdSlotId)!let adLoader = DTBAdLoader()adLoader.setAdSizes([size])adLoader.loadAd(self)}// MARK: DTBAdCallbackfunc onSuccess(_ adResponse: DTBAdResponse!){// 'adView' is your instance of MAAdViewadView.setLocalExtraParameterForKey("amazon_ad_response", value: adResponse)adView.loadAd()}func onFailure(_ error: DTBAdError, dtbAdErrorInfo: DTBAdErrorInfo!){// 'adView' is your instance of MAAdViewadView.setLocalExtraParameterForKey("amazon_ad_error", value:dtbAdErrorInfo)adView.loadAd()}// MARK: MAAdDelegate Protocolfunc 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 Protocolfunc didExpand(_ ad: MAAd) {}func didCollapse(_ ad: MAAd) {}// MARK: Deprecated Callbacksfunc 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
- Swift
- Objective-C
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 iPadlet height: CGFloat = 250let width: CGFloat = 300adView.frame = CGRect(x: x, y: y, width: width, height: height)// Center the MRECadView.center.x = view.center.x// Set background or background color for MREC ads to be fully functionaladView.backgroundColor = «background-color»view.addSubview(adView)let adLoader = DTBAdLoader()adLoader.setAdSizes([DTBAdSize(bannerAdSizeWithWidth: 300,height: 250,andSlotUUID: amazonAdSlotId)!])adLoader.loadAd(self)}// MARK: DTBAdCallback Protocolfunc onSuccess(_ adResponse: DTBAdResponse!){// 'adView' is your instance of MAAdViewadView.setLocalExtraParameterForKey("amazon_ad_response", value: adResponse)adView.loadAd()}func onFailure(_ error: DTBAdError, dtbAdErrorInfo: DTBAdErrorInfo!){// 'adView' is your instance of MAAdViewadView.setLocalExtraParameterForKey("amazon_ad_error", value:dtbAdErrorInfo)adView.loadAd()}// MARK: MAAdDelegate Protocolfunc 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 Protocolfunc didExpand(_ ad: MAAd) {}func didCollapse(_ ad: MAAd) {}// MARK: Deprecated Callbacksfunc 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
- Swift
- Objctive-C
@UIApplicationMainclass 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) inappOpenAd.delegate = selfappOpenAd.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
- Swift
- Objective-C
import AppLovinSDKimport DTBiOSSDKclass ExampleViewController: UIViewController, MAAdDelegate, DTBAdCallback{var interstitialAd: MAInterstitialAd!private static var interstitialAd = MAInterstitialAd(adUnitIdentifier: "«MAX-inter-ad-unit-ID»")private static var isFirstLoad = truefunc createInterstitialAd(){interstitialAd.delegate = selfif self.isFirstLoad {self.isFirstLoad = falselet adLoader = DTBAdLoader()adLoader.setAdSizes([DTBAdSize(interstitialAdSizeWithSlotUUID: "«Amazon-inter-slot-ID»")!])adLoader.loadAd(self)} else {self.interstitialAd.load()}}// MARK: DTBAdCallback Protocolfunc onSuccess(_ adResponse: DTBAdResponse!){// 'interstitialAd' is your instance of MAInterstitialAdself.interstitialAd.setLocalExtraParameterForKey("amazon_ad_response", value: adResponse)self.interstitialAd.load()}func onFailure(_ error: DTBAdError, dtbAdErrorInfo: DTBAdErrorInfo!){// 'interstitialAd' is your instance of MAInterstitialAdself.interstitialAd.setLocalExtraParameterForKey("amazon_ad_error", value: dtbAdErrorInfo)self.interstitialAd.load()}// MARK: MAAdDelegate Protocolfunc 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 adinterstitialAd.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
- Swift
- Objective-C
if interstitialAd.isReady{interstitialAd.show()}
Rewarded Ads
Load a Rewarded Ad
- Swift
- Objective-C
class ExampleViewController : UIViewController, MARewardedAdDelegate{var rewardedAd: MARewardedAd!var retryAttempt = 0.0func createRewardedAd(){rewardedAd = MARewardedAd.shared(withAdUnitIdentifier: "«ad-unit-ID»")rewardedAd.delegate = self// Load the first adrewardedAd.load()}// MARK: MAAdDelegate Protocolfunc didLoad(_ ad: MAAd){// Rewarded ad is ready to show. '[self.rewardedAd isReady]' now returns 'YES'.// Reset retry attemptretryAttempt = 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 += 1let 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 adrewardedAd.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 Protocolfunc didRewardUser(for ad: MAAd, with reward: MAReward){// Rewarded ad was displayed and user should receive the reward}}
Showing a Rewarded Ad
- Swift
- Objective-C
if rewardedAd.isReady{rewardedAd.show()}