「SDK8 Mopub Mediation Adapter Banner」修訂間的差異

出自TAMedia
跳至導覽 跳至搜尋
imported>Wikiuser
 
行 18: 行 18:
   @end
   @end
   
   
<source>  
<nowiki>  
  #import "TAMediaInlineAdAdapter.h"
  #import "TAMediaInlineAdAdapter.h"


行 102: 行 102:


@end
@end
</source>
</nowiki>

於 2022年4月21日 (四) 08:25 的最新修訂

依照 Mopub設定教學 設定後,開始實做 Adapter
官方教學:連結

  1. 先建立一 class 繼承 MPInlineAdAdapter,class 名稱必須與 Mopub 後台設定內一樣
  2. 加入 #import <TAMediaAdsFramework/TAMediaAdsFramework.h>
  3. 複寫 - (void)requestAdWithSize:(CGSize) adapterInfo: adMarkup:以及在此 method 內用 TADBannerView 呼叫廣告
    info 內的 key 值請依照 Mopub 內所設定的 key 值帶入
  4. 在 TADBannerView delegate 內呼叫 MPInlineAdAdapterDelegate 執行相對應的 method

可參考以下代碼,或直接下載 adapter 檔直接加入專案中

回MoPub教學

TAMediaInlineAdAdaptor

 #import <MoPub/MoPub.h>
 #import <TAMediaAdsFramework/TAMediaAdsFramework.h>
 @interface TAMediaInlineAdAdaptor : MPInlineAdAdapter<MPThirdPartyInlineAdAdapter, TADBannerViewDelegate>
 @property (nonatomic, strong) TADBannerView *bannerView;
 @end

 
 #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 : @"";
}

@end