バナー&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 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 = (UIDevice.current.userInterfaceIdiom == .pad) ? 90 : 50
// Stretch to the width of the screen for banners to be fully functional let width = UIScreen.main.bounds.width
adView.frame = CGRect(x: x, y: y, width: width, height: height)
// Set 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のビュー階層に統合するためのラッパーです。
また、MAAdViewAdDelegate
に準拠したラッパーオブジェクト用のカスタム Coordinator
クラスも追加します。
これにより、広告の準備が完了した際に通知され、他の広告関連イベントについても通知されるようになります。
ラッパーの makeUIView
メソッド内で、広告ユニットに対応する MAAdView
オブジェクトを作成し、その loadAd
メソッドを呼び出します。
広告を表示するには、SwiftUIのビュー階層内に UIViewRepresentable
ラッパーオブジェクトを追加します。
実装例については、AppLovin-MAX_SDK_iOS Github repositoryを参照してください。
import AppLovinSDK
struct ExampleSwiftUIWrapper: UIViewRepresentable{ func makeUIView(context: Context) -> MAAdView { let adView = MAAdView(adUnitIdentifier: "«ad-unit-ID»") adView.delegate = context.coordinator
// Set 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 = (UIDevice.current.userInterfaceIdiom == .pad) ? 90 : 50
// Stretch to the width of the screen for banners to be fully functional let width = UIScreen.main.bounds.width
var body: some View { ExampleSwiftUIWrapper() .frame(width: width, height: height) }}
MRECs
ミディアムレクタングル(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 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 = 250 let width = 300
adView.frame = CGRect(x: x, y: y, width: width, height: height)
// Center the MREC adView.center.x = view.center.x
// Set 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のビュー階層に統合するためのラッパーです。
また、MAAdViewAdDelegate
に準拠したラッパーオブジェクト用のカスタム Coordinator
クラスも追加します。
これにより、広告の準備が完了した際に通知され、他の広告関連イベントについても通知されるようになります。
ラッパーの makeUIView
メソッド内で、広告ユニットに対応する MAAdView
オブジェクトを作成し、その loadAd
メソッドを呼び出します。
広告を表示するには、SwiftUIのビュー階層内に UIViewRepresentable
ラッパーオブジェクトを追加します。
実装例については、AppLovin-MAX_SDK_iOS Github repositoryを参照してください。
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 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 = 250 let width = 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
アダプティブバナー
アダプティブバナーは、デバイスの種類や表示可能な幅に応じてディメンションが動的に調整されるレスポンシブな広告です。 アダプティブバナーにはアンカー型とインライン型の2種類があり、それぞれ異なる連携ニーズに対応しています。
MAX SDKバージョン13.2.0以降、MAAdView
をMAAdViewConfiguration
オブジェクトで初期化することで、アダプティブバナーを実装できるようになりました。
アンカーアダプティブバナー
アンカーアダプティブバナーは、画面の上部または下部にバナーを固定表示するものです。 デバイスの種類とバナーの幅に基づいて高さを動的に調整します。
MAAdView
の高さは、50や90のような定数ではなく、MAAdFormat.banner.adaptiveSize.height
が返す値を使用して設定する必要があります。
- (void)createAnchoredAdaptiveBannerAd{ // Stretch to the width of the screen for banners to be fully functional CGFloat width = CGRectGetWidth(UIScreen.mainScreen.bounds);
// Get the anchored adaptive banner height CGFloat height = MAAdFormat.banner.adaptiveSize.height;
MAAdViewConfiguration *config = [MAAdViewConfiguration configurationWithBuilderBlock:^(MAAdViewConfigurationBuilder *builder) { builder.adaptiveType = MAAdViewAdaptiveTypeAnchored; }];
self.adView = [[MAAdView alloc] initWithAdUnitIdentifier: @"«ad-unit-ID»" configuration: config]; self.adView.delegate = self;
self.adView.frame = CGRectMake(x, y, width, height);
// Set 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 createAnchoredAdaptiveBannerAd(){ // Stretch to the width of the screen for banners to be fully functional let width = UIScreen.main.bounds.width
// Get the anchored adaptive banner height. let height = MAAdFormat.banner.adaptiveSize.height
let config = MAAdViewConfiguration { builder in builder.adaptiveType = .anchored } adView = MAAdView(adUnitIdentifier: "«ad-unit-ID»", configuration: config) adView.delegate = self
adView.frame = CGRect(x: x, y: y, width: width, height: height)
// Set 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 config = MAAdViewConfiguration { builder in builder.adaptiveType = .anchored } let adView = MAAdView(adUnitIdentifier: "«ad-unit-ID»", configuration: config) adView.delegate = context.coordinator
// Set 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 ExampleSwiftUIAdaptiveBannerAdView: View{ // Stretch to the width of the screen for banners to be fully functional let width = UIScreen.main.bounds.width
// Get the anchored adaptive banner height let height = MAAdFormat.banner.adaptiveSize.height
var body: some View { ExampleSwiftUIWrapper() .frame(width: width, height: height) }}
カスタム幅を設定
より柔軟な連携を行う場合は、MAAdViewConfiguration
ビルダーメソッドを呼び出すことで、ポイント単位のカスタム幅を設定できます(Googleアダプターバージョン10.2.0.2、Google Ad Managerアダプターバージョン10.2.0.1、Liftoff Monetizeアダプターバージョン7.4.5.1以降でサポート)。
カスタムのアンカーアダプティブ広告に適切な高さを取得するには、Adaptive Size APIを呼び出してください。
CGFloat width = 400;
// Get the anchored adaptive banner heightCGFloat height = [self.adView.adFormat adapativeSizeForWidth: width].height;
MAAdViewConfiguration *config = [MAAdViewConfiguration configurationWithBuilderBlock:^(MAAdViewConfigurationBuilder *builder) { builder.adaptiveType = MAAdViewAdaptiveTypeAnchored; builder.adaptiveWidth = width;}];self.adView = [[MAAdView alloc] initWithAdUnitIdentifier: @"«ad-unit-ID»" configuration: config];
let width = 400
// Get the anchored adaptive banner heightlet height = adView.adFormat.adaptiveSize(forWidth: width).height
let config = MAAdViewConfiguration { builder in builder.adaptiveType = .anchored builder.adaptiveWidth = width}adView = MAAdView(adUnitIdentifier: "«ad-unit-ID»", configuration: config)
let width = 400
// Get the anchored adaptive banner heightlet height = adView.adFormat.adaptiveSize(forWidth: width).height
let config = MAAdViewConfiguration { builder in builder.adaptiveType = .anchored builder.adaptiveWidth = width}let adView = MAAdView(adUnitIdentifier: "«ad-unit-ID»", configuration: config)⋮
インラインアダプティブバナー
アダプティブバナーは、デフォルトで場所が固定(アンカー)されていますが、インラインアダプティブバナーを有効にすれば、スクロール可能なコンテンツ内にバナーを表示できます。 インラインアダプティブバナーは、通常アンカーアダプティブバナーよりも大きく、高さがあります。 高さは可変で、デバイス画面の高さいっぱいまで伸ばすことが可能です。
インラインアダプティブバナーは、iOS Googleアダプターバージョン11.7.0.1 および Liftoff Monetize バージョン7.4.5.1以降でサポートされます。
インラインアダプティブバナーを有効化するには、以下のコードのように、MAAdViewConfiguration
のアダプティブタイプをMAAdViewAdaptiveTypeInline
に設定します。
MAAdViewConfiguration *config = [MAAdViewConfiguration configurationWithBuilderBlock:^(MAAdViewConfigurationBuilder *builder) { builder.adaptiveType = MAAdViewAdaptiveTypeInline; ⋮}];
let config = MAAdViewConfiguration { builder in builder.adaptiveType = .inline ⋮}
let config = MAAdViewConfiguration { builder in builder.adaptiveType = .inline ⋮}
インラインアダプティブ広告の高さの最大値は、デフォルトでデバイス画面の高さと同じになります。
インラインアダプティブ広告を確実にMAAdView
の高さに収めるには、広告の高さの最大値をポイント単位で設定します。以下のコードの例では、最大100ポイントの高さに設定しています。
MAAdViewConfiguration *config = [MAAdViewConfiguration configurationWithBuilderBlock:^(MAAdViewConfigurationBuilder *builder) { builder.adaptiveType = MAAdViewAdaptiveTypeInline; builder.inlineMaximumHeight = 100; ⋮}];
let config = MAAdViewConfiguration { builder in builder.adaptiveType = .inline builder.inlineMaximumHeight = 100 ⋮}
let config = MAAdViewConfiguration { builder in builder.adaptiveType = .inline builder.inlineMaximumHeight = 100 ⋮}
インラインアダプティブMREC広告
インラインアダプティブMRECは、iOS Googleアダプターバージョン11.13.0.1およびLiftoff Monetizeバージョン 7.4.5.1以降でサポートされています。
MAAdViewAdaptiveTypeInline
タイプを正常に機能させるには、インラインアダプティブMRECを設定する必要があります。
インラインアダプティブMRECの幅は、デフォルトでアプリケーション画面の横幅と同じですが、カスタムオプションとして幅をポイント単位で指定できます。 高さは可変で、最大値を指定しない場合は、標準的なMRECの高さを超えてデバイス画面の全高まで拡張される可能性があります。 以下の例のように、インラインアダプティブMRECを設定します。
- (void)createInlineAdaptiveMRecAd{ // Set a custom width, in points, for the inline adaptive MREC. Otherwise stretch to screen width by using CGRectGetWidth(UIScreen.mainScreen.bounds) CGFloat width = 400;
// Set a maximum height, in points, for the inline adaptive MREC. Otherwise use standard MREC height of 250 points // Google recommends a height greater than 50 points, with a minimum of 32 points and a maximum equal to the screen height // The value must also not exceed the height of the MAAdView CGFloat height = 300;
MAAdViewConfiguration *config = [MAAdViewConfiguration configurationWithBuilderBlock:^(MAAdViewConfigurationBuilder *builder) { builder.adaptiveType = MAAdViewAdaptiveTypeInline; builder.adaptiveWidth = width; // Optional: The adaptive ad will span the width of the application window if you do not set a value builder.inlineMaximumHeight = height; // Optional: The maximum height will be the screen height if you do not set a value }]; self.adView = [[MAAdView alloc] initWithAdUnitIdentifier: @"«ad-unit-ID»" adFormat: MAAdFormat.mrec configuration: config]; self.adView.delegate = self;
self.adView.frame = CGRectMake(x, y, width, height);
// Set background color for adaptive MRECs to be fully functional self.adView.backgroundColor = «background-color»;
[self.view addSubview: self.adView];
// Load the ad [self.adView loadAd];}
func createInlineAdaptiveMRecAd(){ // Set a custom width, in points, for the inline adaptive MREC. Otherwise stretch to screen width by using UIScreen.main.bounds.width let width = 400
// Set a maximum height, in points, for the inline adaptive MREC. Otherwise use standard MREC height of 250 points // Google recommends a height greater than 50 points, with a minimum of 32 points and a maximum equal to the screen height // The value must also not exceed the height of the MAAdView let height = 300
let config = MAAdViewConfiguration { builder in builder.adaptiveType = .inline builder.adaptiveWidth = width // Optional: The adaptive ad will span the width of the application window if you do not set a value builder.inlineMaximumHeight = height // Optional: The maximum height will be the screen height if you do not set a value } adView = MAAdView(adUnitIdentifier: "«ad-unit-ID»", adFormat: MAAdFormat.mrec, configuration: config) adView.delegate = self
adView.frame = CGRect(x: x, y: y, width: width, height: height)
// Set background color for adaptive MRECs to be fully functional adView.backgroundColor = «background-color»
view.addSubview(adView)
// Load the first ad adView.loadAd()}
struct ExampleSwiftUIWrapper: UIViewRepresentable{ let width: CGFloat let height: CGFloat
func makeUIView(context: Context) -> MAAdView { let config = MAAdViewConfiguration { builder in builder.adaptiveType = .inline builder.adaptiveWidth = width // Optional: The adaptive ad will span the width of the application window if you do not set a value builder.inlineMaximumHeight = height // Optional: The maximum height will be the screen height if you do not set a value } let adView = MAAdView(adUnitIdentifier: "«ad-unit-ID»", adFormat: MAAdFormat.mrec, configuration: config) adView.delegate = context.coordinator
// Set background color for adaptive MRECs to be fully functional adView.backgroundColor = «background-color»
// Load the first ad adView.loadAd()
return adView } ⋮}
struct ExampleSwiftUIInlineAdaptiveMRecAdView: View{ // Set a custom width, in points, for the inline adaptive MREC. Otherwise stretch to screen width by using UIScreen.main.bounds.width let width = 400
// Set a maximum height, in points, for the inline adaptive MREC. Otherwise use standard MREC height of 250 points // Google recommends a height greater than 50 points, with a minimum of 32 points and a maximum equal to the screen height // The value must also not exceed the height of the MAAdView let height = 300
var body: some View { ExampleSwiftUIWrapper(width: width, height: height) .frame(width: width, height: height) }}
アダプティブ広告サイズを変更
ロードしたアダプティブ広告が、リクエストしたディメンションよりも小さい場合があります。 また、配信されるアダプティブ広告のサイズに合わせて柔軟に適用できるUIを構築したい場合もあるでしょう。 これに対応するには以下のようなコードを使用して、ロードする広告の幅と高さをポイント単位で取得します。
- (void)didLoadAd:(MAAd *)ad{ CGSize adViewSize = ad.size; CGFloat width = adViewSize.width; CGFloat height = adViewSize.height; ⋮}
func didLoad(_ ad: MAAd){ let adViewSize = ad.size let width = adViewSize.width let height = adViewSize.height ⋮}
func didLoad(_ ad: MAAd){ let adViewSize = ad.size let width = adViewSize.width let height = 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()