[英]How to implement interstitial Ads using AdMob in iOS with Swift?
我正在使用AdMob素材实施插页式广告,但未显示。
这是我想在课堂上实施插页式广告的地方。
import UIKit
import GoogleMobileAds
class SearchNew: UIViewController{
var interstitial: GADInterstitial!
override func viewDidLoad() {
super.viewDidLoad()
self.interstitial = GADInterstitial(adUnitID: "ca-app-pub-3940256099942544/4411468910")
let request = GADRequest()
request.testDevices = ["2077ef9a63d2b398840261c8221a0c9b"]
self.interstitial.loadRequest(request)
self.showAd()
}
func showAd() {
if self.interstitial.isReady {
self.interstitial.presentFromRootViewController(self)
}
}
为您的类添加委托函数(GADInterstitialDelegate)。
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, GADInterstitialDelegate {
var mInterstitial: GADInterstitial!
var gViewController: UIViewController?
调用loadRequest,当准备好展示广告时会触发interstitialDidReceiveAd
func showAdmobInterstitial()
{
self.mInterstitial = GADInterstitial.init(adUnitID:kGoogleFullScreenAppUnitID )
mInterstitial.delegate = self
let Request = GADRequest()
Request.testDevices = ["2077ef9a63d2b398840261c8221a0c9b"]
mInterstitial.loadRequest(Request)
}
func interstitialDidReceiveAd(ad: GADInterstitial!)
{
ad.presentFromRootViewController(self.gViewController)
}
从任何viewController调用showAdmobInterstitial
let App = UIApplication.sharedApplication().delegate as! AppDelegate
App.gViewController = self;
App.showAdmobInterstitial()
In this way I did this admob Interstitial ads task
import UIKit
import GoogleMobileAds
//-------Interstitial adds--------
Step 1: You need to add this stuff in side your AppDelegate.swift file
@UIApplicationMain
Delegate: UIResponder, UIApplicationDelegate,GADInterstitialDelegate {
var gViewController: UIViewController?
var window: UIWindow?
func showAdmobInterstitial()
{
let kGoogleFullScreenAppUnitID = "ca-app-pub-3940256099942544/4411468910";
self.mInterstitial = GADInterstitial.init(adUnitID:kGoogleFullScreenAppUnitID )
mInterstitial.delegate = self
let Request = GADRequest()
Request.testDevices = ["2077ef9a63d2b398840261c8221a0c9b"]
mInterstitial.loadRequest(Request)
}
func interstitialDidReceiveAd(ad: GADInterstitial!)
{
ad.presentFromRootViewController(self.gViewController)
}
//-------------------------
第2步:现在无论您想在何处或任何veiwController中展示插页式广告,然后在该文件中添加这些内容。
let App = UIApplication.sharedApplication().delegate as! AppDelegate
App.gViewController = self;
App.showAdmobInterstitial()
By this way we can call now interstitial ads easily.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.