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.
Related
I've recently uploaded my application to App Store but I've found an error. My program crashes every time an interstitial (AdMob) begins and I've no idea why. It never crashes when I run the program from Xcode..
import GoogleMobileAds
First I've imported this one of course.
Also added this to my class
GADInterstitialDelegate
Inside the viewDidLoad() I've added this one:
interstitial = GADInterstitial(adUnitID: "AD-UNIT-ID")
let request = GADRequest()
request.testDevices = [ kGADSimulatorID] //Should this be removed now when the app is on App Store?
interstitial.load(request)
interstitial.delegate = self
Further down I've added this code to my save-button:
//Advertisement test
if self.interstitial.isReady
{
self.interstitial.present(fromRootViewController: self)
}
else
{
print("Advertisement is not ready!")
self.performSegue(withIdentifier: "History", sender: self)
}
And for the last I've this:
//Advertisement will dismiss from the screen
func interstitialDidDismissScreen(_ ad: GADInterstitial)
{
self.performSegue(withIdentifier: "History", sender: self)
}
How can I fix this problem? It crashes every time I press on that "Save button" and the screen is turning black and then boom, crashes.
So it never crashed before when I ran it from my Xcode project, but when I was in TestFlight and in App Store it crashes every time!
I am using a test ad and loading it through a test device. I am about to put my app on the app store so that test device won't exist. I know I need to change my ad key to the real one, but how should I change the code so that it loads on the users device and not a test device?
func createInterstitialAd() -> GADInterstitial? {
interstitial = GADInterstitial(adUnitID: "ca-app-pub-3940256099942544/4411468910")
guard let interstitial = interstitial else {
return nil
}
let request = GADRequest()
request.testDevices = [kGADSimulatorID]
interstitial.load(request)
interstitial.delegate = self
return interstitial
}
Can’t you just edit the production app ID into your function before you deploy it? As stated in the Admob interstitial docs, “Just make sure you replace it with your own ad unit ID before publishing your app.”
https://developers.google.com/admob/ios/interstitial
func createAndLoadInterstitial() -> GADInterstitial {
var interstitial = GADInterstitial(adUnitID: "ca-app-pub-3940256099942544/4411468910")
interstitial.delegate = self
interstitial.load(GADRequest())
return interstitial
}
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.
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
I'm looking to find away to load an admob interstitial ad when my app launches. Just load one initial ad when the app launches. I'm using swift and can't seem to find any forms on how to do this.
import GoogleMobileAds
In Appdelegate Create function
func createAndLoadAd() -> GADInterstitial
{
var ad = GADInterstitial(adUnitID: "ca-app-pub-5018462886395219/7896492088")
var request = GADRequest()
request.testDevices = ["2077ef9a63d2b398840261c8221a0c9b"]
ad.loadRequest(request)
return ad
}
In DidFinishLunchingMethod
self.interstitial = self.createAndLoadAd()
if (self.interstitial.isReady)
{
self.interstitial.presentFromRootViewController(self)
self.interstitial = self.createAndLoadAd()
}