iOS のバナー実装
バナーは、静的またはアニメーション化できる長方形のシステム主導広告であり、ライブアプリコンテンツの周囲の指定された領域に配信されます。
バナー広告オブジェクトを作成し、サイズを設定する
バナー広告ユニットを作成し、バナーサイズを設定します。バナーオブジェクトの作成は、onInitSuccess を受信した後に行う必要があります。
// Create the banner view and set the ad unit id
self.bannerAdView = [[LPMBannerAdView alloc] initWithAdUnitId:@"adUnitId"];
バナーサイズ
LPMAdSize | Description | Dimensions in dp (Width X Height) |
bannerSize | Standard banner | 320 x 50 |
largeSize | Large banner | 320 x 90 |
mediumRectangleSize | Medium Rectangular (MREC) | 300 x 250 |
Adaptive | 広告を自動的にレンダリングして、モバイルやタブレット向けにサイズと方向を調整します | デバイスの幅 X 推奨される高さ |
次のいずれかのオプションに従って広告サイズを作成します:
画面の幅に合わせて調整されるアダプティブ広告サイズ (推奨):
このオプションは、デバイスタイプに応じて BANNER または LEADERBOARD を返します。
アダプティブ機能をサポートするネットワーク (Google、Yandex) は、最適化ロジックに基づいて高さを返します。
LPMAdSize *bannerSize = [LPMAdSize createAdaptiveAdSize];
[self.bannerAdView setAdSize: bannerSize];
特定のバナーサイズ:
このオプションを使用すると、バナーサイズ (BANNER、LARGE、MEDIUM_RECTANGLE) を指定できます。
LPMAdSize *bannerSize = [LPMAdSize largeSize];
[self.bannerAdView setAdSize: bannerSize];
Placement
プレースメントはレポートに使用されるオプションです。すべてのリロードに設定を反映させるには、ロード前に設定する必要があります。
[self.bannerAdView setPlacementName:@"PlacementName"];
バナーデリゲートを実装する
LPMBannerAdViewDelegate を実装します。
- バナー広告のロード前にデリゲートを設定することをお勧めします。
- 各バナー広告にデリゲート実装が必要であることに注意してください。
- コールバックはメインスレッドで実行されます。
- すべてのネットワークでサポートされていないコールバックは、Optional(任意)とマークされています。
// Set delegate implementation
[self.bannerAdView setDelegate:self]
#pragma mark - LPMBannerAdViewDelegate
- (void)didLoadAdWithAdInfo:(nonnull LPMAdInfo *)adInfo {
//Ad was loaded successfully
}
- (void)didFailToLoadAdWithAdUnitId:(nonnull NSString *)adUnitId error:(nonnull NSError *)error {
// Ad load failed
}
- (void)didClickAdWithAdInfo:(LPMAdInfo *)adInfo {
// Ad was clicked
}
- (void)didDisplayAdWithAdInfo:(LPMAdInfo *)adInfo {
// Ad was displayed and visible on screen
}
- (void)didFailToDisplayAdWithAdInfo:(LPMAdInfo *)adInfo error:(NSError *)error {
// Optional. Ad was failed to be displayed on screen
}
- (void)didLeaveAppWithAdInfo:(LPMAdInfo *)adInfo {
// Optional. User pressed on the ad and was navigated out of the app
}
- (void)didExpandAdWithAdInfo:(LPMAdInfo *)adInfo {
// Optional. Ad is opened on full screen
}
- (void)didCollapseAdWithAdInfo:(LPMAdInfo *)adInfo {
// Optional. Ad is restored to its original size
}
LevelPlay Ad Info
LevelPlay Ad Info の実装と利用可能なフィールドの詳細については、こちらをご覧ください。
バナー広告をロードする
バナー広告をロードするには、LoadAd メソッドを呼びます。
バナーの更新を一時停止および再開する
プラットフォームでリフレッシュレートが定義されている場合は、コードでバナーのリフレッシュを一時停止できます。バナー広告の自動リフレッシュを一時停止・再開するには、次の方法を使用します。
- pauseAutoRefresh – バナーの自動リフレッシュを一時停止
- resumeAutoRefresh – 一時停止されたバナーの自動リフレッシュを再開
// Pause refresh
[self.bannerAdView pauseAutoRefresh];
// Resume refresh
[self.bannerAdView resumeAutoRefresh];
バナー広告を破棄する
バナーを破棄するには、destroy メソッドを呼びます。破棄されたバナーはロードできなくなります。もう一度提供したい場合は、新しい LPMBannerAdView オブジェクトを作成する必要があります。
[self.bannerAdView destroy];
広告の作成とロードの実装例
以下に、アダプティブバナーサイズを使用してバナー広告を作成してロードする例を紹介します。
NS_ASSUME_NONNULL_BEGIN
@interface BannerAdViewController () <LPMBannerAdViewDelegate>
@property(nonatomic, strong) LPMBannerAdView *bannerAd;
@end
@implementation BannerAdViewController
- (void)dealloc {
[self.bannerAd destroy];
}
- (void)createBannerAd {
// Create the banner view and set the ad unit id
self.bannerAd = [[LPMBannerAdView alloc] initWithAdUnitId:@"adUnitId"];
// Set the placement name (optional)
[self.bannerAd setPlacementName:@"PlacementName"];
// Set the ad size
LPMAdSize *bannerSize = [self getBannerSize];
// Required when using adaptive ad size
if (bannerSize == nil) {
return;
}
[self.bannerAd setAdSize:bannerSize];
// Set the delegate implementation
[self.bannerAd setDelegate:self];
// Add the banner view to the view hierarchy with the proper constraints
[self addBannerViewWithSize:bannerSize];
}
- (LPMAdSize *)getBannerSize {
// 1. Recommended - Adaptive ad size that adjusts to the screen width
LPMAdSize *bannerSize = [LPMAdSize createAdaptiveAdSize];
// 2. Adaptive ad size using fixed width ad size
// self.bannerSize = [LPMAdSize createAdaptiveAdSizeWithWidth:400];
// 3. Specific banner size - BANNER, LARGE, MEDIUM_RECTANGLE
// self.bannerSize = [LPMAdSize mediumRectangleSize];
return bannerSize;
}
- (void)loadBannerAd {
[self.bannerAd loadAdWithViewController:self];
}
- (void)addBannerViewWithSize:(LPMAdSize *)bannerSize {
self.bannerAd.translatesAutoresizingMaskIntoConstraints = NO;
// Add the banner view to the view hierarchy
[self.view addSubview:self.bannerAd];
[NSLayoutConstraint activateConstraints:@[
[self.bannerAd.bottomAnchor constraintEqualToAnchor:self.view.safeAreaLayoutGuide.bottomAnchor],
[self.bannerAd.centerXAnchor constraintEqualToAnchor:self.view.centerXAnchor],
[self.bannerAd.widthAnchor constraintEqualToConstant:bannerSize.width],
[self.bannerAd.heightAnchor constraintEqualToConstant:bannerSize.height]
]];
}
#pragma mark - LPMBannerAdViewDelegate
- (void)didLoadAdWithAdInfo:(nonnull LPMAdInfo *)adInfo {}
- (void)didFailToLoadAdWithAdUnitId:(nonnull NSString *)adUnitId error:(nonnull NSError *)error {}
- (void)didClickAdWithAdInfo:(LPMAdInfo *)adInfo {}
- (void)didDisplayAdWithAdInfo:(LPMAdInfo *)adInfo {}
- (void)didFailToDisplayAdWithAdInfo:(LPMAdInfo *)adInfo error:(NSError *)error {}
- (void)didLeaveAppWithAdInfo:(LPMAdInfo *)adInfo {}
- (void)didExpandAdWithAdInfo:(LPMAdInfo *)adInfo {}
- (void)didCollapseAdWithAdInfo:(LPMAdInfo *)adInfo {}
@end
NS_ASSUME_NONNULL_END
LevelPlay メディエーションデモアプリ
実装デモアプリでは、バナー広告ユニット API をアプリに実装する方法を確認できます。
完了!
これで、アプリケーションでバナーを配信するように設定されました。Test Suite を使用してインテグレーションを確認します。
次のステップは?
実装ガイドに従って、追加の広告ユニットを実装します: