我有两个视图控制器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新手
答案 0 :(得分:2)
我最简单的方法是在backPressed中检测ios并在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
PS。调用interstitial.presentFromRootViewController(self) on
interstitialDidReceiveAd`不是调用它的最佳方式,因为它在显示时没有控制权。
答案 1 :(得分:1)
它会在后面显示广告按
ListWhaskPagination
答案 2 :(得分:0)
我建议用UIViewController
的方法之一来调用它。例如:
isMovingFromParentViewController()
请参阅documentation。
修改强>
实际上,如果你想让VC作为委托,你应该从父VC调用它,否则你将通过用后退按钮弹出它来丢失委托。
你可以通过几种方式实现。一种是使用closure。在这里,您可以将回调方法放在第一个VC中,并在第二个VC中使用属性,如下所示:
var onBack:()->()?
你需要在显示第二个VC时设置它的值(例如在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?()
}
没有经过测试btw:)
答案 3 :(得分:0)
用于添加插页式广告的完美测试代码。在后退按钮。 在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{
}
}
答案 4 :(得分:0)
最新答案的功能。
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")
}
}
}``