檢視 SDK 8 Mopub Mediation 的原始碼
←
SDK 8 Mopub Mediation
跳至導覽
跳至搜尋
由於下列原因,您沒有權限進行編輯此頁面的動作:
您請求的操作只有這個群組的使用者能使用:
使用者
您可以檢視並複製此頁面的原始碼。
<!-- Config --> Mopub SDK v5.5.0+ 都需要實做 adaptor configuration 考參考官網教學 [https://developers.mopub.com/networks/integrate/build-adapters-ios/#quick-start-for-fullscreen-ads 連結]<br> 直接將 TAMediaAdapterConfiguratioin .h.m 加入專案內 [[MoPub Mediation Setting|回MoPub教學]] <source> #import <Foundation/Foundation.h> #import <MoPub/MoPub.h> NS_ASSUME_NONNULL_BEGIN @interface TAMediaAdapterConfiguration : MPBaseAdapterConfiguration + (void)updateInitializationParameters:(NSDictionary *)parameters; - (void)initializeNetworkWithConfiguration:(NSDictionary<NSString *, id> * _Nullable)configuration complete:(void(^ _Nullable)(NSError * _Nullable))complete; @end NS_ASSUME_NONNULL_END </source> <source> #import "TAMediaAdapterConfiguration.h" #import <TAMediaAdsFramework/TAMediaAdsFramework.h> #import <MoPub/MoPub.h> static NSString *tamediaAdapterVersion = @"1.0.0"; @implementation TAMediaAdapterConfiguration + (void)updateInitializationParameters:(NSDictionary *)parameters { } - (NSString *)adapterVersion { return tamediaAdapterVersion; } - (NSString *)biddingToken { return nil; } - (NSString *)moPubNetworkName { return @"tamedia"; } - (NSString *)networkSdkVersion { return [TADMobileAds getSDKVersion]; } - (void)initializeNetworkWithConfiguration:(NSDictionary<NSString *, id> * _Nullable)configuration complete:(void(^ _Nullable)(NSError * _Nullable))complete { [TADMobileAds config]; MPLogInfo(@"TAmedia SDK init version:%@",[TADMobileAds getSDKVersion]); complete(nil); } @end </source> <!-- Banner --> 依照 [[Mopub Setting|Mopub設定教學]] 設定後,開始實做 Adapter <br> 官方教學:[https://developers.mopub.com/networks/integrate/build-adapters-ios/#quick-start-for-inline-ads 連結]<br> #先建立一 class 繼承 MPInlineAdAdapter,class 名稱必須與 Mopub 後台設定內一樣<br> #加入 #import <TAMediaAdsFramework/TAMediaAdsFramework.h> <br> #複寫 <code>- (void)requestAdWithSize:(CGSize) adapterInfo: adMarkup:</code>以及在此 method 內用 TADBannerView 呼叫廣告<br> #:info 內的 key 值請依照 Mopub 內所設定的 key 值帶入 #在 TADBannerView delegate 內呼叫 MPInlineAdAdapterDelegate 執行相對應的 method<br> 可參考以下代碼,或直接下載 adapter 檔直接加入專案中 [[MoPub Mediation Setting|回MoPub教學]] == TAMediaInlineAdAdaptor == #import <MoPub/MoPub.h> #import <TAMediaAdsFramework/TAMediaAdsFramework.h> @interface TAMediaInlineAdAdaptor : MPInlineAdAdapter<MPThirdPartyInlineAdAdapter, TADBannerViewDelegate> @property (nonatomic, strong) TADBannerView *bannerView; @end <source> #import "TAMediaInlineAdAdapter.h" @implementation TAMediaInlineAdAdapter - (void)requestAdWithSize:(CGSize)size adapterInfo:(NSDictionary *)info adMarkup:(NSString *)adMarkup { TADRequest *request = [TADRequest request]; request.testing = NO; request.gender = kTADGenderUnknown; CGFloat adRatio = floor((size.width/size.height)*100)/100; CGFloat ta1200x627AdRatio = floor((1200.0/627)*100)/100; NSString *bannerId = @""; ///根據 ad size 回傳不同 size 的 TAmedia Banner if (size.width == 300 && size.height == 250) { self.bannerView = [[TADBannerView alloc] initWithAdSize:kTADAdSizeMediumRectangle]; bannerId = info[@"banner300x250"]; } else if (size.width == 320 && size.height == 50) { self.bannerView = [[TADBannerView alloc] initWithAdSize:kTADAdSizeBanner]; bannerId = info[@"banner320x50"]; } else if (adRatio == ta1200x627AdRatio) { self.bannerView = [[TADBannerView alloc] initWithAdSize:TADAdSize1200x627RatioBannerWithWidth(size.width)]; bannerId = info[@"banner1200x627"]; } else { NSError *mopubError = [NSError errorWithCode:MOPUBErrorAdapterInvalid localizedDescription:@"TAmedia banner failed to load due to invalid ad size."]; MPLogAdEvent([MPLogEvent adLoadFailedForAdapter:NSStringFromClass(self.class) error:mopubError], [self getAdUnitId]); [self.delegate inlineAdAdapter:self didFailToLoadAdWithError:mopubError]; return; } self.bannerView.delegate = self; self.bannerView.turnOnMicrophone = NO; self.bannerView.adUnitID = bannerId; self.bannerView.rootViewController = [self.delegate inlineAdAdapterViewControllerForPresentingModalView:self]; MPLogAdEvent([MPLogEvent adLoadAttemptForAdapter:NSStringFromClass(self.class) dspCreativeId:nil dspName:nil], [self getAdUnitId]); [self.bannerView loadRequest:request]; } #pragma mark - [Tamedia Banner delegate] - (void)adViewDidReceiveAd:(TADBannerView *)view { MPLogAdEvent([MPLogEvent adLoadSuccessForAdapter:NSStringFromClass(self.class)], [self getAdUnitId]); MPLogAdEvent([MPLogEvent adShowAttemptForAdapter:NSStringFromClass(self.class)], [self getAdUnitId]); if ([self.delegate respondsToSelector:@selector(inlineAdAdapter:didLoadAdWithAdView:)]) { [self.delegate inlineAdAdapter:self didLoadAdWithAdView:view]; } } - (void)adView:(TADBannerView *)view didFailToReceiveAdWithError:(TADRequestError *)error { NSString *failureReason = [NSString stringWithFormat: @"TAmedia Banner failed to load with error: %@", [error localizedDescription]]; NSError *mopubError = [NSError errorWithCode:MOPUBErrorAdapterInvalid localizedDescription:failureReason]; MPLogAdEvent([MPLogEvent adLoadFailedForAdapter:NSStringFromClass(self.class) error:mopubError], [self getAdUnitId]); if ([self.delegate respondsToSelector:@selector(inlineAdAdapter:didFailToLoadAdWithError:)]) { [self.delegate inlineAdAdapter:self didFailToLoadAdWithError:error]; } } - (void)adViewWillPresentScreen:(TADBannerView *)adView { } - (void)adViewWillDismissScreen:(TADBannerView *)adView { } - (void)adViewDidDismissScreen:(TADBannerView *)adView { } - (void)adViewWillLeaveApplication:(TADBannerView *)adView { MPLogAdEvent([MPLogEvent adWillLeaveApplicationForAdapter:NSStringFromClass(self.class)], [self getAdUnitId]); if ([self.delegate respondsToSelector:@selector(inlineAdAdapterWillLeaveApplication:)]) { [self.delegate inlineAdAdapterWillLeaveApplication:self]; } } - (NSString *)getAdUnitId { return self.bannerView.adUnitID ? self.bannerView.adUnitID : @""; } - (void)dealloc { if (self.bannerView != nil) { self.bannerView.delegate = nil; self.bannerView = nil; } } @end </source> <!-- Interstitial --> 依照[[Mopub Setting|Mopub設定教學]]設定後,開始實做 adaptor<br> 官方教學:[https://developers.mopub.com/networks/integrate/build-adapters-ios/#quick-start-for-fullscreen-ads 連結]<br> #先建立一 class 繼承 MPFullscreenAdAdapter,class 名稱必須與 Mopub 後台設定內一樣<br> #加入#import <TAMediaAdsFramework/TAMediaAdsFramework.h><br> #複寫<code>-requestAdWithAdapterInfo:(NSDictionary *)info adMarkup:</code>以及在此 method 內用 TADInterstitial 呼叫廣告<br> #:info 內的 key 值請依照 Mopub 內所設定的 key 值帶入 #複寫<code>-presentAdFromViewController:(UIViewController *)viewController</code>以及在此 method 內用 TADInterstitial的方法 <code>-presentFromRootViewController:</code>顯示廣告<br> #TADInterstitial 的 delegate 內呼叫 MPFullscreenAdAdapterDelegate 執行對應的 method<br> 可參考以下代碼 [[MoPub Mediation Setting|回MoPub教學]] == TAMediaInterstitialAdapter == <source> #import <MoPub/MoPub.h> #import <TAMediaAdsFramework/TAMediaAdsFramework.h> @interface TAMediaInterstitialAdapter: MPFullscreenAdAdapter <MPThirdPartyFullscreenAdAdapter, TADInterstitialDelegate> @property (nonatomic,strong) TADInterstitial *interstitialView; @end </source> <source> #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 </source>
返回到「
SDK 8 Mopub Mediation
」。
導覽選單
個人工具
登入
命名空間
頁面
討論
變體
已展開
已摺疊
視圖
閱讀
檢視原始碼
檢視歷史
更多
已展開
已摺疊
搜尋
導覽
首頁
近期變更
隨機頁面
有關 MediaWiki 的說明
工具
連結至此的頁面
相關變更
特殊頁面
頁面資訊