提问人:Sajjad Aims 提问时间:2/22/2016 更新时间:1/25/2020 访问量:1694
显示 Admob 插页式广告时,使用导航返回按钮按 iOS Swift
show Admob interstitial when with nav back button press iOS Swift
问:
我有两个视图控制器imageViewController和InfoViewController。我希望我的广告在用户单击InfoViewController的“后退”按钮时显示
class imageViewController: UIViewController , GADInterstitialDelegate {
var interstitial: GADInterstitial!
override func viewDidLoad() {
self.interstitial = self.createAndLoadInterstitial()
super.viewDidLoad()
}
func createAndLoadInterstitial() -> GADInterstitial {
let interstitial: GADInterstitial = GADInterstitial(adUnitID: "ca-app-pub-3940256099942544/4411468910")
interstitial.delegate = self
let request = GADRequest()
interstitial.loadRequest(request)
print("req sent")
return interstitial
}
func interstitialDidReceiveAd(ad: GADInterstitial!) {
if (interstitial.isReady) {
print("ad ready")
interstitial.presentFromRootViewController(self)
}}
func interstitialDidDismissScreen(interstitial: GADInterstitial) {
}
我应该如何通过从 infoViewController 到 ImageViewCroller 的后退按钮单击来调用它。我也未能用全局变量解决这个问题,因为我是 iOS 的新手
答:
我建议用 的方法之一调用它。例如:UIViewController
isMovingFromParentViewController()
请参阅文档。
编辑:
实际上,如果你想让一个 VC 作为委托,你应该从父 VC 调用它,否则你会因为用后退按钮弹出它而丢失委托。
您可以通过几种方式做到这一点。一种是使用闭包。在这里,你可以把你的回调方法放在第一个VC中,并在你的第二个VC中有一个属性,如下所示:
var onBack:()->()?
在显示第二个 VC 时,您需要设置它的值(例如在 method 中):prepareForSegue
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if let destination = segue.destinationViewController as? InfoViewController {
destination.onBack = { ()->() in
self.createAndLoadInterstitial()
}
}
}
然后,在第二个视图控制器中执行以下操作:
override func isMovingFromParentViewController() -> Bool {
onBack?()
}
顺便说一句,这没有经过测试:)
评论
它会在背面显示广告 新闻
var interstitial: GADInterstitial!
var showAd = true
viewdidload {
self.createandLoadInterstitial()
}
func createAndLoadInterstitial() {
interstitial = GADInterstitial(adUnitID: "ca-app-pub-3940256099942544/4411468910")
interstitial.delegate = self
let request = GADRequest()
interstitial.loadRequest(request)
}
override func viewDidAppear(animated: Bool) {
if (interstitial.isReady && showAd) {
showAd = false
print("iterstitalMain is ready")
interstitial.presentFromRootViewController(self)
self.createAndLoadInterstitial()
}
else{
showAd = true
}
}
对我来说,检测 ios back pressed 并在 backPressed 上显示 admob 插页式广告的最简单方法是在我的 childViewController 中使用 或,在您的情况下。willMoveToParentViewController
didMoveToParentViewController
InfoViewController
我在 appDelegate 上创建了我的插页式广告插页式广告实例。所以,我可以全局使用它,并且只使用一个实例/共享实例(我认为😀)
然后就打电话给它或像这样。两者几乎相同。(只会提前调用(我的观察:广告将在向后过渡时显示)(我的观察:广告将在完全过渡回来时显示))willMoveToParentViewController
didMoveToParentViewController
willMoveToParentViewController
didMoveToParentViewController
override func willMoveToParentViewController(parent: UIViewController?) {
super.willMoveToParentViewController(parent)
if parent == nil {
appDelegate.showInterstitialAd()
}
}
或
override func didMoveToParentViewController(parent: UIViewController?) {
super.didMoveToParentViewController(parent)
if parent == nil {
appDelegate.showInterstitialAd()
}
}
从这个答案中得到了想法。https://stackoverflow.com/a/17551952
调用 interstitialDidReceiveAd' 不是调用它的最佳方式,因为您无法控制它何时显示。interstitial.presentFromRootViewController(self) on
评论
完美的测试代码,用于添加插页式广告。在后退按钮中。 在 secondviewController 中添加此代码。无需在 FirstViewcontroller 中添加任何内容。
func createAndLoadInterstitial() {
interstitial = GADInterstitial(adUnitID: "your id")
interstitial.delegate = self
let request = GADRequest()
interstitial.loadRequest(request)
}
override func viewWillDisappear(animated: Bool) {
if (interstitial.isReady && showAd) {
showAd = false
print("iterstitalMain is ready")
interstitial.presentFromRootViewController(parentViewController!)
self.createAndLoadInterstitial()
}
else{
}
}
答案在顶部的更新功能。
override func willMove(toParent parent: UIViewController?) {
super.willMove(toParent: parent)
if parent == nil {
if interstitial.isReady {
interstitial.present(fromRootViewController: self)
} else {
print("Ad wasn't ready")
}
}
}``
评论