A while back I implemented successfully the code in SwiftUI to show banner ads. Since my app wasn't on the AppStore yet, I could only display test ads. Then, I added my app to my Google AdMob profile, and it has been verified (almost 2 weeks ago). So I updated the pods (now Google Mobile Ads SDK is on version 8.13.0), and I waited until today to see if the verification process was maybe taking longer than expected. However, now I can't see proper ads nor test ads. Here is my code:
GoogleAdsManager.swift
import SwiftUI
import GoogleMobileAds
import UIKit
struct Banner:View{
let testIdGoogleBanner = "ca-app-pub-3940256099942544/2934735716"
var body: some View{
HStack{
Spacer()
BannerAdView(bannerId: testIdGoogleBanner)
.frame(width: 320, height: 50, alignment: .center)
Spacer()
}
}
}
struct BannerAdView : UIViewRepresentable {
var bannerId : String
let banner = GADBannerView(adSize: GADAdSizeBanner)
func makeCoordinator() -> Coordinator { Coordinator(self) }
func makeUIView(context: UIViewRepresentableContext<BannerAdView>) -> GADBannerView {
banner.adUnitID = bannerId
banner.rootViewController = UIApplication.shared.windows.first?.rootViewController
banner.load(GADRequest())
banner.delegate = context.coordinator
return banner
}
func updateUIView(_ uiView: GADBannerView, context: UIViewRepresentableContext<BannerAdView>) { }
class Coordinator : NSObject, GADBannerViewDelegate {
var parent : BannerAdView
init(_ parent : BannerAdView) { self.parent = parent }
func adViewDidReceiveAd(_bannerView: GADBannerView) {
print("Ad received")
}
func bannerView(_ bannerView: GADBannerView, didFailToReceiveAdWithError error: Error) {
print("Error when receiving ads: \(error.localizedDescription)")
}
}
}
MyApp.swift
import SwiftUI
import GoogleMobileAds
import AppTrackingTransparency
@main
struct MyApp: App {
init() {
if ATTrackingManager.trackingAuthorizationStatus == .notDetermined {
} else {
ATTrackingManager.requestTrackingAuthorization { status in
GADMobileAds.sharedInstance().start(completionHandler: nil)
}
}
}
var body: some Scene {
WindowGroup {
ContentView()
}
}
}
And then in the ContentView:
import SwiftUI
import GoogleMobileAds
import UIKit
import AppTrackingTransparency
import AdSupport
struct ContentView: View {
var body: some View {
ZStack {
//Other elements
VStack {
Spacer()
Banner()
}
.edgesIgnoringSafeArea(.bottom)
}
.onAppear {
ATTrackingManager.requestTrackingAuthorization { status in
switch status {
case .authorized:
print("Authorized")
print(ASIdentifierManager.shared().advertisingIdentifier)
case .denied:
print("Denied")
case .notDetermined:
print("Not Determined")
case .restricted:
print("Restricted")
@unknown default:
print("Unknown")
}
}
}
}
}
I have implemented the Info.plist exactly as stated here, I only changed the GADApplicationIdentifier with my AdMob app ID. The other thing that I changed are the kGADAdSizeBanner, which were marked as deprecated, and so I changed them to GADAdSizeBanner.
In the Ad implementation (in the BannerAdView) there's also a Coordinator, which in the console prints "Error when receiving ads: No ad to show".
Other questions on "No ad to show" were suggesting to wait for the app approval, or to wait for a few days after it has been approved, but I waited and nothing changed. I have tried other solutions/implementations, such as this one, this one, and others, but the result is always the same.
What am I doing wrong?