174 lines
		
	
	
		
			5.9 KiB
		
	
	
	
		
			Swift
		
	
	
		
		
			
		
	
	
			174 lines
		
	
	
		
			5.9 KiB
		
	
	
	
		
			Swift
		
	
	
|  | // GuruMaxBannerAd.swift | ||
|  | // Implementation for MAX banner ads | ||
|  | // Corresponds to GuruMaxBannerAd.kt in Android implementation | ||
|  | 
 | ||
|  | import Foundation | ||
|  | import UIKit | ||
|  | import AppLovinSDK | ||
|  | 
 | ||
|  | class GuruMaxBannerView : GuruBannerView { | ||
|  |     func view() -> UIView { | ||
|  |         return bannerAdView | ||
|  |     } | ||
|  |      | ||
|  |     func setPlacementName(placement: String) { | ||
|  |         bannerAdView.placement = placement | ||
|  |     } | ||
|  |      | ||
|  |     func onShow() { | ||
|  |         bannerAdView.isHidden = false | ||
|  |         bannerAdView.startAutoRefresh() | ||
|  |     } | ||
|  |      | ||
|  |     func onHide() { | ||
|  |         bannerAdView.setExtraParameterForKey("allow_pause_auto_refresh_immediately", value: "true") | ||
|  |         bannerAdView.isHidden = true | ||
|  |         bannerAdView.stopAutoRefresh() | ||
|  |     } | ||
|  |      | ||
|  |     public var bannerAdView: MAAdView | ||
|  |      | ||
|  |     public init(_ banner: MAAdView) { | ||
|  |         self.bannerAdView = banner | ||
|  |     } | ||
|  | } | ||
|  | 
 | ||
|  | public class GuruMaxBannerAd: GuruBannerAd, MAAdViewAdDelegate, MAAdRevenueDelegate { | ||
|  |      | ||
|  |     private let viewController: UIViewController | ||
|  |     private let adConfig: AdConfig | ||
|  |      | ||
|  |     private var bannerAd: MAAdView? { return bannerAdCoordinator?.bannerAd.bannerAdView} | ||
|  |     private var bannerAdCoordinator: GuruBannerCoordinator<GuruMaxBannerView>? | ||
|  |     private var contentView: UIView? | ||
|  |     private var currentUid2Token: String = "" | ||
|  |      | ||
|  |     private var contentId: Int { | ||
|  |         return self.adConfig.engineId | ||
|  |     } | ||
|  |      | ||
|  |     public init(viewController: UIViewController, adConfig: AdConfig) { | ||
|  |         self.viewController = viewController | ||
|  |         self.adConfig = adConfig | ||
|  |         super.init(engineId: adConfig.engineId, adUnitId: adConfig.adUnitId, adPlatform: AdPlatform.max) | ||
|  |     } | ||
|  |      | ||
|  |     override public func load() -> Bool { | ||
|  |         logInfo("request load") | ||
|  |         let bannerAd = MAAdView(adUnitIdentifier: adConfig.adUnitId, adFormat: MAAdFormat.banner) | ||
|  |                  | ||
|  |         bannerAd.delegate = self | ||
|  |         bannerAd.revenueDelegate = self | ||
|  |         bannerAd.setExtraParameterForKey("allow_pause_auto_refresh_immediately", value: "true") | ||
|  |          | ||
|  |         if let uid2Token = AdsHelper.getUid2Token() { | ||
|  |             bannerAd.setExtraParameterForKey("uid2_token", value: uid2Token) | ||
|  |             currentUid2Token = uid2Token | ||
|  |         } | ||
|  |          | ||
|  |         bannerAd.stopAutoRefresh() | ||
|  |          | ||
|  |         if let amazonSlotId = adConfig.adAmazonSlotId, !amazonSlotId.isEmpty { | ||
|  |             // In a real implementation, you would load Amazon ads here | ||
|  |             logDebug("Would load Amazon banner ad with slot ID: \(amazonSlotId)") | ||
|  |              | ||
|  |             // Simulate direct load for now | ||
|  |             bannerAd.loadAd() | ||
|  |         } else { | ||
|  |             bannerAd.loadAd() | ||
|  |         } | ||
|  |          | ||
|  |         self.bannerAdCoordinator = GuruBannerCoordinator(viewController: viewController, bannerAd: GuruMaxBannerView(bannerAd), contentId: contentId) | ||
|  |          | ||
|  |         return true | ||
|  |     } | ||
|  |      | ||
|  |     public override func show(_ request: BannerShowRequest? = nil) -> Bool { | ||
|  |         logInfo("request show") | ||
|  |         let result = self.bannerAdCoordinator?.show(request) | ||
|  |                  | ||
|  |         // Update UID2 token if needed | ||
|  |         if let uid2Token = AdsHelper.getUid2Token(), !uid2Token.isEmpty, uid2Token != currentUid2Token { | ||
|  |             bannerAd?.setExtraParameterForKey("uid2_token", value: uid2Token) | ||
|  |             currentUid2Token = uid2Token | ||
|  |             logInfo("BannerAd refresh uid2_token \(currentUid2Token)") | ||
|  |         } | ||
|  |          | ||
|  |         return result ?? false | ||
|  |     } | ||
|  |      | ||
|  |     public override func hide() -> Bool { | ||
|  |         logInfo("request hide") | ||
|  |         return bannerAdCoordinator?.hide() ?? false | ||
|  |     } | ||
|  |      | ||
|  |     override public func destroy() -> Bool { | ||
|  |         logInfo("request destroy") | ||
|  |         bannerAd?.delegate = nil | ||
|  |         bannerAd?.revenueDelegate = nil | ||
|  |         let _ = bannerAdCoordinator?.destroy() | ||
|  |         bannerAdCoordinator = nil | ||
|  |         return true | ||
|  |     } | ||
|  |      | ||
|  |     override public func updateOrientation(orientation: ScreenOrientation) -> Bool { | ||
|  |         logInfo("update orientation to \(orientation)") | ||
|  | 
 | ||
|  |         return bannerAdCoordinator?.updateOrientation(orientation: orientation) ?? false | ||
|  |     } | ||
|  |      | ||
|  |     // MARK: - MAXAdViewAdDelegate methods | ||
|  |      | ||
|  |     public func didLoad(_ ad: MAAd) { | ||
|  |         logInfo("loaded from \(ad.networkName)") | ||
|  |         listener?.onAdLoaded(ad: MaxFusionAd(engineId: engineId, adType: AdType.Banner, ad: ad)) | ||
|  |     } | ||
|  |      | ||
|  |     public func didFailToLoadAd(forAdUnitIdentifier adUnitIdentifier: String, withError error: MAError) { | ||
|  |         logError("load failed, \(error.code) - \(error.message)") | ||
|  |         let loadFailedInfo = LoadFailedInfo( | ||
|  |             engineId: engineId, | ||
|  |             adPlatform: AdPlatform.max, | ||
|  |             adType: AdType.Banner, | ||
|  |             adUnitId: adUnitIdentifier, | ||
|  |             error: MaxFusionError(error) | ||
|  |         ) | ||
|  |         listener?.onAdLoadFailed(loadFailedInfo: loadFailedInfo) | ||
|  |     } | ||
|  |      | ||
|  |     public func didDisplay(_ ad: MAAd) { | ||
|  |         listener?.onAdDisplayed(ad: MaxFusionAd(engineId: engineId, adType: AdType.Banner, ad: ad)) | ||
|  |     } | ||
|  |      | ||
|  |     public func didHide(_ ad: MAAd) { | ||
|  |         listener?.onAdHidden(ad: MaxFusionAd(engineId: engineId, adType: AdType.Banner, ad: ad)) | ||
|  |     } | ||
|  |      | ||
|  |     public func didClick(_ ad: MAAd) { | ||
|  |         listener?.onAdClicked(ad: MaxFusionAd(engineId: engineId, adType: AdType.Banner, ad: ad)) | ||
|  |     } | ||
|  |      | ||
|  |     public func didFail(toDisplay ad: MAAd, withError error: MAError) { | ||
|  |         logError("display failed, \(error.code) - \(error.message)") | ||
|  |         listener?.onAdDisplayFailed( | ||
|  |             ad: MaxFusionAd(engineId: engineId, adType: AdType.Banner, ad: ad), | ||
|  |             error: MaxFusionError(error) | ||
|  |         ) | ||
|  |     } | ||
|  |      | ||
|  |     public func didExpand(_ ad: MAAd) { | ||
|  |         // In a real implementation, you would call adBannerExpanded | ||
|  |     } | ||
|  |      | ||
|  |     public func didCollapse(_ ad: MAAd) { | ||
|  |         // In a real implementation, you would call adBannerCollapsed | ||
|  |     } | ||
|  |      | ||
|  |     // MARK: - MAXAdRevenueDelegate methods | ||
|  |      | ||
|  |     public func didPayRevenue(for ad: MAAd) { | ||
|  |         logInfo("revenue paid") | ||
|  |         listener?.onAdRevenuePaid(ad: MaxFusionAd(engineId: engineId, adType: AdType.Banner, ad: ad)) | ||
|  |     } | ||
|  | } |