I am having an issue getting my simple UI for an Admob banner to work using swiftUI and view controller.
Controller:
import UIKit
import Foundation
import GoogleMobileAds
import ToastViewSwift
public class AdsScreenViewController: UIViewController, GADBannerViewDelegate {
var auID = ""
init (auID: String){
self.auID = auID
super.init(nibName: nil, bundle: nil)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
public override func viewDidLoad() {
super.viewDidLoad()
var bannerView: GADBannerView!
bannerView = GADBannerView(adSize: kGADAdSizeBanner)
addBannerViewToView(bannerView)
bannerView.adUnitID = auID
bannerView.rootViewController = self
bannerView.load(GADRequest())
bannerView.delegate = self
}
func addBannerViewToView(_ bannerView: GADBannerView) {
bannerView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(bannerView)
view.addConstraints(
[NSLayoutConstraint(item: bannerView,
attribute: .bottom,
relatedBy: .equal,
toItem: bottomLayoutGuide,
attribute: .top,
multiplier: 1,
constant: 0),
NSLayoutConstraint(item: bannerView,
attribute: .centerX,
relatedBy: .equal,
toItem: view,
attribute: .centerX,
multiplier: 1,
constant: 0)
])
}
public func bannerViewDidReceiveAd(_ bannerView: GADBannerView) {
print("bannerViewDidReceiveAd")
}
public func bannerView(_ bannerView: GADBannerView, didFailToReceiveAdWithError error: Error) {
print("bannerView:didFailToReceiveAdWithError: \(error.localizedDescription)")
}
public func bannerViewDidRecordImpression(_ bannerView: GADBannerView) {
print("bannerViewDidRecordImpression")
}
public func bannerViewWillPresentScreen(_ bannerView: GADBannerView) {
print("bannerViewWillPresentScreen")
}
public func bannerViewWillDismissScreen(_ bannerView: GADBannerView) {
print("bannerViewWillDIsmissScreen")
}
public func bannerViewDidDismissScreen(_ bannerView: GADBannerView) {
print("bannerViewDidDismissScreen")
}
}
Swift UI:
import SwiftUI
import UIKit
struct TestAdsView: View {
@State private var auID = ""
@State private var auType = 1
@State private var isPresented = false
var body: some View {
List {
VStack(alignment: .leading, content: {
Text("AdUnit")
.font(.footnote).fontWeight(.medium)
TextField("adunitid", text: $auID)
.font(.headline)
})
VStack(alignment: .leading, content: {
Button(action: {
self.auID = auID
}, label: {
HStack {
Text("Show Ad")
}
})
BannerViewController(auID: auID)
}
)}
}
struct TestAdsView_Previews: PreviewProvider {
static var previews: some View {
TestAdsView()
}
}
struct BannerViewController: UIViewControllerRepresentable {
var auID: String
public typealias UIViewControllerType = UIViewController
func makeUIViewController(context: UIViewControllerRepresentableContext<BannerViewController>) -> BannerViewController.UIViewControllerType {
return AdsScreenViewController(auID: auID)
}
func updateUIViewController(_ uiViewController: BannerViewController.UIViewControllerType, context: UIViewControllerRepresentableContext<BannerViewController>) {
let controller = AdsScreenViewController(auID: auID)
controller.auID = self.auID
}
}
Everything compiles fine and it runs showing the TextView. But when entering the id it's not invoking the bannerView.load, I thought the UIViewControllerRepresentable auto updates like a listener on the view and it should be invoked but nothing is happening.

loadwhen the id is entered? As of now it is only called whenviewDidLoadso you call it once with the id is blank.updateUIViewControlleryoumakeonce and update or call methods the rest of the times. There are many examples in SO on how to implement google ads.var auID: Stringwith@Binding var auID: StringinBannerViewControlleryou might get it to work but you will likely get flagged by Google because you will be recreating the ad too many times. Because oflet controller = AdsScreenViewController(auID: auID)onupdateUIViewController