一旦用户关闭插页式广告,我就会尝试让此按钮转到新的 ViewController。 Segue 代码可以在没有广告代码的情况下工作,广告代码也可以在没有 Segue 代码的情况下工作。无论哪个先放,都会先发生,但我不知道如何让它们一起工作。您会注意到我尝试将 self.present... 移动到不同的地方,但没有成功(请参阅注释行)。
@IBAction func adButton(_ sender: UIButton) {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let myVC = storyboard.instantiateViewController(withIdentifier: "newVC")
//self.present(myVC, animated: true, completion: nil)
if (interstitial.isReady) {
interstitial.present(fromRootViewController: self)
interstitial = createAd()
//self.present(myVC, animated: true, completion: nil)
}
self.present(myVC, animated: true, completion: nil)
}
最佳答案
interstitial
对象具有一个委托(delegate) (GADInterstitialDelegate
) 协议(protocol),您可以遵守该协议(protocol),以便在广告被关闭时收到通知。有多种处理广告状态的委托(delegate)方法,您可以在文档中查看这些方法。
class ViewController:UIViewController, GADInterstitialDelegate {
override func viewDidLoad() {
super.viewDidLoad()
interstitial = GADInterstitial(adUnitID: "ca-app-pub-3940256099942544/4411468910")
interstitial.delegate = self
let request = GADRequest()
interstitial.load(request)
}
@IBAction func adButton(_ sender: UIButton) {
if (interstitial.isReady) {
interstitial.present(fromRootViewController: self)
} else {
self.present(myVC, animated: true, completion: nil)
}
}
//Tells the delegate the interstitial is to be animated off the screen.
func interstitialWillDismissScreen(_ ad: GADInterstitial) {
print("interstitialWillDismissScreen")
}
//Tells the delegate the interstitial had been animated off the screen.
func interstitialDidDismissScreen(_ ad: GADInterstitial) {
print("interstitialDidDismissScreen")
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let myVC = storyboard.instantiateViewController(withIdentifier: "newVC")
self.present(myVC, animated: true, completion: nil)
}
}
Google Ads 文档:Interstitial Ads
关于swift - 关闭插页式广告后呈现新的 ViewController - Swift 4,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54373942/