SDK8 AdMob Mediation Interstitial
於 2023年4月7日 (五) 09:56 由 CharlesWang(留言 | 貢獻) 所做的修訂
簡介
首先先完成 mediation 基本 setup AdMob Mediation Setup, TADCustomEvent 將會透過此客製化 class 取得 TADInterstitial。
實作方式
TADCustomEventInterstitial.swift
class TADCustomEventInterstitial: NSObject, GADMediationInterstitialAd {
var delegate: GADMediationInterstitialAdEventDelegate?
var interstitialView: TADInterstitial?
var completionHandler: GADMediationInterstitialLoadCompletionHandler?
override required init() {
super.init()
}
func loadInterstitial(for adConfiguration: GADMediationInterstitialAdConfiguration,
completionHandler: @escaping GADMediationInterstitialLoadCompletionHandler) {
// 將 adConfiguration 物件裡的資訊轉入 TADRequest 物件
let tadRequest = TADRequest()
tadRequest.isTesting = adConfiguration.isTestRequest
tadRequest.setLocationWithLatitude(adConfiguration.userLatitude, longitude: adConfiguration.userLongitude, accuracy: adConfiguration.userLocationAccuracyInMeters)
self.completionHandler = completionHandler
self.interstitialView = TADInterstitial()
self.interstitialView?.delegate = self
self.interstitialView?.adUnitID = adConfiguration.credentials.settings["parameter"] as? String
self.interstitialView?.turnOnMicrophone = false
self.interstitialView?.load(tadRequest)
}
func present(from viewController: UIViewController) {
interstitialView?.present(fromRootViewController: viewController)
}
}
// MARK: TADInterstitialDelegate
extension TADCustomEventInterstitial: TADInterstitialDelegate {
func interstitialDidReceiveAd(_ ad: TADInterstitial!) {
print("CustomInterstitial - interstitialDidReceiveAd!!")
if let handler = completionHandler {
delegate = handler(self, nil)
}
}
func interstitial(_ ad: TADInterstitial!, didFailToReceiveAdWithError error: TADRequestError!) {
print("CustomInterstitial - didFailToReceiveAdWithError!! error: \(String(describing: error))")
if let handler = completionHandler {
delegate = handler(nil, error)
}
// 需手動設為 nil,auto release 在 admob SDK 會造成在未在 main thread 執行
self.interstitialView?.delegate = nil
self.interstitialView = nil
}
func interstitialWillPresentScreen(_ ad: TADInterstitial!) {
print("CustomInterstitial - interstitialWillPresentScreen!!")
delegate?.reportImpression()
}
func interstitialDidDismissScreen(_ ad: TADInterstitial!) {
print("CustomInterstitial - interstitialWillDismissScreen!!")
delegate?.didDismissFullScreenView()
}
func interstitialWillLeaveApplication(_ ad: TADInterstitial!) {
print("CustomInterstitial - interstitialWillLeaveApplication!!")
delegate?.reportClick()
}
}
Interstitial畫面展示
插頁式廣告呈現 |