バナー&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のビュー階層に統合するためのラッパーです。
また、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 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のビュー階層に統合するためのラッパーです。
また、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 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
アダプティブバナー
アダプティブバナーは、デバイスの種類やバナーの幅に基づいて高さが調整される、レスポンシブなバナーです。
アダプティブバナーは、通常のバナーと同じ方法で連携します。
ただし、高さは50や90のような定数ではなく、MAAdFormat.banner.adaptiveSize.height
が返す値に設定する必要があります。
広告をロードする前に、以下のコードで示すように、バナーの追加パラメーター 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 アド マネージャーアダプターバージョン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)
アダプティブ バナーのサイズに依存する方法で UI を構成する必要がある場合があります。その場合は、次のようなコードを使用して、読み込まれた広告の幅と高さをポイント単位で取得できます。
- (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()