I am creating a game in GameScene/SKScene using SpriteKit, and I am trying to display a AdMob Banner Ad. Somehow the Banner Ad won't show up.
This is the Banner Ad part of my code that I am currently using.
import SpriteKit
import GameplayKit
import GameKit
// AdMob
import GoogleMobileAds
import UIKit
class GameScene: SKScene, SKPhysicsContactDelegate, CheckboxNodeDelegate, GKGameCenterControllerDelegate, GADFullScreenContentDelegate, GADBannerViewDelegate {
var bannerView: GADBannerView!
var isBannerAdReady = false
override func didMove(to view: SKView) {
...
loadBannerAd()
}
func adView(_ bannerView: GADBannerView, didFailToReceiveAdWithError error: Error) {
print("Failed to load banner ad: \(error.localizedDescription)")
isBannerAdReady = false
}
func loadBannerAd() {
// bannerView = GADBannerView(adSize: kGADAdSizeBanner)
bannerView = GADBannerView(adSize: GADAdSizeFromCGSize(CGSize(width: 320, height: 50))) // Adjust the size as per your requirements
bannerView.adUnitID = "ca-app-pub-3940256099942544/2934735716" // Replace with your own ad unit ID
bannerView.rootViewController = self.view?.window?.rootViewController
bannerView.delegate = self
bannerView.load(GADRequest())
// showBannerAd()
}
func showBannerAd() {
if isBannerAdReady {
self.view?.addSubview(bannerView)
isBannerAdReady = false // Set the flag to false after showing the ad
} else {
print("No ad ready to be displayed.")
}
}
// Tells the delegate that a full-screen view will be presented in response to the user clicking on an ad.
func adViewWillPresentScreen(_ bannerView: GADBannerView) {
// Pause your game or perform any necessary actions when the ad is clicked
}
// Tells the delegate that the full-screen view will be dismissed.
func adViewWillDismissScreen(_ bannerView: GADBannerView) {
// Resume your game or perform any necessary actions when the ad is dismissed
}
// Tells the delegate that the full-screen view has been dismissed.
func adViewDidDismissScreen(_ bannerView: GADBannerView) {
// Load a new banner ad when the previous one is dismissed
bannerView.load(GADRequest())
}
// Tells the delegate that the user click will open another app (such as the App Store), backgrounding the current app.
func adViewWillLeaveApplication(_ bannerView: GADBannerView) {
// Handle the user leaving the app, e.g., pause the game
}
// Tells the delegate that a click on an ad failed to open a full-screen view.
func adView(_ bannerView: GADBannerView, didFailToPresentFullScreenContentWithError error: Error) {
print("Ad did fail to present full screen content.")
}
func bannerView(_ bannerView: GADBannerView, didFailToReceiveAdWithError error: Error) {
print("error mess: \(error)")
}
When running the showBannerAd()
, no Ad show up, and I get the message:
No ad ready to be displayed
What am I doing wrong? I added the Ad Unit for over 24 hours ago, and I am using the correct App ID. I am already using interstitial
ads in my app that is working perfectly.