168 lines
		
	
	
		
			6.3 KiB
		
	
	
	
		
			Swift
		
	
	
			
		
		
	
	
			168 lines
		
	
	
		
			6.3 KiB
		
	
	
	
		
			Swift
		
	
	
| 
 | |
| //
 | |
| //  GuruIronSourceBannerAd.swift
 | |
| //  Pods
 | |
| //
 | |
| //  Created by 250102 on 2025/5/7.
 | |
| //
 | |
| import IronSource
 | |
| 
 | |
| class GuruIronSourceMRecAd : GuruMRecAd, LPMBannerAdViewDelegate, ISImpressionDataDelegate {
 | |
|     
 | |
|     private let viewController: UIViewController
 | |
|     private let adConfig: AdConfig
 | |
|     private var mrecAd: LPMBannerAdView?
 | |
|     private var contentView: UIView?
 | |
|     private var currentUid2Token: String = ""
 | |
|     private var horizontalOffset: CGFloat = 0.0
 | |
|     private var verticalOffset: CGFloat = 0.0
 | |
|     private var orientation: ScreenOrientation = .portrait
 | |
|     
 | |
|     private var contentId: Int {
 | |
|         return self.adConfig.engineId
 | |
|     }
 | |
|     
 | |
|     private let impressionListenerRegistrar: GuruIronSourceImpressionDataListenerRegistrar?
 | |
|         
 | |
|     public init(viewController: UIViewController, adConfig: AdConfig, impressionListenerRegistrar: GuruIronSourceImpressionDataListenerRegistrar? = nil) {
 | |
|         self.viewController = viewController
 | |
|         self.adConfig = adConfig
 | |
|         self.impressionListenerRegistrar = impressionListenerRegistrar
 | |
|         super.init(engineId: adConfig.engineId, adUnitId: adConfig.adUnitId, adPlatform: AdPlatform.ironSource)
 | |
|     }
 | |
|     
 | |
|     override func load() -> Bool {
 | |
|         self.mrecAd = LPMBannerAdView(adUnitId: adConfig.adUnitId)
 | |
|         self.mrecAd?.loadAd(with: self.viewController)
 | |
|         self.mrecAd?.setDelegate(self)
 | |
|         impressionListenerRegistrar?.unregister(listener: self)
 | |
|         impressionListenerRegistrar?.register(adUnitId: adUnitId, listener: self)
 | |
|         self.mrecAd?.setAdSize(LPMAdSize.mediumRectangle())
 | |
|         self.mrecAd?.pauseAutoRefresh()
 | |
|         return true
 | |
|     }
 | |
|     
 | |
|     public override func show(request: MRecShowRequest? = nil) -> Bool {
 | |
|         let placement = request?.placement
 | |
|         let newHorizontalOffset = request?.horizontalOffset
 | |
|         let newVerticalOffset = request?.verticalOffset
 | |
|         
 | |
|         var shouldReattach = false
 | |
|         
 | |
|         // Check if position or offset has changed
 | |
|         if let offset = newVerticalOffset, offset != Float(verticalOffset) {
 | |
|             verticalOffset = CGFloat(offset)
 | |
|             shouldReattach = true
 | |
|         }
 | |
|         
 | |
|         if let offset = newHorizontalOffset, offset != Float(horizontalOffset) {
 | |
|             horizontalOffset = CGFloat(offset)
 | |
|             shouldReattach = true
 | |
|         }
 | |
|         
 | |
|         // If we already have a view and parameters changed, remove it
 | |
|         if let contentView = contentView, shouldReattach {
 | |
|             logInfo("Parameters changed! Re-attaching view!")
 | |
|             contentView.removeFromSuperview()
 | |
|             self.contentView = nil
 | |
|         }
 | |
|         
 | |
|         // Create new content view if needed
 | |
|         if contentView == nil {
 | |
|             let container = UIView()
 | |
|             container.tag = contentId
 | |
|             
 | |
|             viewController.view.addSubview(container)
 | |
|             container.translatesAutoresizingMaskIntoConstraints = false
 | |
|             NSLayoutConstraint.activate([
 | |
|                 container.leadingAnchor.constraint(equalTo: viewController.view.leadingAnchor),
 | |
|                 container.trailingAnchor.constraint(equalTo: viewController.view.trailingAnchor),
 | |
|                 container.topAnchor.constraint(equalTo: viewController.view.topAnchor),
 | |
|                 container.bottomAnchor.constraint(equalTo: viewController.view.bottomAnchor)
 | |
|             ])
 | |
|             
 | |
|             contentView = container
 | |
|             
 | |
|             guard let mrecAd = mrecAd else { return false }
 | |
|             
 | |
|             // Standard MREC size is 300x250
 | |
|             let mrecWidth: CGFloat = 300
 | |
|             let mrecHeight: CGFloat = 250
 | |
|             
 | |
|             mrecAd.translatesAutoresizingMaskIntoConstraints = false
 | |
|             container.addSubview(mrecAd)
 | |
|             
 | |
|             NSLayoutConstraint.activate([
 | |
|                 mrecAd.leadingAnchor.constraint(equalTo: container.leadingAnchor, constant: horizontalOffset),
 | |
|                 mrecAd.topAnchor.constraint(equalTo: container.topAnchor, constant: verticalOffset),
 | |
|                 mrecAd.widthAnchor.constraint(equalToConstant: mrecWidth),
 | |
|                 mrecAd.heightAnchor.constraint(equalToConstant: mrecHeight)
 | |
|             ])
 | |
|             
 | |
|             mrecAd.isHidden = false
 | |
|             mrecAd.resumeAutoRefresh()
 | |
|         } else {
 | |
|             mrecAd?.resumeAutoRefresh()
 | |
|             mrecAd?.isHidden = false
 | |
|         }
 | |
|         
 | |
|         return true
 | |
|     }
 | |
|     
 | |
|     public override func hide() -> Bool {
 | |
|         guard let _ = viewController.view.viewWithTag(contentId) else {
 | |
|             return true
 | |
|         }
 | |
|         
 | |
|         if let mrecAd = mrecAd, !mrecAd.isHidden {
 | |
|             mrecAd.pauseAutoRefresh()
 | |
|             mrecAd.isHidden = true
 | |
|         }
 | |
|         
 | |
|         return true
 | |
|     }
 | |
|     
 | |
|     override public func destroy() -> Bool{
 | |
|         mrecAd?.removeFromSuperview()
 | |
|         mrecAd = nil
 | |
|         
 | |
|         contentView?.removeFromSuperview()
 | |
|         contentView = nil
 | |
|         
 | |
|         impressionListenerRegistrar?.unregister(listener: self)
 | |
|         return true
 | |
|     }
 | |
|     
 | |
|     func didLoadAd(with adInfo: LPMAdInfo) {
 | |
|         adLoaded(ad: IronSourceFusionAd(engineId: engineId, adType: adType, ad: adInfo))
 | |
|     }
 | |
|     
 | |
|     func didFailToLoadAd(withAdUnitId adUnitId: String, error: any Error) {
 | |
|         adLoadFailed(loadFailedInfo: LoadFailedInfo(engineId: engineId, adPlatform: adPlatform, adType: adType, adUnitId: adUnitId, error: SwiftFusionError(error)))
 | |
|     }
 | |
|     
 | |
|     func didClickAd(with adInfo: LPMAdInfo) {
 | |
|         adClicked(ad: IronSourceFusionAd(engineId: engineId, adType: adType, ad: adInfo))
 | |
|     }
 | |
|     
 | |
|     func didExpandAd(with adInfo: LPMAdInfo) {
 | |
|         adBannerExpanded(ad: IronSourceFusionAd(engineId: engineId, adType: adType, ad: adInfo))
 | |
|     }
 | |
|     
 | |
|     func didDisplayAd(with adInfo: LPMAdInfo) {
 | |
|         adDisplayed(ad: IronSourceFusionAd(engineId: engineId, adType: adType, ad: adInfo))
 | |
|     }
 | |
|     
 | |
|     func didCollapseAd(with adInfo: LPMAdInfo) {
 | |
|         adBannerCollapsed(ad: IronSourceFusionAd(engineId: engineId, adType: adType, ad: adInfo))
 | |
|     }
 | |
|     
 | |
|     func didFailToDisplayAd(with adInfo: LPMAdInfo, error: any Error) {
 | |
|         adDisplayFailed(ad: IronSourceFusionAd(engineId: engineId, adType: adType, ad: adInfo), error: SwiftFusionError(error))
 | |
|     }
 | |
|     
 | |
|     func impressionDataDidSucceed(_ impressionData: ISImpressionData!) {
 | |
|         adRevenuePaid(ad: IronSourceImpressedFusionAd(engineId: engineId, adType: adType, impressionData: impressionData))
 | |
|     }
 | |
| }
 |