AdMob Mediation

出自TAMedia
於 2014年7月3日 (四) 11:22 由 imported>Wikiuser 所做的修訂
跳至導覽 跳至搜尋

首先將AdMob和TAMedia SDK整合至專案中,AdMob SDK可以在這裡下載 https://developers.google.com/mobile-ads-sdk/download?#downloadios。
接著在AdMob Mediation新增Custom Event(https://mediation.admob.com)。
新增Custom Event的詳細說明可以參考https://developers.google.com/mobile-ads-sdk/docs/admob/mediation#ios。
最後,在專案中加入與Custom Event對應的Class,其中Custom Event的Class Name需要和新增的Class Name保持一致,例如Custom Event的Class Name是TADCustomView,則新增的Class Name也必須為TADCustomView。

Custom Banner View

// TADCustomBanner.m

// 匯入需要的 .h 檔
#import "TADCustomBanner.h"
#import "GADCustomEventBanner.h"
#import "GADCustomEventBannerDelegate.h"
#import "TADBannerView.h"
#import "TADRequest.h"
#import "TADMediationView.h"

@interface TADCustomBanner () <TADBannerViewDelegate> {
    // 這裡需要使用 TADMediationView 物件!!!
    TADMediationView *_bannerView;
}
@end

@implementation TADCustomBanner

@synthesize delegate = _delegate;

- (void)requestBannerAd:(GADAdSize)adSize
              parameter:(NSString *)serverParameter
                  label:(NSString *)serverLabel
                request:(GADCustomEventRequest *)request  {
    
    // TODO: Use the parameters and self.delegate to make a banner request to your
    // ad network. Remember to set this class to be your banner’s delegate.
    
    TADRequest *tadRequest = [TADRequest request];

    // 設定非測試模式
    tadRequest.testing = NO;
    // 設定 Geolocation 資訊
    [tadRequest setLocationWithLatitude:25.0265954 longitude:121.5540628 accuracy:53.996];
    
    if (_bannerView == nil) {
        _bannerView = [[TADMediationView alloc] initWithAdSize:kTADAdSizeBanner];
        _bannerView.delegate = self;
        _bannerView.adUnitID = serverParameter;
        _bannerView.rootViewController = [_delegate viewControllerForPresentingModalView];
    }
    [_bannerView loadRequest:tadRequest];
}

#pragma mark -
#pragma mark - TADBannerViewDelegate

- (void)adViewDidReceiveAd:(TADBannerView *)view {
    [self.delegate customEventBanner:self didReceiveAd:view];
}

- (void)adView:(TADBannerView *)view didFailToReceiveAdWithError:(NSError *)error {
    [self.delegate customEventBanner:self didFailAd:error];
}

- (void)dealloc{

    // 記得要設為 nil
    if (_bannerView != nil) {
        _bannerView.delegate = nil;
        _bannerView = nil;
    }
} 

@end


Custom Interstitial View

// TADCustomInterstitial.m

// 匯入需要的 .h 檔
#import "TADCustomInterstitial.h"
#import "GADCustomEventInterstitial.h"
#import "GADCustomEventInterstitialDelegate.h"
#import "TADInterstitial.h"
#import "TADRequest.h"


@interface TADCustomInterstitial () <TADInterstitialDelegate> {
    // 使用原本 Interstitial 物件即可
    TADInterstitial *_interstitialView;
}
@end

@implementation TADCustomInterstitial

- (void)requestInterstitialAdWithParameter:(NSString *)serverParameter
                                     label:(NSString *)serverLabel
                                   request:(GADCustomEventRequest *)customEventRequest {
    
    
    TADRequest *tadRequest = [TADRequest request];
    // 設定非測試模式
    tadRequest.testing = NO;
    // 設定 Geolocation 資訊
    [tadRequest setLocationWithLatitude:25.0265954 longitude:121.5540628 accuracy:53.996];
    
    if (!_interstitialView) {
        _interstitialView = [[TADInterstitial alloc] init];
        _interstitialView.delegate = self;
        _interstitialView.adUnitID = serverParameter;
    }
    [_interstitialView loadRequest:tadRequest];
}

- (void)presentFromRootViewController:(UIViewController *)rootViewController {
    [_interstitialView presentFromRootViewController:rootViewController];
}


#pragma mark -
#pragma mark - TADInterstitialDelegate


- (void)interstitialDidReceiveAd:(TADInterstitial *)ad {
    NSLog(@"[Test App] interstitialDidReceiveAd:%@", ad);
    [self presentFromRootViewController:nil];
}

- (void)interstitial:(TADInterstitial *)ad didFailToReceiveAdWithError:(TADRequestError *)error {
    NSLog(@"[Test App] didFailToReceiveAdWithError:%@ %@", ad, error);
}

- (void)interstitialWillPresentScreen:(TADInterstitial *)ad {
    NSLog(@"[Test App] interstitialWillPresentScreen:%@", ad);
}

- (void)interstitialWillDismissScreen:(TADInterstitial *)ad {
    NSLog(@"[Test App] interstitialWillDismissScreen:%@", ad);
}

- (void)interstitialDidDismissScreen:(TADInterstitial *)ad {
    NSLog(@"[Test App] interstitialDidDismissScreen:%@", ad);
    //_interstitialView.delegate = nil;
    //_interstitialView = nil;
}

- (void)interstitialWillLeaveApplication:(TADInterstitial *)ad {
    NSLog(@"[Test App] interstitialWillLeaveApplication:%@", ad);
}

- (void)dealloc {
    // 記得要設為 nil
    if (_interstitialView) {
        _interstitialView.delegate = nil;
        _interstitialView = nil;
    }
}

@end