Mopub Mediation Banner

出自TAMedia
於 2017年6月7日 (三) 06:18 由 imported>Wikiuser 所做的修訂
跳至導覽 跳至搜尋

依照Mopub設定教學設定後,開始實做Custom event
官方教學:連結
1.先建立一class繼承MPBannerCustomEvent,class名稱必須與Mopub設定內一樣
2.加入Tamedia TADBannerView.h
3.複寫- (void)requestAdWithSize:(CGSize)size customEventInfo:(NSDictionary *)info以及在此method內用TADBannerView呼叫廣告
4.在TADBannerView delegate內呼叫MPBannerCustomEvent delegate執行相對應的method
可參考以下代碼

CustomBannerEvent

  //  CustomBannerEvent.h
  //匯入TADBannerVIew
  #import "MPBannerCustomEvent.h"
  #import "TADBannerView.h"
  @interface CustomBannerEvent : MPBannerCustomEvent<TADBannerViewDelegate>
  @property (nonatomic, strong) TADBannerView *bannerView;
  @end
  #import "CustomBannerEvent.h"
  
  @implementation CustomBannerEvent
  
  - (void)requestAdWithSize:(CGSize)size customEventInfo:(NSDictionary *)info {
      TADRequest *request = [TADRequest request];
      request.testing = NO;
      request.gender = kTADGenderUnknown;
      [request setBirthdayWithYear:1988 month:6 day:11];
      [request setLocationWithLatitude:25.033534 longitude:121.534791 accuracy:1];
      if (!self.bannerView) {
          self.bannerView = [[TADBannerView alloc] initWithAdSize:TADAdSizeFromCGSize(size)];
      }
      self.bannerView.delegate = self;
      self.bannerView.turnOnMicrophone = NO;
      NSString *bannerId = info[@"BannerId"];//此處BannerId是在Mopub內設定
      self.bannerView.adUnitID = bannerId;
      self.bannerView.rootViewController = [[UIApplication sharedApplication] keyWindow].rootViewController;
      [self.bannerView loadRequest:request];
  
  }
  #pragma mark - [Tamedia Banner delegate]
  
  - (void)adViewDidReceiveAd:(TADBannerView *)view {
      if ([self.delegate respondsToSelector:@selector(bannerCustomEvent:didLoadAd:)]) {
          [self.delegate bannerCustomEvent:self didLoadAd:view];
      }
  }
  - (void)adView:(TADBannerView *)view didFailToReceiveAdWithError:(TADRequestError *)error {
      if ([self.delegate respondsToSelector:@selector(bannerCustomEvent:didFailToLoadAdWithError:)]) {
          [self.delegate bannerCustomEvent:self didFailToLoadAdWithError:error];
      }
  }
  
  - (void)adViewWillPresentScreen:(TADBannerView *)adView {       
  }
  
  - (void)adViewWillDismissScreen:(TADBannerView *)adView {       
  }
  
  - (void)adViewDidDismissScreen:(TADBannerView *)adView {       
  }
  
  - (void)adViewWillLeaveApplication:(TADBannerView *)adView {
      if ([self.delegate respondsToSelector:@selector(bannerCustomEventWillLeaveApplication:)]) {
          [self.delegate bannerCustomEventWillLeaveApplication:self];
      }
  }   
  - (void)dealloc {
      if (self.bannerView != nil) {
          self.bannerView.delegate = nil;
          self.bannerView = nil;
      }
  }   
  @end