LoginSignup
4
4

More than 5 years have passed since last update.

Admobメディエーションに対応していないアドネットワークを対応させる方法

Last updated at Posted at 2015-04-21

いかがお過ごしでしょうか、アドモブ日和ですね。
さて、残念ながらAdmobメディエーションには対応していないアドネットワークが世の中にはあります。
それをadmobのメディエーションに対応させるのがCustomEventです。

CustomEventでは、通常実装されたAdMobメディエーションに一つクラスを追加するだけで非対応のアドネットワークをメディエーションに追加できます。

今回はアイモバイルのインタースティシャル広告を実装してみます。

手順は以下の通りです。
1.admobの管理画面からカスタムイベントを作成する。
詳しくは->https://support.google.com/admob/answer/3083407?hl=ja
※クラス名にはGADCustomEventInterstitialプロトコルを実装する予定のクラス名を書いてください(今回だとAdmobCustomEvent)
※パラメータにはカスタム対応するアドネットワークの初期化に必要なパラメータを","区切りで設定しておきましょう

  1. GADCustomEventInterstitialプロトコルを実装したクラスを作る
AdmobCustomEvent.h
#import <ImobileSdkAds/ImobileSdkAds.h>
#import "GADCustomEventInterstitial.h"

@interface AdmobCustomEvent : NSObject<GADCustomEventInterstitial, IMobileSdkAdsDelegate>

// アイモバイルのイベント
- (void)imobileSdkAdsSpot: (NSString*)spotId didReadyWithValue: (ImobileSdkAdsReadyResult)value;
- (void)imobileSdkAdsSpot: (NSString*)spotId didFailWithValue: (ImobileSdkAdsFailResult)value;
- (void)imobileSdkAdsSpotIsNotReady: (NSString*)spotId;
- (void)imobileSdkAdsSpotDidClick: (NSString*)spotId;
- (void)imobileSdkAdsSpotDidClose: (NSString*)spotId;
- (void)imobileSdkAdsSpotDidShow: (NSString*)spotId;

// インタースティシャルのカスタムイベント
- (void)requestInterstitialAdWithParameter: (NSString*)serverParameter label: (NSString*)serverLabel request: (GADCustomEventRequest*)request;
- (void)presentFromRootViewController: (UIViewController*)rootViewController;

@property(nonatomic, retain) NSString* spotId;// 代入したら解放されないようにretainのプロパティにする
@property(nonatomic, assign) id<GADCustomEventInterstitialDelegate> delegate;
@end



※必ず次のコードが必要です

必須コード

// アドモブのサーバーから直接このdelegateにGADCustomEventInterstitialDelegateオブジェクトが代入される
// のでこのプロパティは実装していなければならない
@property(nonatomic, assign) id<GADCustomEventInterstitialDelegate> delegate;

// インタースティシャルのカスタムイベント
- (void)requestInterstitialAdWithParameter: (NSString*)serverParameter label: (NSString*)serverLabel request: (GADCustomEventRequest*)request;
- (void)presentFromRootViewController: (UIViewController*)rootViewController;

3.それに合わせて実装ファイルも実装しましょう。

AdmobCustomEvent.m
#import "AdmobCustomEvent.h"

- (void)requestInterstitialAdWithParameter: (NSString*)serverParameter label: (NSString*)serverLabel request: (GADCustomEventRequest*)request
{

    // パラメータは"publisherId,mediaId,spotId"としています

    // パラメータを取得する
    NSArray* parameters = [serverParameter componentsSeparatedByString: @","];

    // 各IDの取得
    NSString* pubId   = parameters[0];
    NSString* mediaId =  parameters[1];
    NSString* spot    = parameters[2];

    // 代入
    self.spotId = spot;

    // SDKに対して枠の設定を行う
    [ImobileSdkAds registerWithPublisherID: pubId MediaID: mediaId SpotID: spot];
    [ImobileSdkAds setSpotDelegate: spot delegate: self];
    [ImobileSdkAds startBySpotID: spot];
}

- (void)presentFromRootViewController: (UIViewController*)rootViewController
{
    [ImobileSdkAds showBySpotID: self.spotId];
}

- (void)imobileSdkAdsSpot: (NSString*)spotId didReadyWithValue: (ImobileSdkAdsReadyResult)value
{
    [self.delegate customEventInterstitial: self didReceiveAd: nil];
}

- (void)imobileSdkAdsSpot: (NSString*)spotId didFailWithValue: (ImobileSdkAdsFailResult)value
{
    [self.delegate customEventInterstitial: self didFailAd: nil];
}

- (void)imobileSdkAdsSpotIsNotReady: (NSString*)spotId{}

- (void)imobileSdkAdsSpotDidClick: (NSString*)spotId
{
    [self.delegate customEventInterstitialWillLeaveApplication: self];
}
- (void)imobileSdkAdsSpotDidClose: (NSString*)spotId
{
    [self.delegate customEventInterstitialWillDismiss: self];
    [self.delegate customEventInterstitialDidDismiss: self];
}

- (void)imobileSdkAdsSpotDidShow: (NSString*)spotId
{
    [self.delegate customEventInterstitialWillPresent: self];
}

ポイントは追加するアドのSDKのdelegateとGADCustomEventInterstitialDelegateの6つのメソッドを必ず繋げているという点です。6つのメソッドは以下の部分です

    [self.delegate customEventInterstitial: self didReceiveAd: nil];
    [self.delegate customEventInterstitial: self didFailAd: nil];
    [self.delegate customEventInterstitialWillLeaveApplication: self];
    [self.delegate customEventInterstitialWillDismiss: self];
    [self.delegate customEventInterstitialDidDismiss: self];
    [self.delegate customEventInterstitialDidDismiss: self];

あとはすでに実装してあるインタースティシャルのメディエーションがあればそこにこのクラスを追加するだけで勝手にカスタムイベントが呼ばれるようになります。クライアントのコードを無駄に増やすことなく実行できるという点がいいですね。

なお、使用したadmob Sdkは6.12.2です。最新の7系だとimportするものが少し異なるかもしれませんのでそこらへんは適宜変更してください

4
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
4