次の方法で共有


iOS カスタム アダプター

カスタム アダプターを使用すると、通常はメディエーションのために、同じデバイスにインストールされている別の SDK に SDK が呼び出されます。 このドキュメントでは、独自のカスタム アダプターを作成するために記述する必要があるコードについて説明します。

注:

ここで説明するプロトコルは、独自の仲介アダプターを実装するために使用したプロトコルです。

バナー

バナーを表示するために別の SDK に仲介呼び出しを行うには、プロトコルを実装する ANCustomAdapterBanner 必要があります。 このプロトコルで指定されるメソッドは 1 つだけです。 requestBannerAdWithSize このメソッドは、仲介された SDK をインスタンス化し、それを使用して広告要求を行います。 仲介された SDK から、 などの didLoadBannerAdデリゲート メソッドを呼び出してイベントを発生させる必要があります。 詳細については、以下の例を参照してください。

requestBannerWithAdSize は、次の引数を受け取ります。

  • size: 仲介された広告ビューのサイズ。
  • rootViewController: 仲介された広告ビューのコントローラー。
  • parameterString: サーバーから渡される省略可能な不透明な文字列。これを使用して、追加のターゲット情報などの SDK 固有のパラメーターを定義できます。 この文字列の内容のエンコードは、完全にサードパーティの SDK アダプターの実装にかかります。 つまり、パラメーターには、サード パーティ製 SDK の広告サーバーによって認識される値が設定されている必要があります。
  • idString: サード パーティ SDK の広告サーバーで認識される広告ユニットの ID。 これは、広告主がモバイルアプリで広告を表示できる特定の場所を指します。 この ID は、モバイル SDK に対して不透明です。ID の内容とそのエンコードは、サード パーティ製 SDK の実装にかかります。
  • targetingParameters: 仲介された SDK に送信する年齢、性別、場所などの広告ターゲティング パラメーター。 理解できる仲介された SDK パラメーターを送信する責任があります。

架空の "Godzilla" SDK を仲介してバナー広告を表示するアダプターの例を次に示します。 この例のコールバックの詳細については、以下の 「必須コールバック 」および 「省略可能なコールバック」 セクションを参照してください。

@implementation GodzillaBannerAdaptor
@synthesize delegate;
#pragma mark GodzillaCustomAdapterBanner
- (void)requestBannerAdWithSize:(CGSize)size
             rootViewController:(UIViewController *)rootViewController
                serverParameter:(NSString *)parameterString
                       adUnitId:(NSString *)idString
            targetingParameters:(ANTargetingParameters *)targetingParameters
{
    // Here is where you are responsible for (1) instantiating the mediated SDK,
    // (2) building a request object using the elements of `targetingParameters`.
    // Note that you are responsible for ensuring that the targeting
    // parameters can be understood by the mediated SDK.  We also assume that (3)
    // you create a view in the mediated SDK that you can use to make the ad request.
    // Next, we tell the mediated SDK's ad view to request a banner ad from their ad
    // server.  If it works, we call the `didLoadBannerAd` method on our delegate to
    // notify it that we've successfully loaded an ad.
    [self.gzAdView getAdWithRequest:request onCompletion:^(BOOL success, NSError *error) {
        if (success) {
            [self.delegate didLoadBannerAd:self.gzAdView];
        } else {
    // If the mediated SDK was not able to load an ad successfully, we are responsible 
    // for translating the mediated SDK's error codes into something that we can 
    // understand and report to our delegate using `didFailToLoadAd`.
            ANAdResponseCode code = ANAdResponseInternalError;
            switch (error.code) {
                case GodzillaAdUnknownError:
                    code = ANAdResponseInternalError;
                    break;
                case GodzillaAdServerError:
                    code = ANAdResponseNetworkError;
                    break;
                case GodzillaAdUnavailable:
                    code = ANAdResponseUnableToFill;
                    break;
                case GodzillaAdDisabled:
                    code = ANAdResponseInvalidRequest;
                    break;
                default:
                    code = ANAdResponseInternalError;
                    break;
            }
            [self.delegate didFailToLoadAd:code];
        }
    }];
}
#pragma mark GodzillaBannerDelegate
// Implement the required callbacks that allow the mediated SDK to
// communicate with ours.
- (void)GodzillaAdWillPresent:(GodzillaBanner *)bannerAd {
    [self.delegate willPresentAd];
}
- (void)GodzillaAdDidPresent:(GodzillaBanner *)bannerAd {
    [self.delegate didPresentAd];
}
- (void)GodzillaAdWillDismiss:(GodzillaBanner *)bannerAd {
    [self.delegate willCloseAd];
}
- (void)GodzillaAdDidDismiss:(GodzillaBanner *)bannerAd {
    [self.delegate didCloseAd];
}
- (void)GodzillaAdDidInteract:(GodzillaBanner *)bannerAd {
    [self.delegate adWasClicked];
}
- (void)GodzillaAdWillLeaveApplication:(GodzillaBanner *)bannerAd {
    [self.delegate willLeaveApplication];
}

スポット

別の SDK に仲介呼び出しを行ってスポットを表示するには、プロトコルを実装する ANCustomAdapterInterstitial 必要があります。 このプロトコルでは、次の 3 つの方法が指定されています。

  • requestInterstitialAdWithParameter: 仲介された SDK にスポット広告を要求します。 次の 3 つの引数を受け取ります。
    • parameterString: サーバーから渡される省略可能な不透明な文字列。これを使用して、追加のターゲット情報などの SDK 固有のパラメーターを定義できます。 この文字列の内容のエンコードは、完全にサードパーティの SDK アダプターの実装にかかります。 つまり、パラメーターには、サード パーティ製 SDK の広告サーバーによって認識される値が設定されている必要があります。
    • idString: サード パーティ SDK の広告サーバーで認識される広告ユニットの ID。 これは、広告主がモバイルアプリで広告を表示できる特定の場所を指します。 この ID は、モバイル SDK に対して不透明です。ID の内容とそのエンコードは、サード パーティ製 SDK の実装にかかります。
    • targetingParameters: 仲介された SDK に送信する年齢、性別、場所などの広告ターゲティング パラメーター。 理解できる仲介された SDK パラメーターを送信する責任があります。
  • isReady: スポット広告が読み込まれ、ユーザーに表示する準備ができているかどうか。
  • presentFromViewController: スポット広告をユーザーに表示します。 動作できるかどうかを確認するためのチェックをisReady含める必要があります。 1 つの引数 を viewController受け取ります。これは、スポットを表示するために使用されるビュー コントローラーです。

このコード例では、架空の "Godzilla" SDK を仲介し、スポット広告を表示させます。

@implementation GodzillaInterstitialAdaptor
@synthesize delegate;
#pragma mark GodzillaCustomAdapterInterstitial
- (void)requestInterstitialAdWithParameter:(NSString *)parameterString
                                  adUnitId:(NSString *)idString
                       targetingParameters:(ANTargetingParameters *)targetingParameters
{
    // Here is where you are responsible for (1) instantiating the mediated SDK,
    // (2) building a request object using the elements of `targetingParameters`.
    // Note that you are responsible for ensuring that the targeting
    // parameters can be understood by the mediated SDK.  We also assume that (3)
    // you create a view in the mediated SDK that you can use to make the ad request.
    // Next, we tell the mediated SDK's ad view to request an interstitial ad from their
    // ad server.  If it works, we call the `didLoadInterstitialAd` method on our 
    // delegate to notify it that we've successfully loaded the ad.
    [GodzillaInterstitial fetchWithRequest:request
                          UUID:idString
                          onCompletion:^(BOOL success, NSError *error) {
                          if (success) {
                            [self.delegate didLoadInterstitialAd:self];
                          }
                          else {
    // If the mediated SDK was not able to load an ad successfully, we are responsible 
    // for translating the mediated SDK's error codes into something that we can 
    // understand and report to our delegate using `didFailToLoadAd`.
    ANAdResponseCode code = ANAdResponseInternalError;
    switch (error.code) {
                    case GodzillaAdUnknownError:
                        code = ANAdResponseInternalError;
                        break;
                    case GodzillaAdServerError:
                        code = ANAdResponseNetworkError;
                        break;
                    case GodzillaAdUnavailable:
                        code = ANAdResponseUnableToFill;
                        break;
                    case GodzillaAdDisabled:
                        code = ANAdResponseInvalidRequest;
                        break;
                    default:
                        code = ANAdResponseInternalError;
                        break;
                }
                [self.delegate didFailToLoadAd:code];
            }
        }];
}
- (void)presentFromViewController:(UIViewController *)viewController {
    // First, we check if the ad is not ready to show.  If true, we
    // fire the `failedToDisplayAd` event and exit.
    if (![self isReady]) {
        [self.delegate failedToDisplayAd];
        return;
    }
    // At this point, we know that an ad is ready.  We tell the
    // mediated interstitial view to show the ad, and if it fails,
    // we fire the `failedToDisplayAd` method.
    [GodzillaInterstitial displayfromViewController:viewController
                          onCompletion:^(BOOL success, NSError *error) {
                          if (!success) {
                              NSLog(@"Godzilla interstitial call to display ad failed");
                              [self.delegate failedToDisplayAd];
                          }
                      }];
}
- (BOOL)isReady {
    // Check whether the mediated SDK has an ad available to show.
    return [GodzillaInterstitial isReady];
}
#pragma mark GodzillaInterstitialDelegate
// Implement the required callbacks that allow the mediated SDK to
// communicate with ours.
- (void)GodzillaAdWillPresent:(GodzillaInterstitial *)interstitialAd {
    [self.delegate willPresentAd];
}
- (void)GodzillaAdDidPresent:(GodzillaInterstitial *)interstitialAd {
    [self.delegate didPresentAd];
}
- (void)GodzillaAdWillDismiss:(GodzillaInterstitial *)interstitialAd {
    [self.delegate willCloseAd];
}
- (void)GodzillaAdDidDismiss:(GodzillaInterstitial *)interstitialAd {
    [self.delegate didCloseAd];
}
- (void)GodzillaAdDidInteract:(GodzillaInterstitial *)interstitialAd {
    [self.delegate adWasClicked];
}
- (void)GodzillaAdWillLeaveApplication:(GodzillaInterstitial *)interstitialAd {
    [self.delegate willLeaveApplication];
}

必要なコールバック

次に示すコールバックは、カスタム アダプターに実装する必要があります。 使用例については、このページの他のコード サンプルを参照してください。

広告リクエストが成功したことを示すには、広告がバナーかスポットかに応じて、次のいずれかを呼び出します。

  • - (void)didLoadBannerAd:(UIView *)view: 仲介された SDK の広告要求が完了し、バナーが正常に返されました。
  • - (void)didLoadInterstitialAd:(id<ANCustomAdapterInterstitial>)adaptor: 仲介された SDK の広告リクエストが完了し、スポットが返されました。 現在のアダプターへの参照を渡します。

広告がバナーかスポットかを問わず、広告リクエストが失敗したことを示すには、次のように呼び出します。

  • - (void)didFailToLoadAd:(ANAdResponseCode)errorCode: 仲介された SDK の広告要求が有効な広告を返できませんでした。

さらに、スポットアダプターの場合、show() の試行に失敗した場合は、次を呼び出す必要があります。

  • - (void)failedToDisplayAd: 仲介された SDK はスポットを提示するための呼び出しを受け取ったが、広告が利用できなかったか、SDK が表示されませんでした。

省略可能なコールバック

次に示すコールバックは、バナーとスポットの両方のカスタム アダプターに実装する必要があります。 厳密には必須ではないことに注意してください。

  • - (void)adWasClicked: 仲介された SDK によって表示されている間に、ユーザーによって広告がクリックされました。
  • - (void)willPresentAd: ユーザーが広告と対話し、仲介された SDK でモーダル ビューが表示されます。
  • - (void)didPresentAd: 仲介された SDK によってモーダル ビューが表示されました。
  • - (void)willCloseAd: 仲介された SDK がモーダル ビューを閉じようとしています。
  • - (void)didCloseAd: 仲介された SDK はモーダル ビューを閉じます。
  • - (void)willLeaveApplication: 仲介された SDK によって外部アプリケーションが起動されます。