Fableon/Fableon/Object/Libs/AdManager/Open/FATradPlusOpenAd.swift
2026-02-04 17:25:54 +08:00

109 lines
2.7 KiB
Swift

//
// BRTradPlusOpenAd.swift
// Fableon
//
// Created by on 2026/1/23.
//
import UIKit
#if canImport(TradPlusAds)
import TradPlusAds
#endif
class FATradPlusOpenAd: NSObject, FAOpenAd {
#if canImport(TradPlusAds)
private var appOpenAd: TradPlusAdSplash?
#endif
weak var delegate: FAOpenAdDelegate?
var adPlatform: FAAdPlatform {
return .tradPlus
}
var adType: FAAdType {
return .open
}
var adUnitID: String {
return self.adPlatform.openUnitId
}
var isReady: Bool {
#if canImport(TradPlusAds)
return self.appOpenAd?.isAdReady ?? false
#else
return false
#endif
}
func loadAd() {
appOpenAd = nil
guard let targetWindow = FATool.keyWindow else {
let error = NSError(domain: "com.beereel.ad",
code: -2,
userInfo: [NSLocalizedDescriptionKey: "TradPlus failed: No active window found"])
self.delegate?.fa_openAd(ad: self, didLoadFail: error)
return
}
#if canImport(TradPlusAds)
// 3.
let splash = TradPlusAdSplash()
splash.setAdUnitID(adUnitID)
splash.delegate = self
self.appOpenAd = splash
DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) {
splash.loadAd(with: targetWindow, bottomView: nil)
}
#endif
}
func showAd() {
if isReady {
#if canImport(TradPlusAds)
DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) { [weak self] in
guard let self = self else { return }
self.appOpenAd?.show()
}
#endif
}
}
}
#if canImport(TradPlusAds)
// MARK: - TradPlusADSplashDelegate
extension FATradPlusOpenAd: TradPlusADSplashDelegate {
func tpSplashAdLoaded(_ adInfo: [AnyHashable : Any]) {
self.delegate?.fa_openAdDidLoadFinish(ad: self)
}
func tpSplashAdLoadFailWithError(_ error: Error, adInfo: [AnyHashable : Any]) {
self.appOpenAd = nil
self.delegate?.fa_openAd(ad: self, didLoadFail: error)
}
func tpSplashAdImpression(_ adInfo: [AnyHashable : Any]) {
self.delegate?.fa_openAdDidShow(ad: self)
}
func tpSplashAdShow(_ adInfo: [AnyHashable : Any], didFailWithError error: Error) {
self.appOpenAd = nil
self.delegate?.fa_openAd(ad: self, didDisplayFail: error)
}
func tpSplashAdClicked(_ adInfo: [AnyHashable : Any]) {
self.delegate?.fa_openAdDidClick(ad: self)
}
func tpSplashAdDismissed(_ adInfo: [AnyHashable : Any]) {
self.appOpenAd = nil
self.delegate?.fa_openAdDidDismiss(ad: self)
}
}
#endif