SDK8 Interstitial
於 2021年8月5日 (四) 04:53 由 imported>Wikiuser 所做的修訂
Interstitial廣告可以在app啟動, 視頻載入期間或遊戲暫停的時候,呈現內容豐富的HTML5網頁。
以下將告訴您如何讓您的app顯示Interstitial廣告。
加入 TADInterstitial
TADInterstitial 的用法與 TADBannerView 相似,簡單的步驟即可加入TADInterstitial 建議在 UIViewController 中執行以下步驟:
- 匯入 #import <TAMediaAdsFramework/TAMediaAdsFramework.h>
- 在 UIViewController 中宣告 TADInterstitial 執行個體
- 建立插頁式廣告
- 設定廣告單元編號
// InterstitialViewController.h
// 匯入 TAMedia SDK 定義
#import <TAMediaAdsFramework/TAMediaAdsFramework.h>
@interface InterstitialViewController : UIViewController <TADInterstitialDelegate>
{
// 以 instant variable 的方式, 宣告 interstitialView 物件
TADInterstitial *interstitialView;
}
@end
// InterstitialViewController.m
#import "InterstitialViewController.h"
@implementation InterstitialViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor whiteColor];
self.title = @"TAMedia Interstitial";
UIButton *fireButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
fireButton.frame = CGRectMake(10, 120, 200, 40);
fireButton.backgroundColor = [UIColor colorWithRed:239/255.0 green:239/255.0 blue:239/255.0 alpha:1.0];
[fireButton setTitle:@"Interstitial Ad" forState:UIControlStateNormal];
[fireButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[fireButton addTarget:self action:@selector(requestAd) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:fireButton];
}
- (void)requestAd
{
TADRequest *request = [TADRequest request];
// Type: TADGender
request.gender = [_GENDER_];
// Set birthday
[request setBirthdayWithYear:[_YEAR_] month:[_MONTH_] day:[_DAY_]];
// Set location
[request setLocationWithLatitude:[_LATITUDE_] longitude:[_LONGITUDE_] accuracy:[_ACCURACY_]];
// interstitialView 物件初始化
if (interstitialView == nil) {
interstitialView = [[TADInterstitial alloc] init];
}
// 必須要設定delegate
interstitialView.delegate = self;
//是否使用麥克風廣告 default為Yes,依需求修改 [註1]
interstitialView.turnOnMicrophone = YES;
//讓廣告控制audioSessionControl default為Yes,依需求修改 [註2]
interstitialView.allowAudioSessionControl = YES;
// 設定 AD Unit ID
interstitialView.adUnitID = [_YOUR_AD_UNIT_AD_];
// 載入廣告
[interstitialView loadRequest:request];
}
@end
特別注意
在 ViewController 的 view 即將消失前, 或是 interstitialView 消失前, 必須要將 interstitialView 本身以及其 delegate 設定為 nil
// InterstitialViewController.m
// ViewController Delegate Function
- (void)viewWillDisappear:(BOOL)animated {
// viewController 的 view 消失前, 必須將 interstitialView 及其 delegate 設為 nil
if (interstitialView != nil) {
interstitialView.delegate = nil;
interstitialView = nil;
}
}
// TADInterstitial Delegate Function
- (void)interstitialDidDismissScreen:(TADInterstitial *)ad{
// interstitialView 關閉前, 必須將 interstitialView 及其 delegate 設為 nil
if (interstitialView != nil) {
interstitialView.delegate = nil;
interstitialView = nil;
}
}
TADInterstitialDelegate
@protocol TADInterstitialDelegate <NSObject> @optional // Ad Request Lifecycle Notifications // Interstitial廣告成功載入後呼叫, 可以在此時間點及之後顯示呈現廣告 - (void)interstitialDidReceiveAd:(TADInterstitial *)ad; // Interstitial廣告載入失敗時呼叫, 處理錯誤 或 顯示錯誤訊息 - (void)interstitial:(TADInterstitial *)ad didFailToReceiveAdWithError:(TADRequestError *)error; // Display-Time Lifecycle Notifications // Interstitial廣告呈現之前呼叫, 注意使用者可能會在廣告內按下連結(網頁, AppStore, ...)而因此離開應用程式, 可以在這function中處理該暫停之項目 - (void)interstitialWillPresentScreen:(TADInterstitial *)ad; // Interstitial廣告關閉, 在螢幕上消失之前呼叫, 再次提醒在Interstitail消失前, 必須將Interstitial及delegate設為nil (可以在這個function中執行) - (void)interstitialWillDismissScreen:(TADInterstitial *)ad; // Interstitial廣告關閉, 在螢幕上消失之後呼叫 - (void)interstitialDidDismissScreen:(TADInterstitial *)ad; // 使用者在廣告內按下連結(網頁, AppStore, ...)而因此離開應用程式之前呼叫 - (void)interstitialWillLeaveApplication:(TADInterstitial *)ad; @end
[註1]TADBannerView 及 TADInterstitial增加此屬性,Default為YES。設為YES會去使用TADMicrophoneDetector物件,進而拿到麥克風類型廣告,與使用者互動,設為NO,則回傳server無Microphone。因為使用麥克風廣告會造成音樂類型的app中斷播放,建議音樂類型app在此設為NO。
[註2]allowAudioSessionControl設為YES以及原本app的AudioSession category為AVAudioSessionCategorySoloAmbient或AVAudioSessionCategoryPlayback時,將由SDK控制AudioSession設定active及改變category以滿足背景音樂中斷及續撥的行為。若是設為NO或category原本設定並非上述兩種SDK將不會去對AudioSession做改變,而是交給開發者在插頁出現及消失時,依據想要的行為做設定。