131 lines
		
	
	
		
			4.1 KiB
		
	
	
	
		
			Swift
		
	
	
		
		
			
		
	
	
			131 lines
		
	
	
		
			4.1 KiB
		
	
	
	
		
			Swift
		
	
	
|  | // | ||
|  | //  GuruAdmobAdsSdk.swift | ||
|  | //  Pods | ||
|  | // | ||
|  | //  Created by 250102 on 2025/5/9. | ||
|  | // | ||
|  | import GoogleMobileAds | ||
|  | import FBAudienceNetwork | ||
|  | 
 | ||
|  | class GuruAdMobAdsSdk : GuruAdsSdk { | ||
|  |     private static var instance: GuruAdMobAdsSdk? | ||
|  | 
 | ||
|  |      | ||
|  |     public var adPlatform: AdPlatform { | ||
|  |         return AdPlatform.adMob | ||
|  |     } | ||
|  |      | ||
|  |     private var initialized = false | ||
|  |      | ||
|  |     private static var interstitialAds: [String: GuruInterstitialAd] = [:] | ||
|  |     private static var rewardedAds: [String: GuruRewardedAd] = [:] | ||
|  |     private static var bannerAds: [String: GuruBannerAd] = [:] | ||
|  |     private static var mrecAds: [String: GuruMRecAd] = [:] | ||
|  |      | ||
|  |     private func logDebug(_ message: String) { | ||
|  |         Logger.d(tag: "GuruAdMobAdsSdk", message: message) | ||
|  |     } | ||
|  |      | ||
|  |     private func logInfo(_ message: String) { | ||
|  |         Logger.i(tag: "GuruAdMobAdsSdk", message: message) | ||
|  |     } | ||
|  |      | ||
|  |     private func logWarn(_ message: String) { | ||
|  |         Logger.w(tag: "GuruAdMobAdsSdk", message: message) | ||
|  |     } | ||
|  |      | ||
|  |     private func logError(_ message: String) { | ||
|  |         Logger.e(tag: "GuruAdMobAdsSdk", message: message) | ||
|  |     } | ||
|  |      | ||
|  |     private let viewController: UIViewController | ||
|  |      | ||
|  |     private init(viewController: UIViewController) { | ||
|  |         self.viewController = viewController | ||
|  |     } | ||
|  |      | ||
|  |     public static func obtain(viewController: UIViewController) -> GuruAdsSdk { | ||
|  |         if let existingInstance = instance { | ||
|  |             return existingInstance | ||
|  |         } | ||
|  |          | ||
|  |         let newInstance = GuruAdMobAdsSdk(viewController: viewController) | ||
|  |         instance = newInstance | ||
|  |         return newInstance | ||
|  |     } | ||
|  |      | ||
|  |     public func initialize(adsProfile: AdsProfile) async -> Bool { | ||
|  |         guard !initialized else { | ||
|  |             logWarn("SDK already initialized! Ignoring.") | ||
|  |             return false | ||
|  |         } | ||
|  |          | ||
|  |         initialized = true | ||
|  |          | ||
|  |         if(adsProfile.debugMode) { | ||
|  |              | ||
|  |         } | ||
|  |         return await withCheckedContinuation(isolation: MainActor.shared) { continuation in | ||
|  |             FBAdSettings.setAdvertiserTrackingEnabled(true) | ||
|  |             GADMobileAds.sharedInstance().start(completionHandler: { status in | ||
|  |                 var initedAtLeastOne = false | ||
|  |                 status.adapterStatusesByClassName.forEach {key, value in | ||
|  |                     if(value.state == GADAdapterInitializationState.ready) { | ||
|  |                         self.logInfo("adapter \(key) ready, latency \(value.latency)") | ||
|  |                         initedAtLeastOne = true | ||
|  |                     } else { | ||
|  |                         self.logWarn("adapter \(key) not ready, latency \(value.latency)") | ||
|  |                     } | ||
|  |                 } | ||
|  |                 continuation.resume(returning: initedAtLeastOne) | ||
|  |             }) | ||
|  |         } | ||
|  | 
 | ||
|  |     } | ||
|  |      | ||
|  |     public func obtainInterstitialAd(adConfig: AdConfig) -> GuruInterstitialAd { | ||
|  |         if let existingAd = Self.interstitialAds[adConfig.cacheKey] { | ||
|  |             return existingAd | ||
|  |         } | ||
|  |          | ||
|  |         let newAd = GuruAdMobInterstitialAd(viewController: viewController, adConfig: adConfig) | ||
|  |         Self.interstitialAds[adConfig.cacheKey] = newAd | ||
|  |         return newAd | ||
|  |     } | ||
|  |      | ||
|  |     public func obtainRewardedAd(adConfig: AdConfig) -> GuruRewardedAd { | ||
|  |         if let existingAd = Self.rewardedAds[adConfig.cacheKey] { | ||
|  |             return existingAd | ||
|  |         } | ||
|  |          | ||
|  |         let newAd = GuruAdMobRewardedAd(viewController: viewController, adConfig: adConfig) | ||
|  |         Self.rewardedAds[adConfig.cacheKey] = newAd | ||
|  |         return newAd | ||
|  |     } | ||
|  |      | ||
|  |     public func obtainBannerAd(adConfig: AdConfig) -> GuruBannerAd { | ||
|  |         if let existingAd = Self.bannerAds[adConfig.cacheKey] { | ||
|  |             return existingAd | ||
|  |         } | ||
|  |          | ||
|  |         let newAd = GuruAdMobBannerAd(viewController: viewController, adConfig: adConfig) | ||
|  |         Self.bannerAds[adConfig.cacheKey] = newAd | ||
|  |         return newAd | ||
|  |     } | ||
|  |      | ||
|  |     public func obtainMRecAd(adConfig: AdConfig) -> GuruMRecAd { | ||
|  |         if let existingAd = Self.mrecAds[adConfig.cacheKey] { | ||
|  |             return existingAd | ||
|  |         } | ||
|  |          | ||
|  |         let newAd = GuruAdMobMRecAd(viewController: viewController, adConfig: adConfig) | ||
|  |         Self.mrecAds[adConfig.cacheKey] = newAd | ||
|  |         return newAd | ||
|  |     } | ||
|  |      | ||
|  |     public func processCrossAction(action: AdsCrossAction) -> Bool { | ||
|  |         return false | ||
|  |     } | ||
|  |      | ||
|  | } |