「AdMob Mediation」修訂間的差異
跳至導覽
跳至搜尋
imported>Wikiuser |
imported>Wikiuser |
||
行 49: | 行 49: | ||
tadRequest.birthday = customEventRequest.userBirthday; | tadRequest.birthday = customEventRequest.userBirthday; | ||
[tadRequest setLocationWithLatitude:customEventRequest.userLatitude longitude:customEventRequest.userLongitude accuracy:customEventRequest.userLocationAccuracyInMeters]; | [tadRequest setLocationWithLatitude:customEventRequest.userLatitude longitude:customEventRequest.userLongitude accuracy:customEventRequest.userLocationAccuracyInMeters]; | ||
'''// bannerView 物件初始化, 帶入自訂的origin''' | '''// bannerView 物件初始化, 帶入自訂的origin''' | ||
行 189: | 行 180: | ||
NSLog(@"[Test App] interstitialDidReceiveAd:%@", ad); | NSLog(@"[Test App] interstitialDidReceiveAd:%@", ad); | ||
[self presentFromRootViewController:nil]; | [self presentFromRootViewController:nil]; | ||
_interstitialView.delegate = [[UIApplication sharedApplication].windows objectAtIndex:0]; | |||
} | } | ||
行 211: | 行 203: | ||
} | } | ||
- (void) | @end | ||
'''// | |||
== Custom Video View == | |||
'''// TADCustomVideo.h''' | |||
'''// 匯入 TAMedia SDK 定義''' | |||
#import "TADVideoAdView.h" | |||
#import "TADVideoAdViewDelegate.h" | |||
'''// 匯入 Admob SDK 定義''' | |||
#import "GADCustomEventBanner.h" | |||
#import "GADCustomEventBannerDelegate.h" | |||
@interface TADCustomVideo : NSObject<GADCustomEventBanner, TADVideoAdViewDelegate> { | |||
TADVideoAdView *_videoView; | |||
} | |||
@property (nonatomic, weak) id<GADCustomEventBannerDelegate> delegate; | |||
@end | |||
'''// TADCustomVideo.m''' | |||
#import "TADCustomVideo.h" | |||
@interface TADCustomVideo () { | |||
UIView *tempView; | |||
} | |||
@end | |||
@implementation TADCustomVideo | |||
@synthesize delegate = _delegate; | |||
- (void)requestBannerAd:(GADAdSize)adSize | |||
parameter:(NSString *)serverParameter | |||
label:(NSString *)serverLabel | |||
request:(GADCustomEventRequest *)customEventRequest { | |||
'''// 將 GADCustomEventRequest 物件裡的資訊轉入 TADRequest 物件''' | |||
TADRequest *tadRequest = [TADRequest request]; | |||
tadRequest.testing = customEventRequest.isTesting; | |||
tadRequest.gender = (TADGender)customEventRequest.userGender; | |||
tadRequest.birthday = customEventRequest.userBirthday; | |||
[tadRequest setLocationWithLatitude:customEventRequest.userLatitude longitude:customEventRequest.userLongitude accuracy:customEventRequest.userLocationAccuracyInMeters]; | |||
'''// bannerView 物件初始化, 帶入自訂的origin''' | |||
if (_videoView == nil) { | |||
_videoView = [[TADVideoAdView alloc] initWithVideoAd]; | |||
} | } | ||
'''// 必須要設定delegate''' | |||
_videoView.delegate = self; | |||
'''// serverParameter 為 AD Unit ID, 在 Admob 裡設定''' | |||
_videoView.adUnitID = serverParameter; | |||
'''// 載入廣告''' | |||
[_videoView loadRequest:tadRequest]; | |||
} | |||
#pragma mark - | |||
#pragma mark - TADBannerViewDelegate | |||
- (void)adViewDidReceiveAd:(TADVideoAdView *)view { | |||
NSLog(@"CustomBanner - didReceived! View: %@", view); | |||
'''// 回傳成功地收到廣告''' | |||
'''// (回傳和 videoView 一樣大小的 UIView 物件給 customEventBanner)''' | |||
tempView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 250)]; | |||
[_delegate customEventBanner:self didReceiveAd:tempView]; | |||
'''// 將 videoView 的 delegate 設為不是 self 的物件, 以免 custom 物件比 video 更早被 dealloc''' | |||
view.delegate = [_delegate viewControllerForPresentingModalView]; | |||
} | } | ||
- (void)adView:(TADVideoAdView *)view didFailToReceiveAdWithError:(NSError *)error { | |||
[_delegate customEventBanner:self didFailAd:error]; | |||
} | |||
- (void)adViewWillDismissScreen:(TADVideoAdView *)adView { | |||
[_delegate customEventBannerWillDismissModal:self]; | |||
} | |||
- (void)adViewDidDismissScreen:(TADVideoAdView *)adView { | |||
[_delegate customEventBannerDidDismissModal:self]; | |||
} | |||
@end | @end | ||
於 2014年7月11日 (五) 03:43 的最新修訂
首先將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.h // 匯入 TAMedia SDK 定義 #import "TADBannerView.h" #import "TADBannerViewDelegate.h" // 匯入 Admob SDK 定義 #import "GADCustomEventBanner.h" #import "GADCustomEventBannerDelegate.h" @interface TADCustomBanner : NSObject<GADCustomEventBanner, TADBannerViewDelegate> { // 以 instant variable 的方式, 宣告 bannerView 物件 TADBannerView *_bannerView; } @property (nonatomic, weak) id<GADCustomEventBannerDelegate> delegate; @end
// TADCustomBanner.m #import "TADCustomBanner.h" @interface TADCustomBanner () @end @implementation TADCustomBanner @synthesize delegate = _delegate; - (void)requestBannerAd:(GADAdSize)adSize parameter:(NSString *)serverParameter label:(NSString *)serverLabel request:(GADCustomEventRequest *)customEventRequest { // 將 GADCustomEventRequest 物件裡的資訊轉入 TADRequest 物件 TADRequest *tadRequest = [TADRequest request]; tadRequest.testing = customEventRequest.isTesting; tadRequest.gender = (TADGender)customEventRequest.userGender; tadRequest.birthday = customEventRequest.userBirthday; [tadRequest setLocationWithLatitude:customEventRequest.userLatitude longitude:customEventRequest.userLongitude accuracy:customEventRequest.userLocationAccuracyInMeters]; // bannerView 物件初始化, 帶入自訂的origin if (!_bannerView) { _bannerView = [[TADBannerView alloc] initWithAdSize:TADAdSizeFromCGSize(adSize.size) origin:CGPointMake(0.0, 264.0)]; } // 必須要設定delegate _bannerView.delegate = self; // serverParameter 為 AD Unit ID, 在 Admob 裡設定 _bannerView.adUnitID = serverParameter; // 載入廣告 [_bannerView loadRequest:tadRequest]; } #pragma mark - #pragma mark - TADBannerViewDelegate - (void)adViewDidReceiveAd:(TADBannerView *)view { NSLog(@"CustomBanner - didReceived! View: %@", view); [_delegate customEventBanner:self didReceiveAd:view]; // 在這裡設定 rootViewController!!! view.rootViewController = [_delegate viewControllerForPresentingModalView]; } - (void)adView:(TADBannerView *)view didFailToReceiveAdWithError:(NSError *)error { [_delegate customEventBanner:self didFailAd:error]; } - (void)adViewWillPresentScreen:(TADBannerView *)adView { [_delegate customEventBannerWillPresentModal:self]; } - (void)adViewWillDismissScreen:(TADBannerView *)adView { [_delegate customEventBannerWillDismissModal:self]; } - (void)adViewDidDismissScreen:(TADBannerView *)adView { [_delegate customEventBannerDidDismissModal:self]; } - (void)adViewWillLeaveApplication:(TADBannerView *)adView { [_delegate customEventBannerWillLeaveApplication:self]; } - (void)dealloc { // Custom物件消失前, 要將 bannerView 及其 delegate 設為 nil if (_bannerView != nil) { _bannerView.delegate = nil; _bannerView = nil; } } @end
Custom Interstitial View
// TADCustomInterstitial.h // 匯入 TAMedia SDK 定義 #import "TADInterstitial.h" #import "TADInterstitialDelegate.h" // 匯入 Admob SDK 定義 #import "GADCustomEventInterstitial.h" #import "GADCustomEventInterstitialDelegate.h" @interface TADCustomInterstitial : NSObject <GADCustomEventInterstitial, TADInterstitialDelegate> { // 以 instant variable 的方式, 宣告 interstitialView 物件 TADInterstitial *_interstitialView; } @property (nonatomic, weak) id<GADCustomEventInterstitialDelegate> delegate; @end
// TADCustomInterstitial.m #import "TADCustomInterstitial.h" @interface TADCustomInterstitial () @end @implementation TADCustomInterstitial @synthesize delegate = _delegate; - (void)requestInterstitialAdWithParameter:(NSString *)serverParameter label:(NSString *)serverLabel request:(GADCustomEventRequest *)customEventRequest { // 將 GADCustomEventRequest 物件裡的資訊轉入 TADRequest 物件 TADRequest *tadRequest = [TADRequest request]; tadRequest.testing = customEventRequest.isTesting; tadRequest.gender = (TADGender)customEventRequest.userGender; tadRequest.birthday = customEventRequest.userBirthday; [tadRequest setLocationWithLatitude:customEventRequest.userLatitude longitude:customEventRequest.userLongitude accuracy:customEventRequest.userLocationAccuracyInMeters]; // interstitialView 物件初始化 if (_interstitialView == nil) { _interstitialView = [[TADInterstitial alloc] init]; } // 必須要設定delegate _interstitialView.delegate = self; // serverParameter 為 AD Unit ID, 在 Admob 裡設定 _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]; _interstitialView.delegate = [[UIApplication sharedApplication].windows objectAtIndex:0]; } - (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); } - (void)interstitialWillLeaveApplication:(TADInterstitial *)ad { NSLog(@"[Test App] interstitialWillLeaveApplication:%@", ad); } @end
Custom Video View
// TADCustomVideo.h // 匯入 TAMedia SDK 定義 #import "TADVideoAdView.h" #import "TADVideoAdViewDelegate.h" // 匯入 Admob SDK 定義 #import "GADCustomEventBanner.h" #import "GADCustomEventBannerDelegate.h" @interface TADCustomVideo : NSObject<GADCustomEventBanner, TADVideoAdViewDelegate> { TADVideoAdView *_videoView; } @property (nonatomic, weak) id<GADCustomEventBannerDelegate> delegate; @end
// TADCustomVideo.m #import "TADCustomVideo.h" @interface TADCustomVideo () { UIView *tempView; } @end @implementation TADCustomVideo @synthesize delegate = _delegate; - (void)requestBannerAd:(GADAdSize)adSize parameter:(NSString *)serverParameter label:(NSString *)serverLabel request:(GADCustomEventRequest *)customEventRequest { // 將 GADCustomEventRequest 物件裡的資訊轉入 TADRequest 物件 TADRequest *tadRequest = [TADRequest request]; tadRequest.testing = customEventRequest.isTesting; tadRequest.gender = (TADGender)customEventRequest.userGender; tadRequest.birthday = customEventRequest.userBirthday; [tadRequest setLocationWithLatitude:customEventRequest.userLatitude longitude:customEventRequest.userLongitude accuracy:customEventRequest.userLocationAccuracyInMeters]; // bannerView 物件初始化, 帶入自訂的origin if (_videoView == nil) { _videoView = [[TADVideoAdView alloc] initWithVideoAd]; } // 必須要設定delegate _videoView.delegate = self; // serverParameter 為 AD Unit ID, 在 Admob 裡設定 _videoView.adUnitID = serverParameter; // 載入廣告 [_videoView loadRequest:tadRequest]; } #pragma mark - #pragma mark - TADBannerViewDelegate - (void)adViewDidReceiveAd:(TADVideoAdView *)view { NSLog(@"CustomBanner - didReceived! View: %@", view); // 回傳成功地收到廣告 // (回傳和 videoView 一樣大小的 UIView 物件給 customEventBanner) tempView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 250)]; [_delegate customEventBanner:self didReceiveAd:tempView]; // 將 videoView 的 delegate 設為不是 self 的物件, 以免 custom 物件比 video 更早被 dealloc view.delegate = [_delegate viewControllerForPresentingModalView]; } - (void)adView:(TADVideoAdView *)view didFailToReceiveAdWithError:(NSError *)error { [_delegate customEventBanner:self didFailAd:error]; } - (void)adViewWillDismissScreen:(TADVideoAdView *)adView { [_delegate customEventBannerWillDismissModal:self]; } - (void)adViewDidDismissScreen:(TADVideoAdView *)adView { [_delegate customEventBannerDidDismissModal:self]; } @end