When my callButton is clicked it rings a phone number, I want to display my interstitial ad before the call is made. I have tested my ad on the "IBAction function ad" button and it works on the button but when I call it on the callButton func it will not work and goes straight to making a call.
class ProfilePageViewController: UIViewController, MFMessageComposeViewControllerDelegate, UITableViewDelegate, UIAlertViewDelegate, GADInterstitialDelegate {
#IBAction func ad(_ sender: Any) {
if self.interstitialAd.isReady {
self.interstitialAd.present(fromRootViewController: self)
}
}
#IBAction func callButton(_ sender: Any) {
if let contactopt = contact{
if let url = NSURL(string: "tel://\(contactopt)") {
// UIApplication.shared.openURL(url as URL)
UIApplication.shared.open(url as URL, options: [:], completionHandler: nil)
}
}
}
var interstitialAd: GADInterstitial!
func reloadInterstitialAd() -> GADInterstitial {
var interstitial = GADInterstitial(adUnitID: "ca-app-pub-/6966780536")
interstitial.delegate = self
interstitial.load(GADRequest())
return interstitial
}
func interstitialDidDismissScreen(ad: GADInterstitial!) {
self.interstitialAd = reloadInterstitialAd()
}
func interstitialDidDismissScreen(_ ad: GADInterstitial) {
interstitialAd = reloadInterstitialAd()
}
override func viewDidLoad() {
self.interstitialAd = GADInterstitial(adUnitID: "ca-app-pub-/6966780536")
let request = GADRequest()
request.testDevices = ["2077ef9a63d2b398840261c8221a0c9b"]
self.interstitialAd.load(request)
self.interstitialAd = reloadInterstitialAd()
super.viewDidLoad()
}
}
Of course it goes to making a call. That's what you're telling it to do with UIApplication.shared.open.
Make the call once the ad has been dismissed in interstitialDidDismissScreen.
Also check to see if there is an ad to present, interstitialAd.isReady. If there is no ad to present go straight to making the call.
You're also doing the same thing twice in your viewDidLoad:
// Sets up an ad
self.interstitialAd = GADInterstitial(adUnitID: "ca-app-pub-/6966780536")
let request = GADRequest()
request.testDevices = ["2077ef9a63d2b398840261c8221a0c9b"]
self.interstitialAd.load(request)
// Creates a new ad
self.interstitialAd = reloadInterstitialAd()
Just call self.interstitialAd = reloadInterstitialAd().
I believe your problem has to do with the synchrony of the present method on the interstitial. I do not know what the implementation is, but it seems like the ad's presentation does not block the thread on which it was called. You should try implementing GADInterstitialDelegate to grab the event when the ad is presented then proceed to make your call from there.
Try this:
GADInterstitial is a one time use object. That means once an
interstitial is shown, hasBeenUsed returns true and the interstitial
can't be used to load another ad. To request another interstitial,
you'll need to create a new GADInterstitial object. The best practice,
as shown above, is to have a helper method to handle creating and
loading an interstitial.
func createAndLoadInterstitial() -> GADInterstitial {
var interstitial = GADInterstitial(adUnitID: "ca-app-pub-3940256099942544/4411468910")
interstitial.delegate = self
interstitial.load(GADRequest())
return interstitial
}
func interstitialDidDismissScreen(_ ad: GADInterstitial) {
interstitial = createAndLoadInterstitial()
}
#IBAction func ad(_ sender: Any) {
if self.interstitialAd.isReady {
self.interstitialAd.present(fromRootViewController: self)
}
}
Related
So, I had the interstitial ads working fine by following the code on the Admob website and then recently I updated my pods and with it the GoogleAdMob SDK.
Now when my interstitial Ad should present, nothing happens and in the console I have this error:
<GoogleThe provided view controller is not being presented.
[ProcessSuspension] 0x110adfbd0 - ProcessAssertion::processAssertionWasInvalidated()
Here is my code:
extension FirstViewController: GADInterstitialDelegate {
func showInterstitialAd() {
if interstitial.isReady {
interstitial.present(fromRootViewController: self)
} else {
print("Ad wasn't ready")
}
}
func createAndLoadInterstitial() -> GADInterstitial {
var interstitial = GADInterstitial(adUnitID: "ca-app-pub-3940256099942544/4411468910")
interstitial.delegate = self
interstitial.load(GADRequest())
return interstitial
}
func interstitialDidDismissScreen(_ ad: GADInterstitial) {
interstitial = createAndLoadInterstitial()
}
}
In viewDidLoad I call:
func setUpInterstitial() {
interstitial = GADInterstitial(adUnitID: "ca-app-pub-3940256099942544/4411468910")
interstitial.delegate = self
let request = GADRequest()
interstitial.load(request)
}
and in ViewWillAppear
if InterstitialAd.counter >= 3 { self.showInterstitialAd(); InterstitialAd.counter = 0}
The intended logic is every 3 times a user views a page, namely FirstViewController, an interstitial ad shows.
Thank you in advance :)
You can update your code like below:
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
if InterstitialAd.counter >= 3 {
self.showInterstitialAd();
InterstitialAd.counter = 0
}
}
Remove your code from viewWillAppear.
I implemented a test adMob with Firebase:
override func viewDidLoad() {
super.viewDidLoad()
interstitial = createAndLoadInterstitial()
interstitial.delegate = self
....}
func createAndLoadInterstitial() -> GADInterstitial {
var interstitial = GADInterstitial(adUnitID: "testVideo")
interstitial.delegate = self
interstitial.load(GADRequest())
return interstitial
}
func interstitialWillDismissScreen(_ ad: GADInterstitial) {
print("interstitialWillDismissScreen")
UserDefaults.standard.set(false, forKey: "adWasShowing")
}
func interstitialDidDismissScreen(_ ad: GADInterstitial) {
if ... {
...
}
interstitial = createAndLoadInterstitial()
print("interstitialDidDismissScreen")
}
The problem is when I get an ad (5sec video) and take the app to background and then to foreground again
interstitialWillDismissScreen and interstitialDidDismissScreen
automatically gets called resulting in closing the ad. I think many users will use this exploit to not stay on the video. Any idea on how to solve this?
I tried to set up a bool in UserDefaults in the "interstitialWillPresentScreen" and check it at "applicationWillEnterForeground", but I don't like this idea and also the interstitial request is not yet loaded (interstitialRequest must be recreated for every ad).
Right now it only shows the full page ad one time. When I go back and press the button again it does not show. I would like it to show overtime my button is clicked. I have my AdMob hooked up to my Firebase project.
class FirstViewController: UIViewController, UIAlertViewDelegate {
var interstitial: GADInterstitial!
#IBAction func loadAd(_ sender: Any) {
if (interstitial.isReady){
interstitial.present(fromRootViewController: self)
}
}
override func viewDidLoad() {
interstitial = GADInterstitial(adUnitID: "ca-app-pub-11111111/9758796532")
let request = GADRequest()
interstitial.load(request)
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
You need to create GAD request every time.
GADInterstitial is a one time use object. That means once an
interstitial is shown, hasBeenUsed returns true and the interstitial
can't be used to load another ad. To request another interstitial,
you'll need to create a new GADInterstitial object. The best practice,
as shown above, is to have a helper method to handle creating and
loading an interstitial.
#IBAction func loadAd(_ sender: Any) {
interstitial = GADInterstitial(adUnitID: "ca-app-pub-11111111/9758796532")
interstitial.delegate = self
let request = GADRequest()
interstitial.load(request)
}
And while loaded successfully you need to present at delegate method
optional func interstitialDidReceiveAd(_ ad: GADInterstitial!){
if (interstitial.isReady){
interstitial.present(fromRootViewController: self)
}
}
optional func interstitialWillDismissScreen(_ ad: GADInterstitial!){
interstitial.delegate = nil
interstitial = nil
}
optional func interstitialDidFail(toPresentScreen ad: GADInterstitial!){
interstitial.delegate = nil
interstitial = nil
}
More : https://firebase.google.com/docs/admob/ios/interstitial
When dismissing the interstitial we have to create and make load request for next interstitial ads.
func interstitialDidDismissScreen(_ ad: GADInterstitial) {
interstitial = createAndLoadInterstitial()
}
The best place to allocate another interstitial is in the interstitialDidDismissScreen: method on GADInterstitialDelegate so that the next interstitial starts loading as soon as the previous one is dismissed. You may even consider breaking out interstitial initialization into its own helper method:
Source: https://firebase.google.com/docs/admob/ios/interstitial
I am using Admob interstitials in my swift project and I am having trouble reloading the ads. The first interstitial displays fine, and when interstitialWillDismissScreen is called, it exits back to the game and reloads a new interstitial as it's supposed to. However, neither interstitialDidReceiveAd nor interstitialWillDismissScreen are called again, so after the second ad is displayed, no other ads come through. What am I missing?
import GoogleMobileAds
let appDelegate = (UIApplication.sharedApplication().delegate as! AppDelegate)
class GameScene: SKScene, GKGameCenterControllerDelegate, GADInterstitialDelegate {
var interstitial: GADInterstitial!
override func didMoveToView(view: SKView) {
//preload interstitial ad
if NSUserDefaults.standardUserDefaults().boolForKey("paidToRemoveAds") == false {
interstitial = loadAd()
}
interstitial.delegate = self
}
func gameOver() {
runGoogleAd()
}
func loadAd() -> GADInterstitial {
let ad = GADInterstitial(adUnitID: "ca-app-pub-2963481692578498/7292484569")
let request = GADRequest()
request.testDevices = [kGADSimulatorID]
ad.loadRequest(request)
return ad
}
func runGoogleAd() {
if interstitial.isReady {
interstitial.presentFromRootViewController((appDelegate.window?.rootViewController)!)
}
}
func interstitialDidReceiveAd(ad: GADInterstitial!) {
print("ad loaded")
}
func interstitialWillDismissScreen(ad: GADInterstitial!) {
interstitial = loadAd()
print("loading new ad")
}
}
I assume loadAd() creates a new instance? You need to set the delegate on the new instance:
func interstitialWillDismissScreen(ad: GADInterstitial!) {
interstitial = loadAd()
interstitial.delegate = self // <-- You forgot this.
print("loading new ad")
}
Actually you should probably set the delegate in loadAd() since it is an instance member of this class.
I'm developing an iOS application using Swift2 and Xcode7. I'm trying to implement AdMob but it doesn't display my interstitial ad.
override func viewDidLoad() {
super.viewDidLoad()
_interstitial = createAndLoadInterstitial()
}
func createAndLoadInterstitial()->GADInterstitial {
let interstitial = GADInterstitial(adUnitID: "interstitial_ID")
let gadRequest:GADRequest = GADRequest()
gadRequest.testDevices = ["test device id"]
interstitial.delegate = self
interstitial?.loadRequest(gadRequest)
return interstitial!
}
func interstitialDidReceiveAd(ad: GADInterstitial!) {
_interstitial?.presentFromRootViewController(self)
}
func interstitial(ad: GADInterstitial!, didFailToReceiveAdWithError error: GADRequestError!) {
print(error.localizedDescription)
}
func interstitialDidDismissScreen(ad: GADInterstitial!) {
_interstitial = createAndLoadInterstitial()
}
I receive this error:
Request Error: No ad to show.
Request Error: No ad to show.
means that your request was successful but that Admob has no ad to show for your device at this time. The best way to ensure that you always have ads to show is to use mediation so that an unfulfilled request falls through to another ad network. Admob provides good mechanisms to do that.
You should have two Ad Unit ID's. One for your GADBannerView and one for your GADInterstitial. Make sure your Ad Unit ID supplied by AdMob for your interstitial is exactly the same as what they've given you. Update to the latest AdMob SDK, currently 7.5.0. Also consider calling presentFromRootViewController(self) at specific intervals or once the user completes a desired action. The way you have it setup now will keep presenting interstitials one after another because you are sending requests for new interstitials every time one is dismissed and then displaying the interstitial as soon as it receives an ad.
import UIKit
import GoogleMobileAds
class ViewController: UIViewController, GADInterstitialDelegate {
var myInterstitial : GADInterstitial?
override func viewDidLoad() {
super.viewDidLoad()
myInterstitial = createAndLoadInterstitial()
}
func createAndLoadInterstitial()->GADInterstitial {
let interstitial = GADInterstitial(adUnitID: "Your Ad Unit ID")
interstitial.delegate = self
interstitial?.loadRequest(GADRequest())
return interstitial
}
#IBAction func someButton(sender: AnyObject) {
myInterstitial?.presentFromRootViewController(self)
}
func interstitialDidReceiveAd(ad: GADInterstitial!) {
print("interstitialDidReceiveAd")
}
func interstitial(ad: GADInterstitial!, didFailToReceiveAdWithError error: GADRequestError!) {
print(error.localizedDescription)
}
func interstitialDidDismissScreen(ad: GADInterstitial!) {
print("interstitialDidDismissScreen")
myInterstitial = createAndLoadInterstitial()
}