- Sign up for or sign in to Admob
- Add an App in Admob
- Add an Ad unit in Admob - Choose Interstitial. You can set ad type, frequency capping and eCPM floor under advanced settings
- You should now have an Ad Unit ID and App Id which will be used in displaying interstitial ads to the user
- In your code, create a file InterstitialAd.swift, it should have the following code:
let adController = InterstitialAd() import GoogleMobileAds class InterstitialAd : NSObject, GADInterstitialDelegate { var testAdId = "ca-app-pub-3940256099942544/4411468910" var adId = "[YOUR AD UNIT ID GOES HERE]" var interstitial : GADInterstitial! func createAndLoadInterstitial() { interstitial = GADInterstitial(adUnitID: testAdId) interstitial.delegate = self interstitial.load(GADRequest()) } // Load new interstitial on close so that adController // is ready for the next time showAd is called func interstitialDidDismissScreen(_ ad: GADInterstitial) { createAndLoadInterstitial() } func showAd(_ viewController: UIViewController) { if interstitial.isReady { interstitial.present(fromRootViewController: viewController) } } }
- Add the following to your AppDelegate.swift file, it will configure the app for Admob ads and load the first interstitial so that it is ready to be displayed when you try to display the interstitial:
... import GoogleMobileAds @UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate { ... var appId = "[YOUR ADMOB APP ID GOES HERE]" func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { GADMobileAds.configure(withApplicationID: appId) adController.createAndLoadInterstitial() ... } ... }
- From the view controller where you want to show your interstitial ad, make the following call:
adController.showAd(self)
Note:
For testing, you should use the testAdId as the adUnitID in InterstitialAd.swift, this is so that real ads are not displayed during testing. For release, switch to using adId.... included for brevity in ApplicationDelegate.swift code above
No comments:
Post a Comment