「AdMob Mediation Interstitial」修訂間的差異

出自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是TADCustomInterstitial,則新增的Class Name也必須為TADCustomInterstitial。<br>
[[檔案:Admob_sample_setting.png |400px]]
<br>
[[Google AdMob Mediation|回 Google AdMob Mediation]]<br>
 
== Mediation View Controller ==
 
 
'''// MediationViewController.h'''
'''// 使用 GADInterstitial 物件來介接 TADInterstitial'''
@property (nonatomic, strong) GADInterstitial *_adIntsl;
 
'''// MediationViewController.m'''
- (void)viewWillDisappear:(BOOL)animated {
    '''// MediationViewController 的 view 消失前, 將 _adIntsl 及其 delegate 設為 nil'''
    if (_adIntsl != nil) {
        _adIntsl.delegate = nil;
        _adIntsl = nil;
    }
}




行 11: 行 32:
   
   
  '''// 匯入 TAMedia SDK 定義'''
  '''// 匯入 TAMedia SDK 定義'''
  #import "TADInterstitial.h"
  #import <TAMediaAdsFramework/TAMediaAdsFramework.h>
#import "TADInterstitialDelegate.h"
   
   
  '''// 匯入 Admob SDK 定義'''
  '''// 匯入 Admob SDK 定義'''
  #import "GADCustomEventInterstitial.h"
  #import "GADCustomEventInterstitial.h"
#import "GADCustomEventInterstitialDelegate.h"
   
   
  @interface TADCustomInterstitial : NSObject <GADCustomEventInterstitial, TADInterstitialDelegate>
  @interface TADCustomInterstitial : NSObject <GADCustomEventInterstitial, TADInterstitialDelegate>
行 27: 行 46:
   
   
  @end
  @end


  '''// TADCustomInterstitial.m'''
  '''// TADCustomInterstitial.m'''
   
   
  #import "TADCustomInterstitial.h"
  #import "TADCustomInterstitial.h"
@interface TADCustomInterstitial ()
@end
   
   
  @implementation TADCustomInterstitial
  @implementation TADCustomInterstitial
行 76: 行 91:
   
   
  - (void)interstitialDidReceiveAd:(TADInterstitial *)ad {
  - (void)interstitialDidReceiveAd:(TADInterstitial *)ad {
     NSLog(@"[Test App] interstitialDidReceiveAd:%@", ad);
     NSLog(@"CustomInterstitial - interstitialDidReceiveAd!!");
    [self presentFromRootViewController:nil];
    [_delegate customEventInterstitialDidReceiveAd:self];
   
    '''// 將 interstitial 物件的 delegate 設為 self 以外的永存物件,以避免 crash 的發生!'''
    '''// 這裡選擇使用 UIApplication 的 window 物件'''
    _interstitialView.delegate = [[UIApplication sharedApplication].windows objectAtIndex:0];
  }
  }
   
   
  - (void)interstitial:(TADInterstitial *)ad didFailToReceiveAdWithError:(TADRequestError *)error {
  - (void)interstitial:(TADInterstitial *)ad didFailToReceiveAdWithError:(TADRequestError *)error {
     NSLog(@"[Test App] didFailToReceiveAdWithError:%@ %@", ad, error);
     NSLog(@"CustomInterstitial - didFailToReceiveAdWithError!! error: %@", error);
    [_delegate customEventInterstitial:self didFailAd:error];
  }
  }
   
   
  - (void)interstitialWillPresentScreen:(TADInterstitial *)ad {
  - (void)interstitialWillPresentScreen:(TADInterstitial *)ad {
     NSLog(@"[Test App] interstitialWillPresentScreen:%@", ad);
     NSLog(@"CustomInterstitial - interstitialWillPresentScreen!!");
    [_delegate customEventInterstitialWillPresent:self];
  }
  }
   
   
  - (void)interstitialWillDismissScreen:(TADInterstitial *)ad {
  - (void)interstitialWillDismissScreen:(TADInterstitial *)ad {
     NSLog(@"[Test App] interstitialWillDismissScreen:%@", ad);
     NSLog(@"CustomInterstitial - interstitialWillDismissScreen!!");
    [_delegate customEventInterstitialWillDismiss:self];
  }
  }
   
   
  - (void)interstitialDidDismissScreen:(TADInterstitial *)ad {
  - (void)interstitialDidDismissScreen:(TADInterstitial *)ad {
     NSLog(@"[Test App] interstitialDidDismissScreen:%@", ad);
     NSLog(@"CustomInterstitial - interstitialDidDismissScreen!!");
    [_delegate customEventInterstitialDidDismiss:self];
   
    '''// interstitial 廣告關閉時, 要將 interstitialView 及其 delegate 設為 nil'''
    if (_interstitialView != nil) {
        _interstitialView.delegate = nil;
        _interstitialView = nil;
    }
  }
  }
   
   
  - (void)interstitialWillLeaveApplication:(TADInterstitial *)ad {
  - (void)interstitialWillLeaveApplication:(TADInterstitial *)ad {
     NSLog(@"[Test App] interstitialWillLeaveApplication:%@", ad);
     NSLog(@"CustomInterstitial - interstitialWillLeaveApplication!!");
    [_delegate customEventInterstitialWillLeaveApplication:self];
  }
  }
   
   
  @end
  @end
行 110: 行 133:




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

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

首先將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是TADCustomInterstitial,則新增的Class Name也必須為TADCustomInterstitial。
Admob sample setting.png
回 Google AdMob Mediation

Mediation View Controller

// MediationViewController.h

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

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


Custom Interstitial View

// TADCustomInterstitial.h

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

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

@interface TADCustomInterstitial : NSObject <GADCustomEventInterstitial, TADInterstitialDelegate>
{
    // 以 instant variable 的方式, 宣告 interstitialView 物件
    TADInterstitial *_interstitialView;
}

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

@end
// TADCustomInterstitial.m

#import "TADCustomInterstitial.h"

@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(@"CustomInterstitial - interstitialDidReceiveAd!!");
   [_delegate customEventInterstitialDidReceiveAd:self];
}

- (void)interstitial:(TADInterstitial *)ad didFailToReceiveAdWithError:(TADRequestError *)error {
    NSLog(@"CustomInterstitial - didFailToReceiveAdWithError!! error: %@", error);
    [_delegate customEventInterstitial:self didFailAd:error];
}

- (void)interstitialWillPresentScreen:(TADInterstitial *)ad {
    NSLog(@"CustomInterstitial - interstitialWillPresentScreen!!");
    [_delegate customEventInterstitialWillPresent:self];
}

- (void)interstitialWillDismissScreen:(TADInterstitial *)ad {
    NSLog(@"CustomInterstitial - interstitialWillDismissScreen!!");
    [_delegate customEventInterstitialWillDismiss:self];
}

- (void)interstitialDidDismissScreen:(TADInterstitial *)ad {
    NSLog(@"CustomInterstitial - interstitialDidDismissScreen!!");
    [_delegate customEventInterstitialDidDismiss:self];
    
    // interstitial 廣告關閉時, 要將 interstitialView 及其 delegate 設為 nil
    if (_interstitialView != nil) {
        _interstitialView.delegate = nil;
        _interstitialView = nil;
    }
}

- (void)interstitialWillLeaveApplication:(TADInterstitial *)ad {
    NSLog(@"CustomInterstitial - interstitialWillLeaveApplication!!");
    [_delegate customEventInterstitialWillLeaveApplication:self];
}


@end



回 iOS首頁