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{var adView: MAAdView!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)// Load the first adadView.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{var adView: MAAdView!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)// Load the first adadView.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
class ExampleViewController: UIViewController, MAAdDelegate{var interstitialAd: MAInterstitialAd!var retryAttempt = 0.0func createInterstitialAd(){interstitialAd = MAInterstitialAd(adUnitIdentifier: "«ad-unit-ID»")interstitialAd.delegate = self// Load the first adinterstitialAd.load()}// MARK: MAAdDelegate Protocolfunc didLoad(_ ad: MAAd){// Interstitial ad is ready to be shown. 'interstitialAd.isReady' will now return 'true'// Reset retry attemptretryAttempt = 0}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)retryAttempt += 1let delaySec = pow(2.0, min(6.0, retryAttempt))DispatchQueue.main.asyncAfter(deadline: .now() + delaySec) {self.interstitialAd.load()}}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()}