Files
Woka_Native_iOS/WOKA/Helpers/AdResuable/AdReusable.swift
Bilal dcbbfc3417 - made theme 2 banner dynamic, if ad loads then only it will be shown
- made radio ad dynamic, now ad will show up only if ad is received
- added a retry count to fm, it will try 4 times to connect to the server if not then showing the reload btn
2024-09-19 01:22:04 +05:30

62 lines
2.1 KiB
Swift

//
// AdReusable.swift
// WOKA
//
// Created by MacBook Pro on 16/09/24.
//
import UIKit
import GoogleMobileAds
class AdReusable {
// Singleton instance
static let sharedInstance = AdReusable()
// Private init to prevent creating additional instances
private init() {}
// Function to set up banner ads with an existing banner view
func setupBannerAd(bannerView: GADBannerView, in containerView: UIView, adUnitID: String, viewController: GADBannerViewDelegate & UIViewController, height: CGFloat? = 0.0, width: CGFloat? = 0.0) {
// Adjust the view width and height based on provided values
let viewWidth = containerView.frame.inset(by: containerView.safeAreaInsets).width - (width ?? 0)
let viewHeight = containerView.frame.height - (height ?? 0)
// Create the adaptive banner size
// let adaptiveSize = GADPortraitAnchoredAdaptiveBannerAdSizeWithWidth(viewWidth)
let adaptiveSize = GADPortraitInlineAdaptiveBannerAdSizeWithWidth(viewWidth)
bannerView.adSize = adaptiveSize
// Set the delegate to handle banner events
bannerView.delegate = viewController
// Adjust the banner's frame
bannerView.frame = CGRect(x: 0, y: 0, width: viewWidth, height: viewHeight)
// Center the banner within the containerView
bannerView.center = CGPoint(x: containerView.frame.width / 2, y: containerView.frame.height / 2)
// If height is provided, add rounded corners
if let heightValue = height, heightValue > 0 {
bannerView.layer.cornerRadius = 4
bannerView.clipsToBounds = true
}
// Add the banner view to the container
containerView.addSubview(bannerView)
// Set the ad unit ID and root view controller for the banner
bannerView.adUnitID = adUnitID
bannerView.rootViewController = viewController
// Load the ad request
bannerView.load(GADRequest())
// Ensure the layout is updated
containerView.layoutIfNeeded()
}
}