SDK8 AdMob Mediation Interstitial

出自TAMedia
於 2023年3月1日 (三) 10:04 由 CharlesWang留言 | 貢獻 所做的修訂
跳至導覽 跳至搜尋

簡介

首先將 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/admob/ios/custom-events/interstitial)
最後,在專案中加入與 Custom Event 對應的 Class,其中 Custom Event 的 Class Name 需要和專案新增的 Class Name 保持一致 例如新增的 Class Name 為 TADCustom Interstitial,Custom Event 的 Class Name 則必須是 專案名稱.TADCustom Interstitial。(以 Swift 編寫的專案需加上專案名稱;若為以 OC編寫,則放入 Class Name)

實作方式

  • MediationViewController.swift
// MediationViewController.swift
import UIKit
// 匯入 Admob SDK 定義
import GoogleMobileAds
class MediationViewController: UIViewController, GADFullScreenContentDelegate {
     // 使用 GADInterstitial 物件來介接 TADInterstitial
      var interstitialView: GADInterstitialAd?
      func viewDidLoad() {
         super.viewDidLoad()
     // 使用 GADRequest 載入AdMob設定之廣告
         let request = GADRequest()
         GADInterstitialAd.load(withAdUnitID:"ca-app-pub-AdUnitID",
                                                        request: request,
                                     completionHandler: { [self] ad, error in
                                        if let error = error {
                                           print("Failed to load interstitial ad with error: \(error.localizedDescription)")
                                           return
                                        }
                                       interstitial = ad
                                      }
          )
       }     
}
//MARK: GADFullScreenContentDelegate
 extension MediationViewController {
   /// Tells the delegate that the ad failed to present full screen content.
   func ad(_ ad: GADFullScreenPresentingAd, didFailToPresentFullScreenContentWithError error: Error) {
     print("Ad did fail to present full screen content.")
   }
     
   /// Tells the delegate that the ad presented full screen content.
   func adDidPresentFullScreenContent(_ ad: GADFullScreenPresentingAd) {
     print("Ad did present full screen content.")
   }
     
   /// Tells the delegate that the ad dismissed full screen content.
   func adDidDismissFullScreenContent(_ ad: GADFullScreenPresentingAd) {
     print("Ad did dismiss full screen content.")
   }
 }


  • Custom Banner View
// TADCustomInterstitial.swift
// 匯入 TAMedia SDK 定義
import TAMediaAdsFramework
// 匯入 Admob SDK 定義
import GoogleMobileAds

class TADCustomInterstitial: NSObject, GADCustomEventInterstitial, TADInterstitialDelegate {
  // 宣告 TADInterstitial 物件
  var interstitialView: TADInterstitial?
  // 宣告 GADCustomEventInterstitialDelegate 物件
  var delegate: GADCustomEventInterstitialDelegate?
   
  required override init() {
      super.init()
  }
   
   func requestAd(withParameter serverParameter: String?, label serverLabel: String?, request: GADCustomEventRequest) {
       // 將 GADCustomEventRequest 物件裡的資訊轉入 TADRequest 物件
       let tadRequest = TADRequest()
       tadRequest.isTesting = request.isTesting
       tadRequest.location = request.userLocationDescription
       tadRequest.setLocationWithLatitude(request.userLatitude, longitude: request.userLongitude, accuracy: request.userLocationAccuracyInMeters)
        
        //必須將 interstitialView 物件初始化
       self.interstitialView = TADInterstitial()
       // 必須設定delegate
       self.interstitialView?.delegate = self
       // 必須設定 adUnitID,serverParameter 為在 Admob 裡設定的 AD Unit ID
       self.interstitialView?.adUnitID = serverParameter
       // 必須載入廣告
       self.interstitialView?.load(tadRequest)
   }
   
   func present(fromRootViewController rootViewController: UIViewController) {
       interstitialView?.present(fromRootViewController: rootViewController)
   }
}
//MARK: TADInterstitialDelegate
extension TADCustomInterstitial {
   func interstitialDidReceiveAd(_ ad: TADInterstitial!) {
       print("CustomInterstitial - interstitialDidReceiveAd!!")
       self.delegate?.customEventInterstitialDidReceiveAd(self)
   }
   
   func interstitial(_ ad: TADInterstitial!, didFailToReceiveAdWithError error: TADRequestError!) {
       print("CustomInterstitial - didFailToReceiveAdWithError!! error: \(String(describing: error))")
       self.delegate?.customEventInterstitial(self, didFailAd: error)
       // 將 TAmedia 廣告設為 nil
       self.interstitialView?.delegate = nil
       self.interstitialView = nil      
   }
   
   func interstitialWillPresentScreen(_ ad: TADInterstitial!) {
       print("CustomInterstitial - interstitialWillPresentScreen!!")
       self.delegate?.customEventInterstitialWillPresent(self)
   }
   
   func interstitialDidDismissScreen(_ ad: TADInterstitial!) {
       print("CustomInterstitial - interstitialWillDismissScreen!!")
       self.delegate?.customEventInterstitialWillDismiss(self)
   }
    
   func interstitialWillLeaveApplication(_ ad: TADInterstitial!) {
       print("CustomInterstitial - interstitialWillLeaveApplication!!")
       self.delegate?.customEventInterstitialWillLeaveApplication(self)
   }
}

Interstitial畫面展示

插頁式廣告呈現
2021-08-06-AdMob Interstitial.jpeg