Swift Interstitial Making a Banner Ad

How to add Admob interstitial ads

You cannot just present the ad in a gameScene, you need a viewController.

The easiest way is to put the code from GameScene into your ViewController.

So move/or create a func in ViewController like so

func showInterAd() {
/// code to present inter ad
}

Than you can use something like NSNotificationCenter to forward the message from GameScene to the ViewController.

Still in your ViewController add an observer in viewDidLoad

NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(showInterAd), name: "ShowAd", object: nil)

Than in your GameScene when you want to show the ad post the notification.

NSNotificationCenter.defaultCenter().postNotificationName("ShowAd", object: nil)

Alternatively I have a helper on gitHub if you want a cleaner and more reusable solution

https://github.com/crashoverride777/Swift-iAds-AdMob-CustomAds-Helper

How to make my interstitial ad pop up after x amount of clicks?

Figured it out! It wasn't var counter that was causing the problem like I thought it was, rather it was the AdMob code. My AdMob was caching an ad and showing them once and then not caching/creating new ads, so regardless of the counter or not it will only display one ad. I followed a video tutorial (Geeky Lemon) that showed me how to properly cache and load ads. I then reenabled my var counter code and everything worked like it was supposed to. It shows an ad every 15th click. Here is the code.

class Page1: UIViewController, AVAudioPlayerDelegate, GADInterstitialDelegate, UIAlertViewDelegate {







var interstitial: GADInterstitial!



var counter: Int = 0



@IBOutlet weak var scrollView: UIScrollView!

@IBOutlet weak var pageControl: UIPageControl!





override func viewDidLoad() {

super.viewDidLoad()

self.view.backgroundColor = UIColor(patternImage: UIImage(named: "6splash.png")!)



interstitial = GADInterstitial(adUnitID: "ca-app-pub-3940256099942544/4411468910")



let request = GADRequest()

interstitial.load(request)



}





@IBAction func playAgain(_ sender: Any) {

if counter % 15 == 0 {

if interstitial.isReady {

interstitial.present(fromRootViewController: self)

interstitial = CreateAd()

} else {

print("Ad wasn't ready")

}

}

counter += 1

}



}

func CreateAd() -> GADInterstitial {

let interstitial = GADInterstitial(adUnitID: "ca-app-pub-3940256099942544/4411468910")

interstitial.load(GADRequest())

return interstitial

}

Animation when displaying AdMob interstitial ads (Swift, GADInterstitial)

I searched for an answer about 6 months ago and was told by an SDK engineer there is no method for animating an interstitial using the GAD framework. I guess it is within some reasonable possibility that you could modify the framework to accommodate a different style animation, but it would be ill-advised. You can, however, animate banner ads from AdMob.

How do I hide Interstitial ads after IAP in Swift?

There are 2 scenarios to handle this.

  1. Remove GADInterstitialAd right after the purchase.
  2. Check if the Ads are removed on app start.

In both the case, make your interstitial nullable like:

private var interstitial: GADInterstitialAd?

To handle the first case:

if Purchase.isSuccesful == true {
interstitial = nil
}

To handle the second case (assuming you are saving the purchase state locally):
Inside the timerBlock() function, simply check if the Ads are removed & load Ads accordingly like:

@objc func timerBlock() {
if UserDefaults.standard.getActiveRemoveAdsSubscription() == false {
// Load Ad & show Interstitial
} else {
print("Ad Removed, not loading Interstitial.")
}
}

In-App Purchase : Remove Ad Banners & Interstitial Ads - Code Assistance

If you're implementing your own ADBannerView then you need to remove self.canDisplayBannerAds = true.

self.canDisplayBannerAds = true can be used for a no hassle way of implementing iAd banners in your application. This will create an ADBannerView for you and show or hide the ADBannerView on the bottom of your view depending on whether it receives an ad or not from the iAd network.

You either implement your own ADBannerView or use self.canDisplayBannerAds = true, not both.

Admob and Interstitial Ads with iOS9

AdMob framework not found even if I imported it

Remove the framework and import it again. Make sure you're checking "Copy items if needed". Also, check your library search paths under your project settings and build phases to see if the framework is include.

I imported all the frameworks required by AdMob

This is not necessary. The AdMob framework will import the frameworks it needs once you include import GoogleMobileAds.

And I didn't touch the storyboard. Do I have to create something in the storyboard?

No

Before submitting the app to App Store I have to remove the test devices line and put my real AdUnitID?

Yes. Try using your real AdUnitID now to see if your test ID is causing the problem.

Maybe the code is right but if the app is not published AdMob won't load, or maybe I have to wait a day before my AdMob id starts working

No. The application does not have to be published to receive live advertisements.

Where I can find recent AdMob IDs for testing? So I test the app, and if works I change the id and submit

Nowhere. It is not wise to give out your AdUnitID.

Another question about AdMob guidelines, in other apps can I use iAd in a view and AdMob in an other? May I be banned for this? Only banner ads in lower part of the screen

Use as many ad networks in your application as you'd like. There's nothing stating you aren't allowed to.


Sounds like you're on the right track. You should set and include the interstitial's delegate methods though to see if you're getting any error messages. You need to allow arbitrary loads in your .plist too. Check AdMob's iOS 9 Considerations.

Are you calling createNloadAd() before you attempt to present the interstitial in your viewDidLoad? It takes time to download the advertisement.

Make sure the AdUnitID provided by AdMob is identical to the one you're using.

Also, move the presentation from viewDidLoad to viewDidAppear.

Here's an example of creating our interstitial in our AppDelegate.swift and then presenting the interstitial in our ViewController.swift:

AppDelegate.swift

import UIKit
import GoogleMobileAds // Import AdMob

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, GADInterstitialDelegate { // Include delegate

var window: UIWindow?
var myInterstitial : GADInterstitial?

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// App launched
// Load interstitial
myInterstitial = createAndLoadInterstitial()
return true
}

func createAndLoadInterstitial()->GADInterstitial {
let interstitial = GADInterstitial(adUnitID: "Your Ad Unit ID")
interstitial.delegate = self
interstitial?.loadRequest(GADRequest())
return interstitial
}

func interstitialDidReceiveAd(ad: GADInterstitial!) {
print("interstitialDidReceiveAd")
}

func interstitial(ad: GADInterstitial!, didFailToReceiveAdWithError error: GADRequestError!) {
print(error.localizedDescription)
}

func interstitialDidDismissScreen(ad: GADInterstitial!) {
print("interstitialDidDismissScreen")
myInterstitial = createAndLoadInterstitial()
}

ViewController.swift (This would be your game over view controller)

import UIKit

class ViewController: UIViewController {

let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate // Create reference to our app delegate

override func viewDidLoad() {
super.viewDidLoad()
}

override func viewDidAppear(animated: Bool) {
appDelegate.myInterstitial?.presentFromRootViewController(self)
}


Related Topics



Leave a reply



Submit