FusionAds-iOS/FusionAds/Classes/fusion/admob/GuruAdMobInterstitialAd.swift

83 lines
3.1 KiB
Swift
Raw Normal View History

//
// GuruAdMobInterstitialAd.swift
// Pods
//
// Created by 250102 on 2025/5/9.
//
import GoogleMobileAds
class GuruAdMobInterstitialAd : GuruInterstitialAd, GADFullScreenContentDelegate {
private let viewController: UIViewController
private let adConfig: AdConfig
private var ad: GADInterstitialAd?
public init(viewController: UIViewController, adConfig: AdConfig) {
self.viewController = viewController
self.adConfig = adConfig
super.init(engineId: adConfig.engineId, adUnitId: adConfig.adUnitId, adPlatform: AdPlatform.adMob)
}
override func load() -> Bool {
logInfo("request load")
Task { @MainActor in
do {
ad?.fullScreenContentDelegate = nil
ad?.paidEventHandler = nil
let interstitialAd = try await GADInterstitialAd.load(
withAdUnitID: adConfig.adUnitId, request: GADRequest())
interstitialAd.fullScreenContentDelegate = self
interstitialAd.paidEventHandler = { paidValue in
self.logInfo("revenue paid")
self.adRevenuePaid(ad: self.createFusionAd(interstitialAd, adValue: paidValue))
}
ad = interstitialAd
logInfo("loaded")
adLoaded(ad: createFusionAd(interstitialAd))
} catch {
logError("load failed, \(error.code) - \(error.localizedDescription)")
adLoadFailed(loadFailedInfo: LoadFailedInfo(engineId: engineId, adPlatform: adPlatform, adType: adType, adUnitId: adUnitId, error: SwiftFusionError(error)))
}
}
return true
}
override func show(_ request: InterstitialShowRequest? = nil) -> Bool {
logInfo("request show")
guard let ad = ad else {
logWarn("AdMob interstitialAd not ready for show")
return false
}
ad.present(fromRootViewController: viewController)
return true
}
override func destroy() -> Bool {
logInfo("request destroy")
self.ad?.fullScreenContentDelegate = nil
self.ad?.paidEventHandler = nil
self.ad = nil
return true
}
func adDidRecordClick(_ ad: any GADFullScreenPresentingAd) {
adClicked(ad: createFusionAd(self.ad))
}
func adDidRecordImpression(_ ad: any GADFullScreenPresentingAd) {
adDisplayed(ad: createFusionAd(self.ad))
}
func adDidDismissFullScreenContent(_ ad: any GADFullScreenPresentingAd) {
adHidden(ad: createFusionAd(self.ad))
}
func ad(_ ad: any GADFullScreenPresentingAd, didFailToPresentFullScreenContentWithError error: any Error) {
logError("displayed failed, \(error.code) - \(error.localizedDescription)")
adDisplayFailed(ad: createFusionAd(self.ad), error: SwiftFusionError(error))
}
private func createFusionAd(_ interstitialAd: GADInterstitialAd?, adValue: GADAdValue? = nil) -> FusionAd {
return AdMobFusionAd(engineId: engineId, adType: adType, adUnitId: adUnitId, ad: interstitialAd?.responseInfo, adValue: adValue)
}
}