SDK8 Mopub Mediation Adapter Interstitial
於 2021年9月10日 (五) 09:24 由 imported>Wikiuser 所做的修訂 (新頁面: 依照Mopub設定教學設定後,開始實做 adaptor<br> 官方教學:[https://developers.mopub.com/networks/integrate/build-adapters-ios/#quick-start-for-fullsc...)
依照Mopub設定教學設定後,開始實做 adaptor
官方教學:連結
- 先建立一 class 繼承 MPFullscreenAdAdapter,class 名稱必須與 Mopub 後台設定內一樣
- 加入#import <TAMediaAdsFramework/TAMediaAdsFramework.h>
- 複寫
-requestAdWithAdapterInfo:(NSDictionary *)info adMarkup:
以及在此 method 內用 TADInterstitial 呼叫廣告
- info 內的 key 值請依照 Mopub 內所設定的 key 值帶入
- 複寫
-presentAdFromViewController:(UIViewController *)viewController
以及在此 method 內用 TADInterstitial的方法-presentFromRootViewController:
顯示廣告 - TADInterstitial 的 delegate 內呼叫 MPFullscreenAdAdapterDelegate 執行對應的 method
可參考以下代碼
TAMediaInterstitialAdapter
#import <MoPub/MoPub.h>
#import <TAMediaAdsFramework/TAMediaAdsFramework.h>
@interface TAMediaInterstitialAdapter: MPFullscreenAdAdapter <MPThirdPartyFullscreenAdAdapter, TADInterstitialDelegate>
@property (nonatomic,strong) TADInterstitial *interstitialView;
@end
#import "TAMediaInterstitialAdapter.h"
@implementation TAMediaInterstitialAdapter
- (void)requestAdWithAdapterInfo:(NSDictionary *)info adMarkup:(NSString *)adMarkup {
TADRequest *request = [TADRequest request];
request.testing = NO;
// Type: TADGender
request.gender = kTADGenderUnknown;
// interstitialView 物件初始化
if (self.interstitialView == nil) {
self.interstitialView = [[TADInterstitial alloc] init];
}
// 必須要設定delegate
self.interstitialView.delegate = self;
// 設定 AD Unit ID
NSString *interstitialId = info[@"InterstitialId"];
self.interstitialView.adUnitID = interstitialId;
self.interstitialView.turnOnMicrophone = NO;
MPLogAdEvent([MPLogEvent adLoadAttemptForAdapter:NSStringFromClass(self.class) dspCreativeId:nil dspName:nil], [self getAdUnitId]);
// 載入廣告
[self.interstitialView loadRequest:request];
}
- (BOOL) isRewardExpected {
return NO;
}
- (BOOL)hasAdAvailable {
return self.interstitialView.isReady;
}
- (BOOL)enableAutomaticImpressionAndClickTracking {
return YES;
}
- (void)presentAdFromViewController:(UIViewController *)rootViewController {
MPLogAdEvent([MPLogEvent adShowAttemptForAdapter:NSStringFromClass(self.class)], [self getAdUnitId]);
[self.interstitialView presentFromRootViewController:rootViewController];
}
#pragma mark - TADInterstitialDelegate
// Interstitial廣告成功載入後呼叫, 可以在此時間點及之後顯示呈現廣告
- (void)interstitialDidReceiveAd:(TADInterstitial *)ad {
MPLogAdEvent([MPLogEvent adLoadSuccessForAdapter:NSStringFromClass(self.class)], [self getAdUnitId]);
if ([self.delegate respondsToSelector:@selector(fullscreenAdAdapterDidLoadAd:)]) {
[self.delegate fullscreenAdAdapterDidLoadAd:self];
}
}
// Interstitial廣告載入失敗時呼叫, 處理錯誤 或 顯示錯誤訊息
- (void)interstitial:(TADInterstitial *)ad didFailToReceiveAdWithError:(TADRequestError *)error {
MPLogAdEvent([MPLogEvent adLoadFailedForAdapter:NSStringFromClass(self.class) error:error], [self getAdUnitId]);
if ([self.delegate respondsToSelector:@selector(fullscreenAdAdapter:didFailToLoadAdWithError:)]) {
[self.delegate fullscreenAdAdapter:self didFailToLoadAdWithError:error];
}
}
// Interstitial廣告呈現之前呼叫, 注意使用者可能會在廣告內按下連結(網頁, AppStore, ...)而因此離開應用程式, 可以在這function中處理該暫停之項目
- (void)interstitialWillPresentScreen:(TADInterstitial *)ad {
MPLogAdEvent([MPLogEvent adWillAppearForAdapter:NSStringFromClass(self.class)], [self getAdUnitId]);
MPLogAdEvent([MPLogEvent adShowSuccessForAdapter:NSStringFromClass(self.class)], [self getAdUnitId]);
MPLogAdEvent([MPLogEvent adDidAppearForAdapter:NSStringFromClass(self.class)], [self getAdUnitId]);
if ([self.delegate respondsToSelector:@selector(fullscreenAdAdapterAdWillAppear:)]) {
[self.delegate fullscreenAdAdapterAdWillAppear:self];
}
if ([self.delegate respondsToSelector:@selector(fullscreenAdAdapterAdDidAppear:)]) {
[self.delegate fullscreenAdAdapterAdDidAppear:self];
}
}
// Interstitial廣告關閉, 在螢幕上消失之前呼叫, 再次提醒在Interstitail消失前, 必須將Interstitial及delegate設為nil (可以在這個function中執行)
- (void)interstitialWillDismissScreen:(TADInterstitial *)ad {
MPLogAdEvent([MPLogEvent adWillDisappearForAdapter:NSStringFromClass(self.class)], [self getAdUnitId]);
if ([self.delegate respondsToSelector:@selector(fullscreenAdAdapterAdWillDismiss:)]) {
[self.delegate fullscreenAdAdapterAdWillDismiss:self];
}
}
// Interstitial廣告關閉, 在螢幕上消失之後呼叫
- (void)interstitialDidDismissScreen:(TADInterstitial *)ad {
MPLogAdEvent([MPLogEvent adDidDisappearForAdapter:NSStringFromClass(self.class)], [self getAdUnitId]);
if ([self.delegate respondsToSelector:@selector(fullscreenAdAdapterAdDidDismiss:)]) {
[self.delegate fullscreenAdAdapterAdDidDismiss:self];
}
}
// 使用者在廣告內按下連結(網頁, AppStore, ...)而因此離開應用程式之前呼叫
- (void)interstitialWillLeaveApplication:(TADInterstitial *)ad {
MPLogAdEvent([MPLogEvent adWillLeaveApplicationForAdapter:NSStringFromClass(self.class)], [self getAdUnitId]);
if ([self.delegate respondsToSelector:@selector(fullscreenAdAdapterWillLeaveApplication:)]) {
[self.delegate fullscreenAdAdapterWillLeaveApplication:self];
}
if ([self.delegate respondsToSelector:@selector(fullscreenAdAdapterDidReceiveTap:)]) {
[self.delegate fullscreenAdAdapterDidReceiveTap:self];
}
}
- (NSString *)getAdUnitId {
return self.interstitialView.adUnitID ? self.interstitialView.adUnitID : @"";
}
- (void)dealloc {
if (self.interstitialView != nil) {
self.interstitialView.delegate = nil;
self.interstitialView = nil;
}
}
@end