「AdMob Mediation Banner」修訂間的差異

出自TAMedia
跳至導覽 跳至搜尋
imported>Wikiuser
imported>Wikiuser
 
(未顯示同一使用者於中間所作的 12 次修訂)
行 1: 行 1:
首先將AdMob和TAMedia SDK整合至專案中,AdMob SDK可以在這裡下載 https:'''//developers.google.com/mobile-ads-sdk/download?#downloadios。<br>'''
首先將AdMob和TAMedia SDK整合至專案中,AdMob SDK可以在這裡下載(https://developers.google.com/mobile-ads-sdk/download?#downloadios)<br>'''
接著在AdMob Mediation新增Custom Event(https:'''//mediation.admob.com)。<br>'''
接著在AdMob Mediation新增Custom Event (https://mediation.admob.com) <br>'''
新增Custom Event的詳細說明可以參考https:'''//developers.google.com/mobile-ads-sdk/docs/admob/mediation#ios。<br>'''
新增Custom Event的詳細說明可以參考 (https://developers.google.com/mobile-ads-sdk/docs/admob/mediation#ios) <br>'''
最後,在專案中加入與Custom Event對應的Class,其中Custom Event的Class Name需要和新增的Class Name保持一致,例如Custom Event的Class Name是TADCustomView,則新增的Class Name也必須為TADCustomView。
最後,在專案中加入與Custom Event對應的Class,其中Custom Event的Class Name需要和新增的Class Name保持一致,例如Custom Event的Class Name是TADCustomBanner,則新增的Class Name也必須為TADCustomBanner。<br><br>


[[Google AdMob Mediation|回 Google AdMob Mediation]]<br>


== Mediation View Controller ==
'''// MediationViewController.h'''
'''// 使用 GADBannerView 物件來介接 TADBannerView'''
@property (nonatomic, strong) GADBannerView *_adBanner;
'''// MediationViewController.m'''
- (void)viewWillDisappear:(BOOL)animated {
    '''// MediationViewController 的 view 消失前, 將 _adBanner 及其 delegate 設為 nil'''
    if (_adBanner != nil) {
        _adBanner.delegate = nil;
        _adBanner = nil;
    }
}


== Custom Banner View ==
== Custom Banner View ==
行 11: 行 30:
   
   
  '''// 匯入 TAMedia SDK 定義'''
  '''// 匯入 TAMedia SDK 定義'''
  #import "TADBannerView.h"
  #import <TAMediaAdsFramework/TAMediaAdsFramework.h>
#import "TADBannerViewDelegate.h"
   
   
  '''// 匯入 Admob SDK 定義'''
  '''// 匯入 Admob SDK 定義'''
  #import "GADCustomEventBanner.h"
  #import "GADCustomEventBanner.h"
#import "GADCustomEventBannerDelegate.h"
   
   
  @interface TADCustomBanner : NSObject<GADCustomEventBanner, TADBannerViewDelegate>
  @interface TADCustomBanner : NSObject<GADCustomEventBanner, TADBannerViewDelegate>
行 29: 行 46:


  '''// TADCustomBanner.m'''
  '''// TADCustomBanner.m'''
  #import "TADCustomBanner.h"
  #import "TADCustomBanner.h"
@interface TADCustomBanner ()
@end
   
   
  @implementation TADCustomBanner
  @implementation TADCustomBanner
行 51: 行 64:
     [tadRequest setLocationWithLatitude:customEventRequest.userLatitude longitude:customEventRequest.userLongitude accuracy:customEventRequest.userLocationAccuracyInMeters];
     [tadRequest setLocationWithLatitude:customEventRequest.userLatitude longitude:customEventRequest.userLongitude accuracy:customEventRequest.userLocationAccuracyInMeters];
      
      
     '''// bannerView 物件初始化, 帶入自訂的origin'''
     '''// bannerView 物件初始化'''
     if (!_bannerView) {
     if (_bannerView == nil) {
         _bannerView = [[TADBannerView alloc] initWithAdSize:TADAdSizeFromCGSize(adSize.size) origin:CGPointMake(0.0, 264.0)];
        '''// 若是你使用admob smart banner, 請記得要使用size常數'''
        '''// 因為 smart banner 沒有既定 size '''
        '''// e.g. '''
        '''// _bannerView = [[TADBannerView alloc] initWithAdSize:kTADAdSizeBanner];'''
        '''// '''
         _bannerView = [[TADBannerView alloc] initWithAdSize:TADAdSizeFromCGSize(adSize.size)];
     }
     }
      
      
行 61: 行 79:
     '''// serverParameter 為 AD Unit ID, 在 Admob 裡設定'''
     '''// serverParameter 為 AD Unit ID, 在 Admob 裡設定'''
     _bannerView.adUnitID = serverParameter;
     _bannerView.adUnitID = serverParameter;
   
    '''// 必需要設定rootViewController'''
    _bannerView.rootViewController = [[UIApplication sharedApplication] keyWindow].rootViewController;
      
      
     '''// 載入廣告'''
     '''// 載入廣告'''
行 70: 行 91:
   
   
  - (void)adViewDidReceiveAd:(TADBannerView *)view {
  - (void)adViewDidReceiveAd:(TADBannerView *)view {
     NSLog(@"CustomBanner - didReceived! View: %@", view);
     NSLog(@"CustomBanner - adViewDidReceiveAd!!");
     [_delegate customEventBanner:self didReceiveAd:view];
     [_delegate customEventBanner:self didReceiveAd:view];
   
    '''// 在成功收到廣告之後,設定 rootViewController!!!'''
    view.rootViewController = [_delegate viewControllerForPresentingModalView];
  }
  }
   
   
  - (void)adView:(TADBannerView *)view didFailToReceiveAdWithError:(NSError *)error {
  - (void)adView:(TADBannerView *)view didFailToReceiveAdWithError:(NSError *)error {
    NSLog(@"CustomBanner - didFailToReceiveAdWithError!! error: %@", error);
     [_delegate customEventBanner:self didFailAd:error];
     [_delegate customEventBanner:self didFailAd:error];
  }
  }
   
   
  - (void)adViewWillPresentScreen:(TADBannerView *)adView {
  - (void)adViewWillPresentScreen:(TADBannerView *)adView {
    NSLog(@"CustomBanner - adViewWillPresentScreen!!");
     [_delegate customEventBannerWillPresentModal:self];
     [_delegate customEventBannerWillPresentModal:self];
  }
  }
   
   
  - (void)adViewWillDismissScreen:(TADBannerView *)adView {
  - (void)adViewWillDismissScreen:(TADBannerView *)adView {
    NSLog(@"CustomBanner - adViewWillDismissScreen!!");
     [_delegate customEventBannerWillDismissModal:self];
     [_delegate customEventBannerWillDismissModal:self];
  }
  }
   
   
  - (void)adViewDidDismissScreen:(TADBannerView *)adView {
  - (void)adViewDidDismissScreen:(TADBannerView *)adView {
    NSLog(@"CustomBanner - adViewDidDismissScreen!!");
     [_delegate customEventBannerDidDismissModal:self];
     [_delegate customEventBannerDidDismissModal:self];
  }
  }
   
   
  - (void)adViewWillLeaveApplication:(TADBannerView *)adView {
  - (void)adViewWillLeaveApplication:(TADBannerView *)adView {
    NSLog(@"CustomBanner - adViewWillLeaveApplication!!");
     [_delegate customEventBannerWillLeaveApplication:self];
     [_delegate customEventBannerWillLeaveApplication:self];
  }
  }
   
   
  - (void)dealloc {
  - (void)dealloc {
     '''// Custom物件消失前, 要將 bannerView 及其 delegate 設為 nil'''
    NSLog(@"CustomBanner - dealloc!!");
    if (_bannerView != nil) {
     '''// Custom物件消失前, 要將 bannerView 及其 delegate 設為 nil, 須在 Main thread 上執行'''
        _bannerView.delegate = nil;
    if ([NSThread isMainThread]) {
        _bannerView = nil;
        if (_bannerView != nil) {
    }
            _bannerView.delegate = nil;
            _bannerView = nil;
        }
    } else {
        dispatch_async(dispatch_get_main_queue(), ^{
            if (_bannerView != nil) {
                _bannerView.delegate = nil;
                _bannerView = nil;
            }
        });
    }
  }
  }
   
   
行 112: 行 145:




[[IOS SDK 2.0 Developer Guide|Back]]
[[IOS SDK 2.0 Developer Guide|回 iOS首頁]]

於 2021年6月2日 (三) 10:22 的最新修訂

首先將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是TADCustomBanner,則新增的Class Name也必須為TADCustomBanner。

回 Google AdMob Mediation


Mediation View Controller

// MediationViewController.h

// 使用 GADBannerView 物件來介接 TADBannerView
@property (nonatomic, strong) GADBannerView *_adBanner;
// MediationViewController.m

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

Custom Banner View

// TADCustomBanner.h

// 匯入 TAMedia SDK 定義
#import <TAMediaAdsFramework/TAMediaAdsFramework.h>

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

@interface TADCustomBanner : NSObject<GADCustomEventBanner, TADBannerViewDelegate>
{
    // 以 instant variable 的方式, 宣告 bannerView 物件
    TADBannerView *_bannerView;
}

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

@end
// TADCustomBanner.m
#import "TADCustomBanner.h"

@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 物件初始化
    if (_bannerView == nil) {
        // 若是你使用admob smart banner, 請記得要使用size常數
        // 因為 smart banner 沒有既定 size 
        // e.g. 
        // _bannerView = [[TADBannerView alloc] initWithAdSize:kTADAdSizeBanner];
        // 
        _bannerView = [[TADBannerView alloc] initWithAdSize:TADAdSizeFromCGSize(adSize.size)];
    }
    
    // 必須要設定delegate
    _bannerView.delegate = self;
    
    // serverParameter 為 AD Unit ID, 在 Admob 裡設定
    _bannerView.adUnitID = serverParameter;
    
    // 必需要設定rootViewController
    _bannerView.rootViewController = [[UIApplication sharedApplication] keyWindow].rootViewController;
    
    // 載入廣告
    [_bannerView loadRequest:tadRequest];
}

#pragma mark -
#pragma mark - TADBannerViewDelegate

- (void)adViewDidReceiveAd:(TADBannerView *)view {
    NSLog(@"CustomBanner - adViewDidReceiveAd!!");
    [_delegate customEventBanner:self didReceiveAd:view];
}

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

- (void)adViewWillPresentScreen:(TADBannerView *)adView {
    NSLog(@"CustomBanner - adViewWillPresentScreen!!");
    [_delegate customEventBannerWillPresentModal:self];
}

- (void)adViewWillDismissScreen:(TADBannerView *)adView {
    NSLog(@"CustomBanner - adViewWillDismissScreen!!");
    [_delegate customEventBannerWillDismissModal:self];
}

- (void)adViewDidDismissScreen:(TADBannerView *)adView {
    NSLog(@"CustomBanner - adViewDidDismissScreen!!");
    [_delegate customEventBannerDidDismissModal:self];
}

- (void)adViewWillLeaveApplication:(TADBannerView *)adView {
    NSLog(@"CustomBanner - adViewWillLeaveApplication!!");
    [_delegate customEventBannerWillLeaveApplication:self];
}

- (void)dealloc {
    NSLog(@"CustomBanner - dealloc!!");
    // Custom物件消失前, 要將 bannerView 及其 delegate 設為 nil, 須在 Main thread 上執行
   if ([NSThread isMainThread]) {
       if (_bannerView != nil) {
           _bannerView.delegate = nil;
           _bannerView = nil;
       }
   } else {
       dispatch_async(dispatch_get_main_queue(), ^{
           if (_bannerView != nil) {
               _bannerView.delegate = nil;
               _bannerView = nil;
           }
       });
   }
}

@end




回 iOS首頁