横幅和 MREC 广告
横幅和 MREC 广告均为矩形的广告格式,会占据应用版面中的一部分,通常位于屏幕顶部或底部,有时也内嵌在可滚动的内容中。用户与应用互动时,横幅和 MREC 广告始终可见,既不会干扰或打断游戏和使用体验,也可以在指定时间周期后自动刷新。
以下各节将向您介绍如何加载、显示和隐藏横幅或 MREC 广告。
加载横幅或 MREC
横幅
要加载横幅,请创建与广告单元相对应的 MAAdView
对象并调用其 loadAd
方法。要展示该广告,请将 MAAdView
对象添加为视图层级的子视图。
实现 MAAdViewAdDelegate
以便在广告就绪时收到通知。
这也会提醒您其他的广告相关事件。
#import "ExampleViewController.h"#import <AppLovinSDK/AppLovinSDK.h>
@interface ExampleViewController()<MAAdViewAdDelegate>@property (nonatomic, strong) MAAdView *adView;@end
@implementation ExampleViewController
- (void)createBannerAd{ self.adView = [[MAAdView alloc] initWithAdUnitIdentifier: @"«ad-unit-ID»"]; self.adView.delegate = self;
// Banner height on iPhone and iPad is 50 and 90, respectively CGFloat height = (UIDevice.currentDevice.userInterfaceIdiom == UIUserInterfaceIdiomPad) ? 90 : 50;
// Stretch to the width of the screen for banners to be fully functional CGFloat width = CGRectGetWidth(UIScreen.mainScreen.bounds);
self.adView.frame = CGRectMake(x, y, width, height);
// Set background or background color for banners to be fully functional self.adView.backgroundColor = «background-color»;
[self.view addSubview: self.adView];
// Load the ad [self.adView loadAd];}
#pragma mark - MAAdDelegate Protocol
- (void)didLoadAd:(MAAd *)ad {}
- (void)didFailToLoadAdForAdUnitIdentifier:(NSString *)adUnitIdentifier withError:(MAError *)error {}
- (void)didClickAd:(MAAd *)ad {}
- (void)didFailToDisplayAd:(MAAd *)ad withError:(MAError *)error {}
#pragma mark - MAAdViewAdDelegate Protocol
- (void)didExpandAd:(MAAd *)ad {}
- (void)didCollapseAd:(MAAd *)ad {}
#pragma mark - Deprecated Callbacks
- (void)didDisplayAd:(MAAd *)ad { /* DO NOT USE - THIS IS RESERVED FOR FULLSCREEN ADS ONLY AND WILL BE REMOVED IN A FUTURE SDK RELEASE */ }- (void)didHideAd:(MAAd *)ad { /* DO NOT USE - THIS IS RESERVED FOR FULLSCREEN ADS ONLY AND WILL BE REMOVED IN A FUTURE SDK RELEASE */ }
@end
要加载横幅,请创建与广告单元相对应的 MAAdView
对象并调用其 loadAd
方法。要展示该广告,请将 MAAdView
对象添加为视图层级的子视图。
实现 MAAdViewAdDelegate
以便在广告就绪时收到通知。
这也会提醒您其他的广告相关事件。
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, 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)
// Load the first ad 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 */ }}
要加载横幅,请创建一个 UIViewRepresentable
对象,这是一个包装器,能让您将 MAAdView
(一种 UIKit
视图类型对象) 集成到 SwiftUI 视图层级中。同时,为包装器对象提供自定义的 Coordinator
类,该类需遵循 MAAdViewAdDelegate
协议。
在广告准备就绪时它会通知您,以及通知您其他广告相关事件。
在包装器的 makeUIView
方法中创建与广告单元相对应的 MAAdView
对象。调用其 loadAd
方法。
要展示该广告,请在 SwiftUI 视图层级中添加 UIViewRepresentable
包装器对象。
您可以在 AppLovin-MAX_SDK_iOS Github 库 中找到相关实施示例。
import AppLovinSDK
struct ExampleSwiftUIWrapper: UIViewRepresentable{ func makeUIView(context: Context) -> MAAdView { let adView = MAAdView(adUnitIdentifier: "«ad-unit-ID»") adView.delegate = context.coordinator
// Set background or background color for banners to be fully functional adView.backgroundColor = «background-color»
// Load the first Ad adView.loadAd()
return adView }
func updateUIView(_ uiView: MAAdView, context: Context) {}
func makeCoordinator() -> Coordinator { Coordinator() }}
extension ExampleSwiftUIWrapper{ class Coordinator: NSObject, MAAdViewAdDelegate { // 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) { /* use this for impression tracking */ } func didHide(_ ad: MAAd) { /* DO NOT USE - THIS IS RESERVED FOR FULLSCREEN ADS ONLY AND WILL BE REMOVED IN A FUTURE SDK RELEASE */ } }}
// SwiftUI view to show adstruct ExampleSwiftUIBannerAdView: View{ // 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
var body: some View { ExampleSwiftUIWrapper() .frame(width: width, height: height) }}
MREC
要加载 MREC 广告,请创建与广告单元相对应的 MAAdView
对象,并调用其 loadAd
方法。要展示广告,请将 MAAdView
对象添加为视图层级的子视图。实现 MAAdViewAdDelegate
以便在广告就绪时收到通知。这也会通知您其他广告相关事件。
#import "ExampleViewController.h"#import <AppLovinSDK/AppLovinSDK.h>
@interface ExampleViewController()<MAAdViewAdDelegate>@property (nonatomic, strong) MAAdView *adView;@end
@implementation ExampleViewController
- (void)createMRECAd{ self.adView = [[MAAdView alloc] initWithAdUnitIdentifier: @"«ad-unit-ID»" adFormat: MAAdFormat.mrec]; self.adView.delegate = self;
// MREC width and height are 300 and 250 respectively, on iPhone and iPad CGFloat width = 300; CGFloat height = 250;
// Center the MREC CGFloat x = self.view.center.x - 150;
self.adView.frame = CGRectMake(x, y, width, height);
// Set background or background color for MREC ads to be fully functional self.adView.backgroundColor = «background-color»;
[self.view addSubview: self.adView];
// Load the ad [self.adView loadAd];}
#pragma mark - MAAdDelegate Protocol
- (void)didLoadAd:(MAAd *)ad {}
- (void)didFailToLoadAdForAdUnitIdentifier:(NSString *)adUnitIdentifier withError:(MAError *)error {}
- (void)didClickAd:(MAAd *)ad {}
- (void)didFailToDisplayAd:(MAAd *)ad withError:(MAError *)error {}
#pragma mark - MAAdViewAdDelegate Protocol
- (void)didExpandAd:(MAAd *)ad {}
- (void)didCollapseAd:(MAAd *)ad {}
#pragma mark - Deprecated Callbacks
- (void)didDisplayAd:(MAAd *)ad { /* DO NOT USE - THIS IS RESERVED FOR FULLSCREEN ADS ONLY AND WILL BE REMOVED IN A FUTURE SDK RELEASE */ }- (void)didHideAd:(MAAd *)ad { /* DO NOT USE - THIS IS RESERVED FOR FULLSCREEN ADS ONLY AND WILL BE REMOVED IN A FUTURE SDK RELEASE */ }
@end
要加载 MREC 广告,请创建与广告单元相对应的 MAAdView
对象并调用其 loadAd
方法。要展示广告,请将 MAAdView
对象添加为视图层级的子视图。实现 MAAdViewAdDelegate
以便在广告就绪时收到通知。这也会提醒您其他广告相关事件。
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 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)
// Load the first ad 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 */ }}
要加载 MREC 广告,请首先创建一个 UIViewRepresentable
对象,这是一个包装器,允许您将 MAAdView
(一种 UIKit
视图类型对象) 集成到 SwiftUI 视图层级中。同时,为包装器对象提供自定义的 Coordinator
类,该类需遵循 MAAdViewAdDelegate
协议。
广告准备就绪时会通知您,以及通知您其他广告相关事件。
在包装器的 makeUIView
方法内,创建与广告单元相对应的 MAAdView
对象并调用其 loadAd
方法。
要展示该广告,请在 SwiftUI 视图层级中添加 UIViewRepresentable
包装器对象。
您可以在 AppLovin-MAX_SDK_iOS Github 库 中找到相关实施示例。
import AppLovinSDK
struct ExampleSwiftUIWrapper: UIViewRepresentable{ func makeUIView(context: Context) -> MAAdView { let adView = MAAdView(adUnitIdentifier: "«ad-unit-ID»", adFormat: MAAdFormat.mrec) adView.delegate = context.coordinator
// Set background or background color for banners to be fully functional adView.backgroundColor = «background-color»
// Load the first Ad adView.loadAd()
return adView }
func updateUIView(_ uiView: MAAdView, context: Context) {}
func makeCoordinator() -> Coordinator { Coordinator() }}
extension ExampleSwiftUIWrapper{ class Coordinator: NSObject, MAAdViewAdDelegate { // 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) { /* use this for impression tracking */ } func didHide(_ ad: MAAd) { /* DO NOT USE - THIS IS RESERVED FOR FULLSCREEN ADS ONLY AND WILL BE REMOVED IN A FUTURE SDK RELEASE */ } }}
// SwiftUI view to show adstruct ExampleSwiftUIMRECAdView: View{ // MREC width and height are 300 and 250 respectively, on iPhone and iPad let height: CGFloat = 250 let width: CGFloat = 300
var body: some View { ExampleSwiftUIWrapper() .frame(width: width, height: height) }}
销毁横幅和 MREC 广告
您可能不再需要 MAAdView
实例。
例如,如果用户购买了广告移除服务,就可能发生这种情况。
释放此类 MAAdView
实例以释放资源。
如果您使用多个具有相同广告单元 ID 的实例,请不要释放 MAAdView
实例。
[self.adView removeFromSuperview];self.adView.delegate = nil;self.adView = nil;
adView.removeFromSuperview()adView.delegate = niladView = nil
自适应横幅广告
自适应横幅是响应性横幅,其高度取决于设备类型和横幅宽度。
集成自适应横幅的方法与集成常规横幅类似。
但是您必须将高度设置为由 MAAdFormat.banner.adaptiveSize.height
返回的值,而不是 50 或 90 这样的常量值。在加载广告之前,将横幅额外参数 adaptive_banner
设置为 true
,如以下代码所示:
- (void)createBannerAd{ self.adView = [[MAAdView alloc] initWithAdUnitIdentifier: @"«ad-unit-ID»"]; self.adView.delegate = self;
// Get the adaptive banner height CGFloat height = MAAdFormat.banner.adaptiveSize.height;
// Stretch to the width of the screen for banners to be fully functional CGFloat width = CGRectGetWidth(UIScreen.mainScreen.bounds);
self.adView.frame = CGRectMake(x, y, width, height); [self.adView setExtraParameterForKey: @"adaptive_banner" value: @"true"];
// Set background or background color for banners to be fully functional self.adView.backgroundColor = «background-color»;
[self.view addSubview: self.adView];
// Load the ad [self.adView loadAd];}
func createBannerAd(){ adView = MAAdView(adUnitIdentifier: "«ad-unit-ID»") adView.delegate = self
// Get the adaptive banner height. let height: CGFloat = MAAdFormat.banner.adaptiveSize.height
// 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) adView.setExtraParameterForKey("adaptive_banner", value: "true")
// Set background or background color for banners to be fully functional adView.backgroundColor = «background-color»
view.addSubview(adView)
// Load the first ad adView.loadAd()}
struct ExampleSwiftUIWrapper: UIViewRepresentable{ func makeUIView(context: Context) -> MAAdView { let adView = MAAdView(adUnitIdentifier: "«ad-unit-ID»") adView.delegate = context.coordinator
adView.setExtraParameterForKey("adaptive_banner", value: "true")
// Set background or background color for banners to be fully functional adView.backgroundColor = «background-color»
// Load the first Ad adView.loadAd()
return adView } ⋮}
// SwiftUI view to show adstruct ExampleSwiftUIBannerAdView: View{ let height: CGFloat = MAAdFormat.banner.adaptiveSize.height
// Stretch to the width of the screen for banners to be fully functional let width: CGFloat = UIScreen.main.bounds.width
var body: some View { ExampleSwiftUIWrapper() .frame(width: width, height: height) }}
设置自定义宽度
对于更具体的集成,您可以使用本地额外参数 API 设置自定义宽度 (Google 适配器版本 10.2.0.2 及 Google Ad Manager 适配器版本 10.2.0.1 及更高版本)。 您可以使用自适应尺寸 API 为自定义自适应横幅调取合适的高度。
CGFloat width = 400;CGFloat height = [self.adView.adFormat adapativeSizeForWidth: width].height;⋮[self.adView setLocalExtraParameterForKey: @"adaptive_banner_width" value: @(width)];
let width: CGFloat = 400
// Get the adaptive banner heightlet height: CGFloat = adView.adFormat.adaptiveSize(forWidth: width).height⋮adView.setLocalExtraParameterForKey("adaptive_banner_width", value: width)
let width: CGFloat = 400
// Get the adaptive banner heightlet height: CGFloat = MAAdFormat.banner.adaptiveSize(forWidth: width).height⋮adView.setLocalExtraParameterForKey("adaptive_banner_width", value: width)
内嵌自适应横幅
自适应横幅默认为锚定横幅。 您也可以启用内嵌自适应横幅,此类横幅用于滚动内容中。 内嵌自适应横幅通常比锚定自适应横幅更大更高。 其高度是可变的,可以扩展到设备屏幕的整个高度。
Android 适配器 23.2.0.1 和 iOS 适配器 11.7.0.1 及更新版本均支持内嵌自适应横幅。要启用此功能,请将额外本地参数 adaptive_banner_type
设置为 inline
,如以下代码所示:
[adView setLocalExtraParameterForKey: @"adaptive_banner_type" value: @"inline"];
adView.setLocalExtraParameterForKey("adaptive_banner_type", value: "inline")
adView.setLocalExtraParameterForKey("adaptive_banner_type", value: "inline")
您可能需要为内联自适应横幅设置最大高度(以点为单位)。 您可以使用如下代码执行此操作,以高度 100 点为例:
[adView setLocalExtraParameterForKey: @"inline_adaptive_banner_max_height" value: @(100)];
adView.setLocalExtraParameterForKey("inline_adaptive_banner_max_height", value: 100)
adView.setLocalExtraParameterForKey("inline_adaptive_banner_max_height", value: 100)
您可能需要以取决于自适应横幅广告尺寸的方式配置您的用户界面。 如果是这样,您可以使用如下代码检索已加载广告的宽度和高度(以点为单位):
- (void)didLoadAd:(MAAd *)ad{ CGSize adViewSize = ad.size; CGFloat widthDp = adViewSize.width; CGFloat heightDp = adViewSize.height;}
func didLoad(_ ad: MAAd){ let adViewSize = ad.size let widthDp = adViewSize.width let heightDp = adViewSize.height}
func didLoad(_ ad: MAAd){ let adViewSize = ad.size let widthDp = adViewSize.width let heightDp = adViewSize.height}
停止和启动自动刷新
您可能想要停止广告的自动刷新。 例如,当您隐藏横幅广告或想要手动刷新时,您可能想要这样做。 使用以下代码停止横幅或 MREC 广告的自动刷新:
// Set this extra parameter to work around SDK bug that ignores calls to stopAutoRefresh()[adView setExtraParameterForKey: @"allow_pause_auto_refresh_immediately" value: @"true"];[adView stopAutoRefresh];
// Set this extra parameter to work around SDK bug that ignores calls to stopAutoRefresh()adView.setExtraParameterForKey("allow_pause_auto_refresh_immediately", value: "true")adView.stopAutoRefresh()
// Set this extra parameter to work around SDK bug that ignores calls to stopAutoRefresh()adView.setExtraParameterForKey("allow_pause_auto_refresh_immediately", value: "true")adView.stopAutoRefresh()
使用以下调用启动横幅或 MREC 广告的自动刷新:
[adView startAutoRefresh];
adView.startAutoRefresh()
adView.startAutoRefresh()
使用以下调用手动刷新内容。
在调用 loadAd()
之前您必须停止自动刷新。
[adView loadAd];
adView.loadAd()
adView.loadAd()