# iOS のバナー広告単位 API への移行

> 広告単位 ID を使用してバナー広告をロードおよびディスプレイすることで、LevelPlay バナー API に切り替えます。

このガイドでは、広告単位 ID を使用して LevelPlay API を統合し、バナー広告をロードおよびディスプレイする方法について説明します。

## 前提条件##prerequisites

* サポートされている最小 SDK は 8.6.0 です。最新の SDK は[ここ](/grow/levelplay/sdk/ios/sdk-integration.md)からダウンロードできます。
* LevelPlay 初期化 API を使用して SDK を初期化していることを確認します。
* LevelPlay ダッシュボードで AdUnitID を見つけます。

## バナー広告オブジェクトの作成とサイズの設定##create-banner-ad-object-and-set-size

バナー広告オブジェクトの作成は、**onInitSuccess** コールバックの受信後に行う必要があります。

1. **Objective-C**

   ```objective-c
   // バナービューの作成と広告単位 ID の設定
   self.bannerAdView = [[LPMBannerAdView alloc] initWithAdUnitId:@"adUnitId"];
   ```

2. **Swift**

   ```swift
   // バナービューの作成と広告単位 ID の設定
   self.bannerAdView = LPMBannerAdView(adUnitId: "adUnitId")
   ```

### バナーサイズ##banner-sizes

| **古い機能**                | **広告単位(新規)**                                    | **Dimensions in dp**.                  |
| ----------------------- | ----------------------------------------------- | -------------------------------------- |
| **ISBannerSize**        | **LPMAdSize**                                   | **(幅 X 高さ)**                           |
| ISBannerSize\_BANNER    | bannerSize                                      | 320×50                                 |
| ISBannerSize\_LARGE     | largeSize                                       | 320 x 90                               |
| ISBannerSize\_RECTANGLE | mediumRectangleSize                             | 300 x 250                              |
| ISBannerSize\_SMART     | Adaptive Ad Size (適応型広告サイズ) に置き換えられました (以下を参照)。 | 広告を自動的にレンダリングして、モバイルおよびタブレットのサイズと向きを調整 |

広告サイズを作成するには、以下のいずれかのオプションに従います。 

### 画面幅に合わせて調整できる適応型広告サイズ（推奨）##adaptive-ad-size-that-adjusts-to-the-screen-width-(recommended)

このオプションは、デバイス タイプに応じてバナーまたは LeaderBOARD を返します。適応特徴をサポートするネットワーク (Google、Yandex) は、最適化ロジックに基づいて高ささを返します。

1. **Objective-C**

   ```objective-c
   LPMAdSize *bannerSize = [LPMAdSize createAdaptiveAdSize];
   [self.bannerAdView setAdSize: bannerSize];
   ```

2. **Swift**

   ```swift
   let bannerSize = LPMAdSize.createAdaptive()
   self.bannerAdView.setAdSize(bannerSize)
   ```

\*\*特定のバナーサイズ：\*\*このオプションでは、特定のバナーサイズを設定できます。バナー、LARGE、MEDIUM＿矩形。

1. **Objective-C**

   ```objective-c
   LPMAdSize *bannerSize = [LPMAdSize largeSize];
   [self.bannerAdView setAdSize: bannerSize];
   ```

2. **Swift**

   ```swift
   let bannerSize = LPMAdSize.large()
   self.bannerAdView.setAdSize(bannerSize)
   ```

## Placements##placements

バナーの[プレースメントは](/grow/levelplay/platform/settings/placements.md)、レポートの目的でのみサポートされます。プレースメントは\*\*、loadAd\*\* の前に設定して、リロードされたすべての広告に影響を与える必要があります。

1. **Objective-C**

   ```objective-c
   // 配置名を設定します
   [self.bannerAdView setPlacementName:@"PlacementName"];
   ```

2. **Swift**

   ```swift
   // 配置名を設定します
   self.bannerAdView.setPlacementName("PlacementName")
   ```

## バナーデリゲートの設定##set-banner-delegates

コードに **LevelPlayBannerDelegate の**代わりに **LPMBannerAdViewDelegate** を実装して、広告の配信を通知します。 

* バナー広告をロードする前にデリゲートを設定することをお勧めします。
* バナー広告ビューごとに、独自のデリゲート実装が必要です。
* コールバックはメインスレッドで実行されます。

1. **Objective-C**

   ```objective-c
   // デリゲート実装の設定
   [self.bannerAdView setDelegate:self];
   #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 {
      // Ad failed to be displayed on screen（画面に広告を表示できませんでした）
   }
   - (void)didLeaveAppWithAdInfo:(LPMAdInfo *)adInfo {
      // ユーザーが広告上でクリックされ、アプリケーション外に移動した
   }
   - (void)didExpandAdWithAdInfo:(LPMAdInfo *)adInfo {
      // 広告は全画面で開かれます
   }
   - (void)didCollapseAdWithAdInfo:(LPMAdInfo *)adInfo {
      // 広告が元のサイズに戻されます
   }
   ```

2. **Swift**

   ```swift
   // デリゲート実装の設定
   self.bannerAdView.setDelegate(self)
   func didLoadAd(with adInfo:LPMAdInfo) {
      //広告が正常にロードされました
   }
   func didFailToLoadAd(withAdUnitId adUnitId:文字列、エラー：Error) {
      // 広告ロード失敗
   }
   func didClickAd(with adInfo:LPMAdInfo) {
      // 広告がクリックされました
   }
   func didDisplayAd(with adInfo:LPMAdInfo) {
      // 広告が画面に表示されました
   }
   func didFailToDisplayAd(with adInfo:LPMAdInfo、エラー：Error) {
      // Ad failed to be displayed on screen（画面に広告を表示できませんでした）
   }
   func didLeaveApp(with adInfo:LPMAdInfo) {
      // ユーザーが広告上でクリックされ、アプリケーション外に移動した
   }
   func didExpandAd(with adInfo:LPMAdInfo) {
      // 広告は全画面で開かれます
   }
   func didCollapseAd(with adInfo:LPMAdInfo) {
      // 広告が元のサイズに戻されます
   }
   ```

### LevelPlay 広告情報##levelplay-ad-info

**LPMAdInfo** パラメーターには、ロードされた広告に関する情報が含まれます。

|       | Ironsource 古い機能 API           | 広告単位 API                     |
| ----- | ----------------------------- | ---------------------------- |
| デリゲート | LevelPlayBannerDelegate       | LPMBannerAdViewDelegate      |
| イベント  | didLoad                       | didLoadAdWithAdInfo          |
|       | didFailToLoadWithError        | didFailToLoadAdWithAdUnitId  |
|       | didClickWithAdInfo            | didClickAdWithAdInfo         |
|       | didPresentScreenWithAdInfo    | didExpandAdWithAdInfo        |
|       | didDismissScreenWithAdInfo    | didCollapseAdWithAdInfo      |
|       | didLeaveApplicationWithAdInfo | didLeaveAppWithAdInfo        |
|       | -                             | didDisplayAdWithAdInfo       |
|       | -                             | didFailToDisplayAdWithAdInfo |

## ロードバナー広告##load-banner-ad

バナー広告をロードするには、**Ironsource.loadBanner** の代わりに **loadAd** を使用します。

1. **Objective-C**

   ```objective-c
   // バナー広告のロード
   [self.bannerAdView loadAdWithViewController:self];
   ```

2. **Swift**

   ```swift
   // バナー広告のロード
   self.bannerAdView.loadAd(with: self)
   ```

## バナー更新の一時停止と再開##pause-and-resume-banner-refresh

プラットフォームで更新値が定義されている場合は、コードのバナー更新を一時停止できます。バナー広告の自動更新を停止するか、一時停止後に再度有効にするには、以下の方法を使用します。 

> **Note:**
>
> バナーが再度表示されると、バナーが一時停止されてから更新までの時間が完了します。 

* **pauseAutoRefresh** - バナー広告の自動更新を一時停止します。
* **resumeAutoRefresh** - バナー広告が一時停止された後、自動更新を再開します。

1. **Objective-C**

   ```objective-c
   // 更新一時停止
   [self.bannerAdView pauseAutoRefresh];
   // 更新を再開
   [self.bannerAdView resumeAutoRefresh];
   ```

2. **Swift**

   ```swift
   // 更新一時停止
   self.bannerAdView.pauseAutoRefresh()
   // 更新を再開
   self.bannerAdView.resumeAutoRefresh()
   ```

## バナー広告の破棄##destroy-banner-ad

バナーを破棄するには、 **destroy** メソッドを呼び出します。

破棄されたバナーは再度表示できなくなります。さらに広告を表示するには、新しい **LPMBannerAdView** オブジェクトを作成します。

1. **Objective-C**

   ```objective-c
   [self.bannerAdView destroy];
   ```

2. **Swift**

   ```swift
   self.bannerAdView.destroy()
   ```

## 複数の広告単位バナーAPI##multiple-ad-unit-banner-apis

|          | 古い機能                         | 広告単位 (新規)                |
| -------- | ---------------------------- | ------------------------ |
| バナー広告ビュー | ISBannerView                 | LPMBannerAdView          |
| API      | loadBannerWithViewController | loadAdWithViewController |
|          | loadBannerWithViewController | setPlacementName         |
|          | destroyBanner                | destroy                  |
|          | -                            | LPMAdSize.width          |
|          | -                            | LPMAdSize.height         |
|          | -                            | pauseAutoRefresh         |
|          | -                            | resumeAutoRefresh        |

## バナー広告の完全な実装例##full-implementation-example-of-banner-ads

適応型バナーサイズを使用してバナー広告を作成およびロードする例を示します。

1. **Objective-C**

   ```objective-c
   NS_ASSUME_NONNULL_BEGIN
   @interface BannerAdViewController () <LPMBannerAdViewDelegate>
   @property(nonatomic, strong) LPMBannerAdView *bannerAd;
   @end
   @implementation BannerAdViewController
   - (void)dealloc {
   [self.bannerAd destroy];
   }
   - (void)createBannerAd {
   // バナービューの作成と広告単位 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.推奨 - 画面幅に合わせて調整する適応型広告サイズ
   LPMAdSize *bannerSize = [LPMAdSize createAdaptiveAdSize];
   // 2.固定幅広告サイズを使用した適応型広告サイズ
   // self.bannerSize = [LPMAdSize createAdaptiveAdSizeWithWidth:400];
   // 3.特定のバナーサイズ - 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
   ```

2. **Swift**

   ```swift
   import UIKit
   class BannerAdViewController:UIViewController, LPMBannerAdViewDelegate {
   var bannerAd:LPMBannerAdView!
   deinit {
       self.bannerAd.destroy()
   }
   func createBannerAd() {
       // Create the banner view and set the ad unit id
       self.bannerAd = LPMBannerAdView(adUnitId: "adUnitId")
       
       // Set the placement name (optional)
       self.bannerAd.setPlacementName("PlacementName")
       
       // Set the ad size
       // Null check required when using adaptive ad size
       guard let bannerSize = getBannerSize() else {
           戻り値
       }
       self.bannerAd.setAdSize(bannerSize)
       
       // Set the delegate implementation
       self.bannerAd.setDelegate(self)
       
       // Add the banner view to the view hierarchy with the proper constraints
       addBannerView(withSize: bannerSize)
   }
   func getBannerSize() -> LPMAdSize? {
       // 1.推奨 - 画面幅に合わせて調整する適応型広告サイズ
       let bannerSize = LPMAdSize.createAdaptive()
       // 2.固定幅広告サイズを使用した適応型広告サイズ
       // bannerSize = LPMAdSize.createAdaptiveAdSize(withWidth:400)
       // 3.特定のバナーサイズ - BANNER、LARGE、MEDIUM_RECTangle
       // bannerSize = LPMAdSize.mediumRectangleSize()
       
       return bannerSize
   }
   func loadBannerAd() {
       self.bannerAd.loadAd(with: self)
   }
   func addBannerView(withSize bannerSize:LPMAdSize) {
       DispatchQueue.main.async {
           self.bannerAd.translatesAutoresizingMaskIntoConstraints = false

           // Add the banner view to the view hierarchy
           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.アクティベート([centerX, bottom, width, height])
       }
   }
   // MARK: - LPMBannerAdViewDelegate
   func didLoadAd(with adInfo:LPMAdInfo) {}
   func didFailToLoadAd(withAdUnitId adUnitId:文字列、エラー：Error) {}
   func didClickAd(with adInfo:LPMAdInfo) {}
   func didDisplayAd(with adInfo:LPMAdInfo) {}
   func didFailToDisplayAd(with adInfo:LPMAdInfo、エラー：Error) {}
   func didLeaveApp(with adInfo:LPMAdInfo) {}
   func didExpandAd(with adInfo:LPMAdInfo) {}
   func didCollapseAd(with adInfo:LPMAdInfo) {}
   }
   ```

## LevelPlay Mediation デモアプリケーション##levelplay-mediation-demo-app

インテグレーション Demo (インテグレーションデモ) アプリケーションは、バナー広告単位 API をアプリケーションに統合する方法を示します。

[iOSデモアプリケーションのダウンロード](https://github.com/ironsource-mobile/Mediation-Demo-Apps)
