「AdMob Mediation Video」修訂間的差異

出自TAMedia
跳至導覽 跳至搜尋
imported>Wikiuser
imported>Wikiuser
 
(未顯示同一使用者於中間所作的 10 次修訂)
行 11: 行 11:
  '''// MediationViewController.h'''
  '''// MediationViewController.h'''
   
   
  '''// mediation custom object 呼叫的 videoView, 因為沒有直接的關係'''
  '''// 使用 GADInterstitial 物件來介接 TADVideoAdView'''
'''// 若需要使用則需將其 reference 傳回來使用'''
  @property (nonatomic, strong) GADInterstitial *_adVideo;
  @property (nonatomic, strong) TADVideoAdView *videoView;
 


  '''// MediationViewController.m'''
  '''// MediationViewController.m'''
   
   
  - (void)viewWillDisappear:(BOOL)animated {
  - (void)viewWillDisappear:(BOOL)animated {
     '''// MediationViewController 的 view 消失前, 將 _videoView 及其 delegate 設為 nil'''
     '''// MediationViewController 的 view 消失前, 將 _adVideo 及其 delegate 設為 nil'''
    '''// _videoView 為 custom 物件呼叫的 TADVideoAdView 物件'''
     if (_adVideo != nil) {
    '''// 傳回來的 reference 在這裡使用 可以讓 mediation 呼叫的 video 物件跟著 MediationViewController 一起 dealloc'''
         _adVideo.delegate = nil;
     if (_videoView != nil) {
         _adVideo = nil;
         _videoView.delegate = nil;
         _videoView = nil;
     }
     }
  }
  }
行 36: 行 32:
  '''// 匯入 TAMedia SDK 定義'''
  '''// 匯入 TAMedia SDK 定義'''
  #import "TADVideoAdView.h"
  #import "TADVideoAdView.h"
#import "TADVideoAdViewDelegate.h"
   
   
  '''// 匯入 Admob SDK 定義'''
  '''// 匯入 Admob SDK 定義'''
  #import "GADCustomEventBanner.h"
  #import "GADCustomEventBanner.h"
#import "GADCustomEventBannerDelegate.h"
   
   
  @interface TADCustomVideo : NSObject<GADCustomEventBanner, TADVideoAdViewDelegate> {
  @interface TADCustomVideo : NSObject<GADCustomEventBanner, TADVideoAdViewDelegate>
{
    '''// 以 instant variable 的方式, 宣告 videoView 物件'''
     TADVideoAdView *_videoView;
     TADVideoAdView *_videoView;
  }
  }
行 49: 行 45:
   
   
  @end
  @end


  '''// TADCustomVideo.m'''
  '''// TADCustomVideo.m'''
   
   
  #import "TADCustomVideo.h"
  #import "TADCustomVideo.h"
@interface TADCustomVideo () {
    UIView *tempView;
}
@end
   
   
  @implementation TADCustomVideo
  @implementation TADCustomVideo
行 64: 行 54:
  @synthesize delegate = _delegate;
  @synthesize delegate = _delegate;
   
   
  - (void)requestBannerAd:(GADAdSize)adSize
  - (void)requestInterstitialAdWithParameter:(NSString *)serverParameter
              parameter:(NSString *)serverParameter
                                      label:(NSString *)serverLabel
                  label:(NSString *)serverLabel
                                    request:(GADCustomEventRequest *)customEventRequest {
                request:(GADCustomEventRequest *)customEventRequest {
      
      
     '''// 將 GADCustomEventRequest 物件裡的資訊轉入 TADRequest 物件'''
     '''// 將 GADCustomEventRequest 物件裡的資訊轉入 TADRequest 物件'''
行 75: 行 64:
     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'''
     '''// videoView 物件初始化'''
     if (_videoView == nil) {
     if (_videoView == nil) {
         _videoView = [[TADVideoAdView alloc] initWithVideoAd];
         _videoView = [[TADVideoAdView alloc] initWithVideoAd];
行 91: 行 80:
  }
  }
   
   
  #pragma mark -
  #pragma mark - delegate
#pragma mark - TADBannerViewDelegate
   
   
  - (void)adViewDidReceiveAd:(TADVideoAdView *)view {
  - (void)adViewDidReceiveAd:(TADVideoAdView *)view {
     NSLog(@"CustomBanner - didReceived! View: %@", view);
     NSLog(@"CustomVideo - adViewDidReceiveAd!!");
    '''// 回傳成功地收到廣告'''
     [_delegate customEventInterstitial:self didReceiveAd:view];
    '''// (回傳和 videoView 一樣大小的 UIView 物件給 customEventBanner)'''
    tempView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 250)];
     [_delegate customEventBanner:self didReceiveAd:tempView];
   
    '''// 將 (TADVideoAdView *)view 的 reference 傳回呼叫 mediation 的 view controller'''
    '''// 這裡呼叫 mediation 的 view controller 是 MediationViewController'''
    ((MediationViewController *)[_delegate viewControllerForPresentingModalView]).videoView = view;
    '''// 將 videoView 的 delegate 設為不是 self 的物件, 以免 custom 物件比 video 更早被 dealloc'''
    view.delegate = [_delegate viewControllerForPresentingModalView];
  }
  }
   
   
  - (void)adView:(TADVideoAdView *)view didFailToReceiveAdWithError:(NSError *)error {
  - (void)adView:(TADVideoAdView *)view didFailToReceiveAdWithError:(NSError *)error {
     [_delegate customEventBanner:self didFailAd:error];
    NSLog(@"CustomVideo - didFailToReceiveAdWithError!! error: %@", error);
     [_delegate customEventInterstitial:self didFailAd:error];
  }
  }
   
   
- (void)presentFromRootViewController:(UIViewController *)rootViewController{
    '''// 介接 TADVideoAdView 不需要 presentFromRootViewController'''
}
   
   
  - (void)adViewWillDismissScreen:(TADVideoAdView *)adView {
  - (void)dealloc {
     [_delegate customEventBannerWillDismissModal:self];
    NSLog(@"CustomVideo - dealloc!!");
    '''// Custom物件消失前, 要將 videoView 及其 delegate 設為 nil'''
    if (_videoView) {
        _videoView.delegate = nil;
        _videoView = nil;
     }
  }
  }
   
   
- (void)adViewDidDismissScreen:(TADVideoAdView *)adView {
    [_delegate customEventBannerDidDismissModal:self];
}
   
   
   
   

於 2014年7月24日 (四) 08:14 的最新修訂

首先將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。


Mediation View Controller

// MediationViewController.h

// 使用 GADInterstitial 物件來介接 TADVideoAdView
@property (nonatomic, strong) GADInterstitial *_adVideo;
// MediationViewController.m

- (void)viewWillDisappear:(BOOL)animated {
    // MediationViewController 的 view 消失前, 將 _adVideo 及其 delegate 設為 nil
    if (_adVideo != nil) {
        _adVideo.delegate = nil;
        _adVideo = nil;
    }
}


Custom Video View

// TADCustomVideo.h

// 匯入 TAMedia SDK 定義
#import "TADVideoAdView.h"

// 匯入 Admob SDK 定義
#import "GADCustomEventBanner.h"

@interface TADCustomVideo : NSObject<GADCustomEventBanner, TADVideoAdViewDelegate>
{
   // 以 instant variable 的方式, 宣告 videoView 物件
    TADVideoAdView *_videoView;
}

@property (nonatomic, weak) id<GADCustomEventBannerDelegate> delegate;

@end
// TADCustomVideo.m

#import "TADCustomVideo.h"

@implementation TADCustomVideo

@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];

    // videoView 物件初始化
    if (_videoView == nil) {
        _videoView = [[TADVideoAdView alloc] initWithVideoAd];
    }
    
    // 必須要設定delegate
    _videoView.delegate = self;
    
    // serverParameter 為 AD Unit ID, 在 Admob 裡設定
    _videoView.adUnitID = serverParameter;
    
    // 載入廣告
    [_videoView loadRequest:tadRequest];
}

#pragma mark - delegate

- (void)adViewDidReceiveAd:(TADVideoAdView *)view {
    NSLog(@"CustomVideo - adViewDidReceiveAd!!");
    [_delegate customEventInterstitial:self didReceiveAd:view];
}

- (void)adView:(TADVideoAdView *)view didFailToReceiveAdWithError:(NSError *)error {
    NSLog(@"CustomVideo - didFailToReceiveAdWithError!! error: %@", error);
    [_delegate customEventInterstitial:self didFailAd:error];
}

- (void)presentFromRootViewController:(UIViewController *)rootViewController{
    // 介接 TADVideoAdView 不需要 presentFromRootViewController
}

- (void)dealloc {
    NSLog(@"CustomVideo - dealloc!!");
    // Custom物件消失前, 要將 videoView 及其 delegate 設為 nil
    if (_videoView) {
        _videoView.delegate = nil;
        _videoView = nil;
    }
}



@end




Back