TAMedia Wiki

SDK8 AdMob Mediation Interstitial

簡介

首先先完成 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畫面展示

插頁式廣告呈現

2021-08-06-AdMob_Interstitial.jpeg

回 AdMob Mediation