I'd like to display AdMob interstitials in my app. I manage to display test ads. As far as I understand to display test ads you should add test devices, so this is what I do. When I remove the test devices though, I don't get the real ads. I'd like to test somehow that real ads will be displayed. When I remove the test devices I get interstitial(_ ad: GADInterstitial, didFailToReceiveAdWithError error: GADRequestError) delegate method called and the error is: Request Error: No ad to show. This is my code based on the instructions here:
class MoviesViewController: UIViewController {
var interstitial: GADInterstitial!
override func viewDidLoad() {
super.viewDidLoad()
interstitial = createAndLoadInterstitial()
}
func createAndLoadInterstitial() -> GADInterstitial {
let interstitial = GADInterstitial(adUnitID: "<Ad-Unit-ID>")
interstitial.delegate = self
let request = GADRequest()
//request.testDevices = ["<Device-Test-ID>"]
interstitial.load(request)
return interstitial
}
#IBAction func didTapStartOver(_ sender: Any) {
if interstitial.isReady {
interstitial.present(fromRootViewController: self)
} else {
print("Ad wasn't ready")
interstitial = createAndLoadInterstitial()
}
}
GADInterstitialDelegate:
extension MoviesViewController : GADInterstitialDelegate {
/// Tells the delegate an ad request succeeded.
func interstitialDidReceiveAd(_ ad: GADInterstitial) {
print("interstitialDidReceiveAd")
}
/// Tells the delegate an ad request failed.
func interstitial(_ ad: GADInterstitial, didFailToReceiveAdWithError error: GADRequestError) {
print("interstitial:didFailToReceiveAdWithError: \(error.localizedDescription)")
}
/// Tells the delegate that an interstitial will be presented.
func interstitialWillPresentScreen(_ ad: GADInterstitial) {
print("interstitialWillPresentScreen")
}
/// Tells the delegate the interstitial is to be animated off the screen.
func interstitialWillDismissScreen(_ ad: GADInterstitial) {
dismiss(animated: false, completion: nil)
print("interstitialWillDismissScreen")
}
func interstitialDidDismissScreen(_ ad: GADInterstitial) {
print("interstitialDidDismissScreen")
interstitial = createAndLoadInterstitial()
}
/// Tells the delegate that a user click will open another app
/// (such as the App Store), backgrounding the current app.
func interstitialWillLeaveApplication(_ ad: GADInterstitial) {
print("interstitialWillLeaveApplication")
}
}
I added a mediation group and was able to receive one real ad. After that the issue persists (no more ads to show).
Changing the device for testing from iPad to iPhone also seems to help - the issue seems to be reproducible only on iPad.
I never used the test device feature. I always just plugged in the provided test ad id that is mentioned in their guides. When ready to test real ads, just switch to the live ad id they provide when you create the ad. Then it's good to go without changing any other code.
Related
I'm displaying interstitials with admob 8.0.0 after new screen is loaded in my app, but something is wrong because the present function is being called but interstitial is not being displayed.
If I try displaying them with a button that calls the present function, then it's displayed but only one time, only the first time I press the button. If i press it more times, no more interstitials are being called, and the code is executing the line of the present function. None of the GADFullScreenContentDelegate delegate events are being called.
Can someone explain me what is wrong?
I'm loading interstitials this way:
self.gadInterstitial?.fullScreenContentDelegate = self
let request = GADRequest()
GADInterstitialAd.load(withAdUnitID:self.adId, request: request, completionHandler: { [self] ad, error in
if let error = error {
logger.error(error.localizedDescription)
return
}
gadInterstitial = ad
logger.debug("Loaded Ad Mob interstitial view")
loaded = true
}
)
extension AdMobAdEntity: GADFullScreenContentDelegate {
/// Tells the delegate that the ad failed to present full screen content.
func ad(_ ad: GADFullScreenPresentingAd, didFailToPresentFullScreenContentWithError error: Error) {
print("Ad did fail to present full screen content.")
}
/// Tells the delegate that the ad presented full screen content.
func adDidPresentFullScreenContent(_ ad: GADFullScreenPresentingAd) {
print("Ad did present full screen content.")
}
/// Tells the delegate that the ad dismissed full screen content.
func adDidDismissFullScreenContent(_ ad: GADFullScreenPresentingAd) {
print("Ad did dismiss full screen content.")
}
}
I'm displaying them after new screen has been loaded, or when the button interstitial is pressed this way:
override func showInterstitial(viewController: SectionViewController?) {
guard loaded, let controller = viewController else {
return
}
gadInterstitial?.present(fromRootViewController: controller)
logger.debug("Displaying interstitial view")
}
You dismiss ads after call loadInterstitial() function
func loadInterstitial() {
let request = GADRequest()
GADInterstitialAd.load(
withAdUnitID: self.adId, request: request
) { (ad, error) in
if let error = error {
logger.error(error.localizedDescription)
return
}
self.gadInterstitial = ad
logger.debug("Loaded Ad Mob interstitial view")
loaded = true
self.gadInterstitial?.fullScreenContentDelegate = self
}
}
func adDidDismissFullScreenContent(_ ad: GADFullScreenPresentingAd) {
print("Ad did dismiss full screen content.")
loaded = false
loadInterstitial()
}
override func showInterstitial(viewController: SectionViewController?) {
guard loaded, let controller = viewController else {
loadInterstitial()
return
}
gadInterstitial?.present(fromRootViewController: controller)
logger.debug("Displaying interstitial view")
}
I have integrated the google AdMob interstitial ads in my iOS App using the following code:-
import GoogleMobileAds
class ViewController: UIViewController, GADInterstitialDelegate {
var interstitial: GADInterstitial!
override func viewDidLoad() {
super.viewDidLoad()
interstitial = GADInterstitial(adUnitID: "")
interstitial.delegate = self
let request = GADRequest()
interstitial.load(request)
}
// Google Ads
func interstitialDidReceiveAd(_ ad: GADInterstitial) {
interstitial.present(fromRootViewController: self)
}
}
When I run test ads, I am able to see the interstitial ads! But, after replacing the test ID with my Interstitial Ad Unit ID, I cannot see the live interstitial ads on my iOS app. Could anyone please help me to resolve this issue? Thanks:)
These are the logs after running my app on my iOS Device:-
You must test your google ads with test id like below as per google documentation.
Interstitial Test id
ca-app-pub-3940256099942544/4411468910
Adding test id and try again.
I am trying to load this google interstitial ad when the game is over.
if (self.interstitial.isReady)
{
self.interstitial.presentFromRootViewController(self)
}
But I am getting an error that says "Cannot convert value of type 'GameScene' to expected argument type 'UIViewController!".
I have used the same lines of code with my other apps that don't use sprite kit, is it different with sprite kit?
Yes it's different in SpriteKit because you trying to present from a SKScene and not a UIViewController.
Try this and see if it works
if (self.interstitial.isReady) {
self.interstitial.presentFromRootViewController(self.view?.window?.rootViewController)
}
If you are getting a nil crash now than you did not init the ad property correctly. You should also have some checks to ensure this does not happen.
This is how the adMob code from my gitHub helper looks.
You should have a property like so
var interstitial: GADInterstitial?
Than in view didLoad you should preload the ad
interstitial = adMobLoadInterAd()
This is pre-loading code.
func adMobLoadInterAd() -> GADInterstitial {
Debug.print("AdMob inter loading...")
let googleInterAd = GADInterstitial(adUnitID: "Your adMob ID")
googleInterAd.delegate = self
let request = GADRequest()
request.testDevices = [kGADSimulatorID] // DEBUG only
googleInterAd.loadRequest(request)
return googleInterAd
}
Than when you want to show an ad you call this
func adMobShowInterAd() {
guard interstitial != nil && interstitial!.isReady else { // calls interDidReceiveAd
Debug.print("AdMob inter is not ready, reloading")
interstitial = adMobLoadInterAd()
return
}
Debug.print("AdMob inter showing...")
interstitial?.presentFromRootViewController(self.view?.window?.rootViewController)
}
Than finally in the delegate methods you should pre load a new ad when the current ad is dismissed.
func interstitialDidDismissScreen(ad: GADInterstitial!) {
Debug.print("AdMob inter closed")
interstitial = adMobLoadInterAd()
}
I'm trying to reward a user for watching a video with AdMob. Although I couldn't find any tutorial on the subject, I found these two classes in the documentation:
GADRewardBasedVideoAd
GADRewardBasedVideoAdDelegate
I've set up the display of a GADRewardBasedVideoAd with the following View Controller code:
class MarketController: UIViewController, GADRewardBasedVideoAdDelegate {
override func viewDidLoad() {
super.viewDidLoad()
GADRewardBasedVideoAd.sharedInstance().delegate = self
let request = GADRequest()
// Requests test ads on test devices.
request.testDevices = ["2077ef9a63d2b398840261c8221a0c9b"]
GADRewardBasedVideoAd.sharedInstance().loadRequest(request, withAdUnitID: "ca-app-pub-3940256099942544/4411468910")
}
// MARK: Daily Reward Button Delegate
#IBAction func playVideoClicked() {
if GADRewardBasedVideoAd.sharedInstance().ready {
GADRewardBasedVideoAd.sharedInstance().presentFromRootViewController(self)
}
}
// MARK: GADRewardBasedVideoAdDelegate
func rewardBasedVideoAdDidStartPlaying(rewardBasedVideoAd: GADRewardBasedVideoAd!) {
MediaPlayer.pauseBackgroundMusic()
print("Reward Video: Started")
}
func rewardBasedVideoAdDidClose(rewardBasedVideoAd: GADRewardBasedVideoAd!) {
MediaPlayer.startBackgroundMusic()
requestLoadAd()
print("Reward Video: Complete")
}
func rewardBasedVideoAdWillLeaveApplication(rewardBasedVideoAd: GADRewardBasedVideoAd!) {
requestLoadAd()
print("Reward Video: Will leave application")
}
}
When I press the button to trigger the code in the "playVideoClicked" function, I just see a regular test interstitial (no video) and the "rewardBasedVideoDidStartPlaying" function never gets called. When I close the interstitial the "rewardBasedVideoAdDidClose" function is called, though. Does this mean I did something wrong, or does AdMob just not display test videos?
Also, how do I configure this ad as a Reward Video in the AdMob developer console?
Change request.testDevices = ["2077ef9a63d2b398840261c8221a0c9b"] to request.testDevices = [kGADSimulatorID] and it should be working.
I have a banner ad and an interstitial ad. They are appearing when I use the adUnitID's for testing purposes that AdMob gives you, but neither of them are showing when I use live ads. The banner just doesn't appear at all. When the interstitial ad appears, it is just completely black. The adUnitID's are correct. The ads on my other apps are currently appearing just fine. The problem occurs both when I use the iOS simulator and my device. Any ideas?
var interstitial: GADInterstitial!
func createAndLoadAd() -> GADInterstitial{
let ad = GADInterstitial(adUnitID: "ca-app-pub-7863284438864645/1835730011")
let request = GADRequest()
ad.loadRequest(request)
return ad
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
authenticateLocalPlayer()
self.bannerView.adUnitID = "ca-app-pub-7863284438864645/9358996816"
self.bannerView.rootViewController = self
let request: GADRequest = GADRequest()
self.bannerView.loadRequest(request)
self.interstitial = self.createAndLoadAd()
}
override func viewDidAppear(animated: Bool) {
_ = GADRequest()
//request.testDevices = ["2077ef9a63d2b398840261c8221a0c9b"]
showAd()
}
func showAd(){
if(self.interstitial.isReady){
print("ad ready")
self.interstitial.presentFromRootViewController(self)
}
else{
print("ad not ready")
self.interstitial = createAndLoadAd()
}
}
This is an aside, because my problem looked similar, but was not.
App ID is NOT the same as Ad Unit ID.
And, stupidly, they BOTH start with ca-app-pub- and a lot of numbers. I was using the App ID which I can guarantee 100% does NOT work.
Yes, I feel pretty silly, but the folks at Google should have made them dissimilar.
replace adUnitID with your own ad unit id in your Admob account.
make sure you setup payment method, otherwise the ads wont show up
They have both IDs that contains "~" telda and "/" slash