我实现了一个ADInterstitialAd,它在iPhone上工作正常,但在ipad上不起作用。这是我的代码:
func close(sender: UIButton) {
closeButton.removeFromSuperview()
interAdView.removeFromSuperview()
}
func loadAd() {
println("load ad")
interAd = ADInterstitialAd()
interAd.delegate = self
}
func interstitialAdDidLoad(interstitialAd: ADInterstitialAd!) {
println("ad did load")
interAdView = UIView()
interAdView.frame = CGRect(x: 0, y: 0, width: self.frame.size.width, height: self.frame.size.height)
self.view?.addSubview(interAdView)
interAd.presentInView(interAdView)
UIViewController.prepareInterstitialAds()
interAdView.addSubview(closeButton)
}
func interstitialAdDidUnload(interstitialAd: ADInterstitialAd!) {
closeButton.removeFromSuperview()
interAdView.removeFromSuperview()
}
func interstitialAd(interstitialAd: ADInterstitialAd!, didFailWithError error: NSError!) {
println("failed to receive")
println(error.localizedDescription)
closeButton.removeFromSuperview()
interAdView.removeFromSuperview()
}
它打印"广告已加载",但在iPad上它只显示关闭按钮而不是广告视图,并且不打印错误描述。我导入了iAd框架,并将ADInterstitialAdDelegate包含在我的类中。
presentInView
方法将返回一个布尔值,告诉您它是否正常。 所以我会从检查开始。
如果返回NO
则检查您展示广告的视图是否确实是您希望的尺寸。 如果视图不够大,广告将不会展示。 您可能在数据视图生命周期中过早地展示广告,并且数据视图的尺寸尚未适合屏幕?
其他一些注意事项:
您不需要UIViewController.prepareInterstitialAds()
调用,因为该方法不适用于直接使用 ADInterstitialAd
的用户。 您只准备广告,但创建ADInterstitialAd
的实例。
ADInterstitialAd
对象是一次性对象,因此请确保每次都获得一个新的对象。
此外,UIViewController.prepareInterstitialAds()
是更简单的 API 的一部分,您不必自己管理ADInterstitialAd
。 如果您没有任何特殊要求,您可能更喜欢使用该 API。