Skip to content

Commit

Permalink
Modified ios BannerExecutor to display bannerView only after bannerVi…
Browse files Browse the repository at this point in the history
…ewDidReceiveAd is called
  • Loading branch information
lightning1377 committed Dec 6, 2024
1 parent 86ffb91 commit 7020045
Showing 1 changed file with 24 additions and 21 deletions.
45 changes: 24 additions & 21 deletions ios/Sources/AdMobPlugin/Banner/BannerExecutor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import GoogleMobileAds
class BannerExecutor: NSObject, GADBannerViewDelegate {
weak var plugin: AdMobPlugin?
var bannerView: GADBannerView!
var adPosition: String = ""
var Margin: Int = 0

func showBanner(_ call: CAPPluginCall, _ request: GADRequest, _ adUnitID: String) {
if let rootViewController = plugin?.getRootVC() {
Expand Down Expand Up @@ -109,20 +111,36 @@ class BannerExecutor: NSObject, GADBannerViewDelegate {

private func addBannerViewToView(_ bannerView: GADBannerView, _ adPosition: String, _ Margin: Int) {
removeBannerViewToView()
bannerView.translatesAutoresizingMaskIntoConstraints = false
bannerView.tag = 2743243288699 // rand
self.Margin = Margin
self.adPosition = adPosition
}

private func removeBannerViewToView() {
if let rootViewController = plugin?.getRootVC() {
if let subView = rootViewController.view.viewWithTag(2743243288699) {
bannerView.delegate = nil
NSLog("AdMob: find subView")
subView.removeFromSuperview()
}
}
}

bannerView.translatesAutoresizingMaskIntoConstraints = false
bannerView.tag = 2743243288699 // rand
/// Tells the delegate an ad request loaded an ad.
func bannerViewDidReceiveAd(_ bannerView: GADBannerView) {
NSLog("bannerViewDidReceiveAd")
if let rootViewController = plugin?.getRootVC() {
rootViewController.view.addSubview(bannerView)
rootViewController.view.addConstraints(
[NSLayoutConstraint(item: bannerView,
attribute: adPosition == "TOP_CENTER" ? .top : .bottom,
attribute: self.adPosition == "TOP_CENTER" ? .top : .bottom,
relatedBy: .equal,
toItem: rootViewController.view.safeAreaLayoutGuide,
attribute: adPosition == "TOP_CENTER" ? .top : .bottom,
attribute: self.adPosition == "TOP_CENTER" ? .top : .bottom,
multiplier: 1,
constant: CGFloat(Int(Margin) * -1)),
NSLayoutConstraint(item: bannerView,
constant: CGFloat(Int(self.Margin) * -1)),
NSLayoutConstraint(item: bannerView,
attribute: .centerX,
relatedBy: .equal,
toItem: rootViewController.view,
Expand All @@ -131,21 +149,6 @@ class BannerExecutor: NSObject, GADBannerViewDelegate {
constant: 0)
])
}
}

private func removeBannerViewToView() {
if let rootViewController = plugin?.getRootVC() {
if let subView = rootViewController.view.viewWithTag(2743243288699) {
bannerView.delegate = nil
NSLog("AdMob: find subView")
subView.removeFromSuperview()
}
}
}

/// Tells the delegate an ad request loaded an ad.
func bannerViewDidReceiveAd(_ bannerView: GADBannerView) {
NSLog("bannerViewDidReceiveAd")

self.plugin?.notifyListeners(BannerAdPluginEvents.SizeChanged.rawValue, data: [
"width": bannerView.frame.width,
Expand Down

0 comments on commit 7020045

Please sign in to comment.