gpt4 book ai didi

swift - 优先考虑 iAd 而非 adMob SpriteKit/Swift

转载 作者:行者123 更新时间:2023-11-30 14:06:35 29 4
gpt4 key购买 nike

我将 iAd 和 adMob 集成到我的游戏中,adMob 代码仅在 iAd 加载失败时运行。如果 iAd 没有加载第一个广告失败,则说明它工作正常。当 iAd 加载失败时,Admob 会正常工作。但是,当 iAd 失败但随后加载下一个广告时,adMob 横幅仍然存在并且不会显示 iAd。即使 adMob 已经加载,我如何才能加载 iAd?此外,当 adMob 代码被注释掉时,iAd 横幅在加载失败后加载正常。顺便提一句,如何更改模拟器中 adMob 广告的填充率?谢谢。这是我在 GameViewController 中的代码(iAd 在 gameScene 中调用):

import UIKit
import SpriteKit
import iAd
import GoogleMobileAds

class GameViewController: UIViewController, ADBannerViewDelegate {

var SH = UIScreen.mainScreen().bounds.height
let transition = SKTransition.fadeWithDuration(1)
var UIiAd: ADBannerView = ADBannerView()
var googleBannerView: GADBannerView!

override func viewWillAppear(animated: Bool) {
/* var BV = UIiAd.bounds.height
UIiAd.delegate = self
UIiAd.frame = CGRectMake(0, SH + BV, 0, 0)
self.view.addSubview(UIiAd) */

UIiAd.setTranslatesAutoresizingMaskIntoConstraints(false)
UIiAd.delegate = self
self.view.addSubview(UIiAd)
let viewsDictionary = ["bannerView":UIiAd]
view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|[bannerView]|", options: .allZeros, metrics: nil, views: viewsDictionary))
view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:[bannerView]|", options: .allZeros, metrics: nil, views: viewsDictionary))

}

override func viewWillDisappear(animated: Bool) {
UIiAd.delegate = nil
UIiAd.removeFromSuperview()
}

func bannerViewDidLoadAd(banner: ADBannerView!) {

var BV = UIiAd.bounds.height
UIView.beginAnimations(nil, context: nil)
UIView.setAnimationDuration(1) // Time it takes the animation to complete
UIiAd.alpha = 1 // Fade in the animation
UIView.commitAnimations()

println("iAd work")
}

func bannerView(banner: ADBannerView!, didFailToReceiveAdWithError error: NSError!) {
UIView.beginAnimations(nil, context: nil)
UIView.setAnimationDuration(1)
UIiAd.alpha = 0
UIView.commitAnimations()

googleBannerView = GADBannerView(adSize: kGADAdSizeSmartBannerLandscape)
googleBannerView.adUnitID = "ca-app-pub-3940256099942544/2934735716"
googleBannerView.rootViewController = self
var request: GADRequest = GADRequest()
googleBannerView.loadRequest(request)

googleBannerView.frame = CGRectMake(0, view.bounds.height - googleBannerView.frame.size.height, googleBannerView.frame.size.width, googleBannerView.frame.size.height)

self.view.addSubview(googleBannerView!)

println("iAd fail")
}

func showBannerAd() {
UIiAd.hidden = false
var BV = UIiAd.bounds.height

UIView.beginAnimations(nil, context: nil)
UIView.setAnimationDuration(1) // Time it takes the animation to complete
UIiAd.frame = CGRectMake(0, SH - BV, 2048, 0) // End position of the animation
UIView.commitAnimations()

}

func hideBannerAd() {
UIiAd.hidden = true
var BV = UIiAd.bounds.height

UIView.beginAnimations(nil, context: nil)
UIView.setAnimationDuration(1) // Time it takes the animation to complete
UIiAd.frame = CGRectMake(0, SH + BV, 0, 0) // End position of the animation
UIView.commitAnimations()
}

override func viewDidLoad() {
super.viewDidLoad()
self.UIiAd.hidden = true
self.UIiAd.alpha = 0

NSNotificationCenter.defaultCenter().addObserver(self, selector: "hideBannerAd", name: "hideadsID", object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "showBannerAd", name: "showadsID", object: nil)

let scene = GameScene(size: CGSize(width: 2048, height: 1356))
let skView = self.view as! SKView
skView.showsFPS = true
skView.showsNodeCount = true
skView.ignoresSiblingOrder = true
scene.scaleMode = SKSceneScaleMode.AspectFill
skView.presentScene(scene)
}

func bannerViewActionShouldBegin(banner: ADBannerView!, willLeaveApplication willLeave: Bool) -> Bool {

println("Clicked")
// let skView: SKView = self.view as! SKView
// skView.scene!.paused = true
return true
}
/* func bannerViewActionDidFinish(banner: ADBannerView!) {
let skView: SKView = self.view as! SKView
skView.scene!.paused = false
} */

override func prefersStatusBarHidden() -> Bool {
return true
}

}

最佳答案

当 iAd 在 bannerViewDidLoadAd(banner: ADBannerView!) 中收到广告时,您并未隐藏 AdMob 横幅。因此,您的 iAd 横幅实际上显示在 AdMob 横幅后面。

或者,您可以在 viewDidLoad 中同时创建 iAd 和 AdMob 横幅,并根据 iAd 是否收到广告隐藏或显示它们。您的 iAd 委托(delegate)方法最终看起来与此类似:

func bannerViewDidLoadAd(banner: ADBannerView!) {
UIView.beginAnimations(nil, context: nil)
// Show iAd
UIiAd.alpha = 1.0
// Hide AdMob
googleBannerView.alpha = 0.0
UIView.commitAnimations()
}

func bannerView(banner: ADBannerView!, didFailToReceiveAdWithError error: NSError!) {
UIView.beginAnimations(nil, context: nil)
// Hide iAd
UIiAd.alpha = 0.0
// Show AdMob
googleBannerView.alpha = 1.0
UIView.commitAnimations()
}

检查我的答案 here完整的实现。

关于swift - 优先考虑 iAd 而非 adMob SpriteKit/Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32311642/

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