iOS でバナーをネイティブに表示する
概要
注:
このオファリングは現在アルファ版であり、変更される可能性があります。
"バナー ネイティブ" という用語は、ネイティブ需要を返す Mobile SDK Banner クラスの機能を指します。 バナー オブジェクト (ANBannerAdView) はマルチフォーマットであり、バナー広告の需要を返し、以下の例で説明するように API を介して有効にした場合は、ビデオ広告とネイティブ広告です。 つまり、単一の Banner クラス オブジェクトをインスタンス化し、1 つのメンバー ID とコード (または配置 ID) を渡し、バナー、ビデオ、ネイティブメディアの種類の任意の形式またはすべての形式にアクセスする必要があります。
バナー ネイティブ機能を使用しないことにした場合、既存の実装が機能する方法に大きな変更が加えられたことに気付くことはありません。 標準の MobileSDK ヘッダー (削除された 1 つの iOS ヘッダーを含む) には軽微な変更があります。 ただし、コンパイル プロセスを満たすための軽微な 1 回限りの変更以外に、Banner クラスを使用する既存のコードに影響を与える必要はありません。
仮定
このドキュメントの前提は、バナー クラスとネイティブ クラスの両方について既に理解していることです。 バナー ネイティブを使用するには、構造的にも手続き的にも多くの重要な変更を管理する必要があります。 Banner Native の Banner クラスは、従来の Native Request クラスと同様に動作します。 Banner クラスは、従来の Native Request と同様にネイティブ広告オブジェクトを使用してを返すと、Native Response オブジェクトを返します。 この Native Response オブジェクトは、従来の方法で使用して、ネイティブ広告オブジェクトの広告コンテンツを表示および追跡できます。
変更の処理
Mobile SDK バージョン 4.8 以降では、Banner クラスを使用してバナーオブジェクトと動画広告オブジェクトを処理する手順に変更があります。 バナー ネイティブの導入前は、クラス インスタンスが広告オブジェクトを返す前であっても、Banner クラス インスタンス ビューをビュー階層に追加することが可能でした。 これにより、広告オブジェクトの読み込みとフェッチのライフサイクルが完了する前でも、クラスの表示設定を完了することが可能になりました。 この便利なショートカットは、広告が読み込まれたことを示すコールバックを無視する可能性があることを意味しましたが、これは Mobile SDK の一般的なパターンです。
バナー クラスを使用してネイティブ広告をフェッチする場合、表示手順はコールバックが発生するまで待機する必要があります。 これは、コールバックがネイティブ広告オブジェクトの配信ポイントを提供し、返された場合は、ネイティブ広告オブジェクトとバナーまたはビデオ広告オブジェクトのリターンを区別する唯一の場所であるためです。
Mobile SDK の構造
- メソッドは
ANAdDelegate
、id によってのみ定義されている ad オブジェクト ハンドラーを返します。(以前のバージョン 4.8 では、ハンドラーは ID<ANAdProtocol> として定義されていました)。 - プロトコルには
ANAdDelegate
、新しいメソッドが含まれていますad:didReceivNativeAd:
マルチフォーマットのネイティブ バナー広告ビューの使用
次の手順では、マルチフォーマット ANBannerAdView
の使用方法について説明します。
using メソッドと
shouldAllowNativeDemand
&enableNativeRendering
プロパティに対ANBannerAdView
するsetAllowNativeDemand
ネイティブ需要を有効にします。 ビデオ需要を有効にするには、 プロパティを使用します。shouldAllowVideoDemand.
/** * Sets whether or not Native Ads(AppNexus Media Type:12) can serve on this Ad object. * */ @property (nonatomic, readwrite) BOOL shouldAllowNativeDemand; /** * If enableNativeRendering is not set, the default is false. * Rendering NativeAd to behave as BannerAd */ @property (nonatomic, readwrite) BOOL enableNativeRendering; /** * Sets whether or not Video Ads(AppNexus Media Type:4) can serve on this Ad object. */ @property (nonatomic, readwrite) BOOL shouldAllowVideoDemand;
注:
新しい拡張機能に従って、
RendererId
は SDK から渡されるのではなく、コンソールから設定されます。 そのため、プロパティを使用shouldAllowNativeDemand
する非推奨のメッセージがコード ブロックに表示されます。既定では、これらの値は NO に設定されています。この場合
ANBannerAdView
、従来の HTML バナー広告のみが配信されます。 アプリでネイティブとビデオを明示的に有効にする必要があります。一方
loadAd
、できるだけ早く呼び出すことができますが、バナー (またはビデオ) 広告は、デリゲートadDidReceiveAd:
またはad:didReceiveNativeAd:
呼び出されるまでサブビューに配置できません。ネイティブ広告オブジェクトは のインスタンスとして返され、他の従来の
ANNativeAdResponse
ネイティブ応答クラスと同様に処理できます。 この場合、ANBannerAdView
効果的に ANNativeAdRequest と同じ役割を果たします。 (ANNativeAdResponse の詳細については、「 iOS でネイティブ広告を表示 する」を参照してください)。既定では、mainImage と iconImage は読み込まれません。 これらのイメージは、ANNativeAdResponse クラスの mainImageURL プロパティと iconImageURL を使用して手動でダウンロードできます。
例
複数形式の ANBannerAdView クラスの使用は、バナーと従来のネイティブ クラスの使用法の組み合わせにすぎません。
ネイティブまたはバナー、ビデオ、ネイティブの組み合わせをフェッチする場合は、次のコード例を使用して開始できます。
/**
This view displays ads from placements that return banner, video and/or native ads.
Note the implementation requires delaying the point where the ad is displayed,
because native ads are displayed with ANNativeAdResponse and because the type of ad must be
tested after load in order to determine the proper means for display.
*/
@code
ANBannerAdView *banner = nil;
- (void) loadMultiFormatAd
{
CGSize size = CGSizeMake(300, 250); // NOTE Setting size is necessary only for fetching banner and video ad objects.
// This field is ignored when the placement returns a native ad object.
CGRect someRect = CGRectMake(...);
// Create the banner ad view here, but wait until the delegate fires before displaying.
//
ANBannerAdView *banner = [ANBannerAdView adViewWithFrame:someRect placementId:@"<YOUR_PLACEMENT_ID>" adSize:size];
banner.rootViewController = self;
// Load an ad!
[banner loadAd];
}
// Display all multi-format ads in success method from ANBannerAdViewDelegate.
//
- (void) adDidReceiveAd:(id)adObject
{
if ([adObject isKindOfClass:[ANNativeAdResponse class]])
{
ANNativeAdResponse *nativeAdResponse = (ANNativeAdResponse *)ad;
MyNativeView *nativeView = [[MyNativeView alloc] init];
nativeView.title.text = nativeAdResponse.title;
nativeView.text.text = nativeAdResponse.body;
nativeView.iconImageView.image = nativeAdResponse.iconImage;
nativeView.mainImageView.image = nativeAdResponse.mainImage;
[nativeView.callToActionButton setTitle:response.callToAction forState:UIControlStateNormal];
nativeAdResponse.delegate = self;
[nativeAdResponse registerViewForTracking: nativeView
withRootViewController: self
clickableViews: @[nativeView.callToActionButton, nativeView.mainImageView]
error: nil
];
[self.view addSubview:nativeView];
} else {
[self.view addSubview:banner];
}
}
@endcode
If this view will be displaying placements that include only banner and/or video ads,
then display can be (optimistically) handled without using ANBannerAdViewDelegate:
@code
- (void) loadMultiFormatAdThatDoesNotIncludeNative
{
CGSize size = CGSizeMake(300, 250);
// Create the banner ad view and add it as a subview.
//
ANBannerAdView *banner = [ANBannerAdView adViewWithFrame:rect placementId:@"13572468" adSize:size];
banner.rootViewController = self;
[self.view addSubview:banner];
// Load an ad!
// NOTE Upon loadAd failure, the view impression simply remains blank.
//
[banner loadAd];
}
@endcode
ANBannerAdView でバナー広告や動画広告のみを含む配置を表示する場合は、adDidReceiveAd: delegate メソッドを使用せずに表示を (オプティミスティックに) 処理できます。
@implementation MyViewController
- (void) loadMultiFormatAdThatDoesNotIncludeNative
{
CGSize size = CGSizeMake(300, 250);
// Create the banner ad view and add it as a subview.
//
ANBannerAdView *banner = [ANBannerAdView adViewWithFrame:rect placementId:@"13572468" adSize:size];
banner.rootViewController = self;
[self.view addSubview:banner];
// Load an ad!
// NOTE Upon loadAd failure, the view impression simply remains blank.
//
[banner loadAd];
}
@end