バナー - 広告ユニットAPIへの移行 - iOS
このガイドでは、現在の実装から LevelPlay バナー API(広告ユニット ID を使用)へ移行し、バナー広告をロード・表示する方法について説明します。
バナー広告オブジェクトの作成とサイズ設定
バナー広告オブジェクトの作成は、onInitSuccess コールバックを受け取った後に行ってください。
// Create ad configuration - optional
LPMBannerAdViewConfigBuilder *adConfigBuilder = [LPMBannerAdViewConfigBuilder new];
[adConfigBuilder setWithAdSize:bannerSize];
[adConfigBuilder setWithPlacementName:@"placementName"];
LPMBannerAdViewConfig *adConfig = [adConfigBuilder build];
// Create the banner view and set the ad unit id
self.bannerAd = [[LPMBannerAdView alloc] initWithAdUnitId:@"adUnitId" config: adConfig];
// Create ad configuration - optional
let adConfig = LPMBannerAdViewConfigBuilder()
.set(adSize: bannerSize)
.set(placementName: "placementName")
.build()
// Create the banner view and set the ad unit id
self.bannerAd = LPMBannerAdView(adUnitId: "adUnitId", config: adConfig)
バナーサイズ
レガシー | 広告ユニット(New) | dp 単位のサイズ |
ISBannerSize | LPMAdSize | (幅 × 高さ) |
ISBannerSize_BANNER | bannerSize | 320 x 50 |
ISBannerSize_LARGE | largeSize | 320 x 90 |
ISBannerSize_RECTANGLE | mediumRectangleSize | 300 x 250 |
ISBannerSize_SMART | Adaptive Ad Size に置き換え(下記参照) | モバイルやタブレットの画面サイズ・向きに自動調整される広告 |
広告サイズの作成方法は以下のいずれかになります:
画面幅に自動対応するアダプティブバナーサイズ(推奨):
このオプションはデバイス種別により BANNER または LEADERBOARD を返します。
アダプティブ機能対応ネットワーク(Google, Yandex等)は最適化ロジックにより高さを返します。
LPMAdSize *bannerSize = [LPMAdSize createAdaptiveAdSize];
[self.bannerAdView setAdSize: bannerSize];
let bannerSize = LPMAdSize.createAdaptive()
self.bannerAdView.setAdSize(bannerSize)
特定バナーサイズの指定:
この方法ではBANNER、LARGE、MEDIUM_RECTANGLEのサイズを明示的に指定できます。
LPMAdSize *bannerSize = [LPMAdSize largeSize];
[self.bannerAdView setAdSize: bannerSize];
let bannerSize = LPMAdSize.large()
self.bannerAdView.setAdSize(bannerSize)
プレースメント
バナー広告はプレースメントに対応しており、レポート用途で利用可能です。loadAd より前に設定することで、全てのリロード広告に適用されます。
// Set the placement name
[self.bannerAdView setPlacementName:@"PlacementName"];
// Set the placement name
self.bannerAdView.setPlacementName("PlacementName")
バナーデリゲートの実装
LevelPlayBannerDelegate の代わりに、LPMBannerAdViewDelegate を実装してください。
- バナー広告のロード前にデリゲートを設定することを推奨します。
- 各バナー広告それぞれにデリゲート実装が必要です。
- コールバックはメインスレッドで実行されます。
// 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 {
// Ad was failed to be displayed on screen
}
- (void)didLeaveAppWithAdInfo:(LPMAdInfo *)adInfo {
// User pressed on the ad and was navigated out of the app
}
- (void)didExpandAdWithAdInfo:(LPMAdInfo *)adInfo {
// Ad is opened on full screen
}
- (void)didCollapseAdWithAdInfo:(LPMAdInfo *)adInfo {
// Ad is restored to its original size
}
// Set delegate implementation
self.bannerAdViewView.setDelegate(self)
func didLoadAd(with adInfo: LPMAdInfo) {
//Ad was loaded successfully
}
func didFailToLoadAd(withAdUnitId adUnitId: String, error: Error) {
// Ad load failed
}
func didClickAd(with adInfo: LPMAdInfo) {
// Ad was clicked
}
func didDisplayAd(with adInfo: LPMAdInfo) {
// Ad was displayed and visible on screen
}
func didFailToDisplayAd(with adInfo: LPMAdInfo, error: Error) {
// Ad was failed to be displayed on screen
}
func didLeaveApp(with adInfo: LPMAdInfo) {
// User pressed on the ad and was navigated out of the app
}
func didExpandAd(with adInfo: LPMAdInfo) {
// Ad is opened on full screen
}
func didCollapseAd(with adInfo: LPMAdInfo) {
// Ad is restored to its original size
}
LevelPlay Ad Info
レガシー API | 広告ユニット API | |
Delegates | LevelPlayBannerDelegate | LPMBannerAdViewDelegate |
Events | didLoad | didLoadAdWithAdInfo |
didFailToLoadWithError | didFailToLoadAdWithAdUnitId | |
didClickWithAdInfo | didClickAdWithAdInfo | |
didPresentScreenWithAdInfo | didExpandAdWithAdInfo | |
didDismissScreenWithAdInfo | didCollapseAdWithAdInfo | |
didLeaveApplicationWithAdInfo | didLeaveAppWithAdInfo | |
– | didDisplayAdWithAdInfo | |
– | didFailToDisplayAdWithAdInfo |
バナー広告のロード
バナー広告のロードには IronSource.loadBanner の代わりに loadAd を使用してください。
// Load the banner ad
[self.bannerAdView loadAdWithViewController:self]
// Load the banner ad
self.bannerAdView.loadAd(with: self)
バナーリフレッシュの一時停止・再開
プラットフォームでリフレッシュ値が定義されている場合、コードでバナーリフレッシュを一時停止できます。バナー広告の自動リフレッシュを停止または再開するには、次のメソッドを利用してください。
- pauseAutoRefresh – バナー広告の自動リフレッシュを一時停止します。
- resumeAutoRefresh – バナー広告の自動リフレッシュを再開します。
// Pause refresh
[self.bannerAdView pauseAutoRefresh];
// Resume refresh
[self.bannerAdView resumeAutoRefresh];
// Pause refresh
self.bannerAdView.pauseAutoRefresh()
// Resume refresh
self.bannerAdView.resumeAutoRefresh()
バナー広告を破棄する
バナーを破棄するには、destroy メソッドを呼び出してください。
破棄したバナーは再度表示できません。もう一度広告を表示したい場合は、新たな LPMBannerAdView オブジェクトを作成してください。
マルチ広告ユニット対応バナー API
レガシー | 広告ユニット(New) | |
Banner Ad View | ISBannerView | LPMBannerAdView |
API | loadBannerWithViewController | loadAdWithViewController |
loadBannerWithViewController | setPlacementName | |
destroyBanner | destroy | |
– | LPMAdSize.width | |
– | LPMAdSize.height | |
– | pauseAutoRefresh | |
– | resumeAutoRefresh |
バナー広告のフル実装例
下記はアダプティブバナーサイズを使ったバナー広告の作成・ロード例です。
NS_ASSUME_NONNULL_BEGIN
@interface BannerAdViewController () <LPMBannerAdViewDelegate>
@property(nonatomic, strong) LPMBannerAdView *bannerAd;
@end
@implementation BannerAdViewController
- (void)dealloc {
[self.bannerAd destroy];
}
- (void)createBannerAd {
LPMAdSize *bannerSize = [self getBannerSize] ?: [LPMAdSize mediumRectangleSize];
// Create ad configuration - optional
LPMBannerAdViewConfigBuilder *adConfigBuilder = [LPMBannerAdViewConfigBuilder new];
[adConfigBuilder setWithAdSize:bannerSize];
[adConfigBuilder setWithPlacementName:@"placementName"];
LPMBannerAdViewConfig *adConfig = [adConfigBuilder build];
// Create the banner view and set the ad unit id
self.bannerAd = [[LPMBannerAdView alloc] initWithAdUnitId:@"adUnitId" config: adConfig];
// 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
import UIKit
public class BannerAdViewController: UIViewController, LPMBannerAdViewDelegate {
var bannerAd: LPMBannerAdView!
deinit {
self.bannerAd.destroy()
}
public func createBannerAd() {
guard let bannerSize = getBannerSize() else {
return
}
// Create ad configuration - optional
let adConfig = LPMBannerAdViewConfigBuilder()
.set(adSize: bannerSize)
.set(placementName: "placementName")
.build()
// Create the banner view and set the ad unit id
self.bannerAd = LPMBannerAdView(adUnitId: "adUnitId", config: adConfig)
// Set the delegate implementation
self.bannerAd.setDelegate(self)
// Add the banner view to the view hierarchy with the proper constraints
addBannerView(withSize: bannerSize)
}
public func getBannerSize() -> LPMAdSize? {
// 1. Recommended - Adaptive ad size that adjusts to the screen width
let bannerSize = LPMAdSize.createAdaptive()
// 2. Adaptive ad size using fixed width ad size
// bannerSize = LPMAdSize.createAdaptiveAdSize(withWidth: 400)
// 3. Specific banner size - BANNER, LARGE, MEDIUM_RECTANGLE
// bannerSize = LPMAdSize.mediumRectangleSize()
return bannerSize
}
public func loadBannerAd() {
self.bannerAd.loadAd(with: self)
}
public func addBannerView(withSize bannerSize: LPMAdSize) {
DispatchQueue.main.async {
self.bannerAd.translatesAutoresizingMaskIntoConstraints = false
self.view.addSubview(self.bannerAd)
let centerX = self.bannerAd.centerXAnchor.constraint(equalTo: self.view.centerXAnchor)
let bottom = self.bannerAd.bottomAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.bottomAnchor)
let width = self.bannerAd.widthAnchor.constraint(equalToConstant: CGFloat(bannerSize.width))
let height = self.bannerAd.heightAnchor.constraint(equalToConstant: CGFloat(bannerSize.height))
NSLayoutConstraint.activate([centerX, bottom, width, height])
}
}
// MARK: - LPMBannerAdViewDelegate
public func didLoadAd(with adInfo: LPMAdInfo) {}
public func didFailToLoadAd(withAdUnitId adUnitId: String, error: Error) {}
public func didClickAd(with adInfo: LPMAdInfo) {}
public func didDisplayAd(with adInfo: LPMAdInfo) {}
public func didFailToDisplayAd(with adInfo: LPMAdInfo, error: Error) {}
public func didLeaveApp(with adInfo: LPMAdInfo) {}
public func didExpandAd(with adInfo: LPMAdInfo) {}
public func didCollapseAd(with adInfo: LPMAdInfo) {}
}
LevelPlay Mediation デモアプリ
デモアプリでバナーの広告ユニット API をアプリに実装する方法を確認できます。
完了!
これで新しいマルチ広告ユニット API を使って、アプリケーション内でバナー広告を配信できるようになりました。