gpt4 book ai didi

ios - iAd didFailToReceiveAdWithError 有时不工作,显示白框

转载 作者:行者123 更新时间:2023-11-28 09:01:57 24 4
gpt4 key购买 nike

我正在使用 Swift 和 SpriteKit,一切都在我的 GameViewController 和 GameScene 中完成。

这是与我的 GameViewController 中的广告有关的所有代码:

class GameViewController: UIViewController, ADBannerViewDelegate, GADBannerViewDelegate {

var adBannerView: ADBannerView!
var gadBannerView: GADBannerView!
var bannerDisplayed = false
var bannerNow = "iAd"

override func viewDidLoad() {
super.viewDidLoad()

if let scene = GameScene.unarchiveFromFile("GameScene") as? GameScene {
// Configure the view.
let skView = self.view as! SKView
skView.showsFPS = false
skView.showsNodeCount = false

/* Sprite Kit applies additional optimizations to improve rendering performance */
skView.ignoresSiblingOrder = true

/* Set the scale mode to scale to fit the window */
scene.scaleMode = .AspectFill

if((UIDevice.currentDevice().userInterfaceIdiom == .Phone) && (UIScreen.mainScreen().bounds.height <= 480)) {
println("<= iPhone 4S")
scene.size.height = skView.bounds.size.height * 2
scene.size.width = skView.bounds.size.width * 2
}

NSNotificationCenter.defaultCenter().addObserver(self, selector: "hideADBanner", name: "hideAd", object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "showADBanner", name: "showAd", object: nil)

self.adBannerView = self.loadAds()

skView.presentScene(scene)

println(gadBannerView)

}
}

func loadAds()->ADBannerView{
var adBannerView2 = ADBannerView(frame: CGRect.zeroRect)
adBannerView2.center = CGPoint(x: adBannerView2.center.x, y: view!.bounds.size.height - adBannerView2.frame.size.height / 2)
adBannerView2.delegate = self
adBannerView2.hidden = true

bannerNow = "iAd"

self.view?.addSubview(adBannerView2)
return adBannerView2
}

func bannerViewActionShouldBegin(banner: ADBannerView!, willLeaveApplication willLeave: Bool) -> Bool {
println("left for ad")
return true
}

func bannerViewActionDidFinish(banner: ADBannerView!) {
}

func bannerViewDidLoadAd(banner: ADBannerView!) {
println("ad did load.")
println("Delegate: \(adBannerView.delegate)")
adBannerView.hidden = false
//gadBannerView?.removeFromSuperview()
}

func bannerView(banner: ADBannerView!, didFailToReceiveAdWithError error: NSError!) {
//adBannerView.removeFromSuperview()
//self.gadBannerView = self.createGADBanner()
}

func createGADBanner()->GADBannerView {
var ad = GADBannerView()
ad = GADBannerView(frame: CGRectMake(0, 0, self.view.frame.size.width, 50))
ad.delegate = self
ad.rootViewController = self
ad.adUnitID = "xxxxxxxxxxx"

bannerNow = "GAD"

var reqAd = GADRequest()
//reqAd.testDevices = [GAD_SIMULATOR_ID] // If you want test ad's
ad.loadRequest(reqAd)
self.view.addSubview(ad)
println(ad)
return ad
}

func adViewDidReceiveAd(view: GADBannerView!) {
println("adViewDidReceiveAd:\(view)");
bannerDisplayed = true
relayoutViews()
}

func adView(bannerView: GADBannerView!, didFailToReceiveAdWithError error: GADRequestError!) {
gadBannerView.removeFromSuperview()
}


func relayoutViews() {
if (bannerDisplayed) {
var bannerFrame = gadBannerView!.frame
bannerFrame.origin.x = 0
bannerFrame.origin.y = self.view.bounds.size.height - bannerFrame.size.height

gadBannerView!.frame = bannerFrame
}
}

func hideADBanner() {
println(self.gadBannerView)
println(bannerNow)
//adBannerView?.hidden = true
//gadBannerView?.hidden = true
adBannerView?.removeFromSuperview()
gadBannerView?.removeFromSuperview()
}

func showADBanner() {
self.adBannerView = self.loadAds()
//adBannerView?.hidden = false
//gadBannerView?.hidden = false
}

所以我的计划是在 iAd 不工作时从 AdMob 获取广告,但问题是我的 iAd“总是工作”,即使我已经在 iPhone 的开发者工具中更改了填充率。

当 iAd 加载广告时它工作正常,调用函数 bannerViewDidLoadAd() 并显示广告。虽然有时当我加载 iAd 时它说我已经输入 bannerViewDidLoadAd() 但它只显示一个白色的“框”,底部有 iAd Logo 。现在,这不是我想要的,因为 AdMob 永远不会显示。 println 总是发出相同的委托(delegate),所以看起来至少有效。

这个 View 永远不会刷新,因为正如我所说,一切都在 GameScene 中处理,它只是删除和添加节点。

谢谢!

最佳答案

您使用 bool 和不必要的函数使事情变得过于复杂,看起来您实际上已经在 Interface Builder 中创建了两个 ADBannerViewsself.adBannerView,一旦以编程方式,var adBannerView: ADBannerView!

我创建了一个带有注释的示例,以帮助您一路走下去。这里发生的事情是我们正在创建一个 ADBannerView,在我们的 viewDidLoad 中设置一次,然后根据它是否隐藏/显示我们的 ADBannerView是否从 iAd 网络接收广告。我省略了 AdMob 实现,因为它现在应该非常简单。

import UIKit
import iAd // Import iAd

class ViewController: UIViewController, ADBannerViewDelegate { // Include the delegate for our ADBannerView
var adBannerView = ADBannerView() // Create our ADBannerView
// Create your GADBannerView here

override func viewDidLoad() {
super.viewDidLoad()
loadAds()
}

func loadAds() {
// Setup our ADBannerView
adBannerView = ADBannerView(frame: CGRect.zeroRect)
adBannerView.center = CGPoint(x: view.bounds.size.width / 2, y: view.bounds.size.height - adBannerView.frame.size.height / 2)
adBannerView.delegate = self
adBannerView.hidden = true
view.addSubview(adBannerView)

// Setup your GADBannerView here in the same manner
}

func bannerViewDidLoadAd(banner: ADBannerView!) {
println("bannerViewDidLoadAd")
// We received an ad from iAd. Lets show our banner
adBannerView.hidden = false
// Hide your GADBannerView here
}

func bannerViewActionDidFinish(banner: ADBannerView!) {
println("bannerViewActionDidFinish")
}

func bannerView(banner: ADBannerView!, didFailToReceiveAdWithError error: NSError!) {
println("didFailToReceiveAdWithError: \(error)")
// We failed to receive an ad from iAd. Lets hide our banner and print the error
adBannerView.hidden = true
// Show your GADBannerView here
}

关于ios - iAd didFailToReceiveAdWithError 有时不工作,显示白框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31772062/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com