插屏广告一般采用全屏或整页形式,会暂时覆盖应用的整个界面,通常出现在应用自然的暂停或过渡节点,例如游戏关卡完成后、主要视图切换时等。
以下各节将向您介绍如何加载和显示插屏广告。
要加载插屏广告,请创建一个与您的广告单元对应的 MAInterstitialAd 对象,并调用其 loadAd() 方法。
实现 MAAdDelegate 以便在广告准备就绪时收到通知。这也会通知您其他广告相关的事件。
#import "ExampleViewController.h"
#import <AppLovinSDK/AppLovinSDK.h>
@interface ExampleViewController()<MAAdDelegate>
@property (nonatomic, strong) MAInterstitialAd *interstitialAd;
@property (nonatomic, assign) NSInteger retryAttempt;
@end
@implementation ExampleViewController
- (void)createInterstitialAd
{
self.interstitialAd = [[MAInterstitialAd alloc] initWithAdUnitIdentifier: @"«ad-unit-ID»"];
self.interstitialAd.delegate = self;
// Load the first ad
[self.interstitialAd loadAd];
}
#pragma mark - MAAdDelegate Protocol
- (void)didLoadAd:(MAAd *)ad
{
// Interstitial ad is ready to be shown. '[self.interstitialAd isReady]' will now return 'YES'
// Reset retry attempt
self.retryAttempt = 0;
}
- (void)didFailToLoadAdForAdUnitIdentifier:(NSString *)adUnitIdentifier withError:(MAError *)error
{
// Interstitial ad failed to load
// AppLovin recommends that you retry with exponentially higher delays up to a maximum delay (in this case 64 seconds)
self.retryAttempt++;
NSInteger delaySec = pow(2, MIN(6, self.retryAttempt));
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, delaySec * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
[self.interstitialAd loadAd];
});
}
- (void)didDisplayAd:(MAAd *)ad {}
- (void)didClickAd:(MAAd *)ad {}
- (void)didHideAd:(MAAd *)ad
{
// Interstitial ad is hidden. Pre-load the next ad
[self.interstitialAd loadAd];
}
- (void)didFailToDisplayAd:(MAAd *)ad withError:(MAError *)error
{
// Interstitial ad failed to display. AppLovin recommends that you load the next ad.
[self.interstitialAd loadAd];
}
@end
class ExampleViewController: UIViewController, MAAdDelegate
{
var interstitialAd: MAInterstitialAd!
var retryAttempt = 0.0
func createInterstitialAd()
{
interstitialAd = MAInterstitialAd(adUnitIdentifier: "«ad-unit-ID»")
interstitialAd.delegate = self
// Load the first ad
interstitialAd.load()
}
// MARK: MAAdDelegate Protocol
func didLoad(_ ad: MAAd)
{
// Interstitial ad is ready to be shown. 'interstitialAd.isReady' will now return 'true'
// Reset retry attempt
retryAttempt = 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 += 1
let 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 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()
}
}最佳实践:展示插屏广告的最佳时机
要展示插屏广告,请在创建的 MAInterstitialAd 实例对象上调用 showAd()
if ( [self.interstitialAd isReady] )
{
[self.interstitialAd showAd];
}
if interstitialAd.isReady
{
interstitialAd.show()
}为避免插屏广告音频干扰应用的背景音频,AppLovin 建议您在 didDisplayAd() 回调触发时暂停应用的背景音频。当 didHideAd() 回调触发时,恢复应用的背景音频。