gpt4 book ai didi

ios - SpriteKit 中的插页式广告?

转载 作者:行者123 更新时间:2023-11-29 01:28:16 25 4
gpt4 key购买 nike

我正在为我的 iOS 应用程序使用 SpriteKit,并且需要一些全屏(或有时称为“插页式”iAd)时常弹出。我可以找出它发生时的代码,但我需要知道如何在不必更改 View Controller 的情况下添加 iAd。

我尝试了 Techotopia 关于 Interstitial iAds 的[教程][1],但要做到这一点,我需要在实际的 View Controller 之间进行转换。好吧,我试过了,但是每当从 iAd View Controller 转换回 GameViewController 时,它就会把一切都搞砸了。 GameViewController.m 中的代码表示它最初设置为我的游戏的主菜单(一个 SKScene)。因此,当我尝试从 iAd 过渡回来时,它没有保持相同的 SKScene,而是直接进入我的主菜单场景。

我需要这些答案:

  1. 除了使用两个 View Controller 来显示 Interstitial iAds 之外,还有其他方法吗?
  2. 如果不是,我该如何解决这个问题?
  3. 如果我将“Interstitial iAds”更改为“Pre-roll Video iAds”,是否也会出现此问题?

-

编辑!编辑!编辑!编辑!编辑!编辑!编辑!编辑!编辑!编辑!编辑!编辑!编辑!编辑!

-

我最终决定使用 crashoverride777 的答案,并创建一个 Objective-C 桥接 header 以将他的 Swift 代码转换为 Objective-C。但是当引用 Ads.swift 的变量 presentingViewController 时,它最终为 nil。这是我在 GameViewController.m 中的代码:

// Set up interstitial iAds.
Ads *adsClass = [[Ads alloc] init];
Ads *adsInstance = [Ads sharedInstance];
adsInstance.presentingViewController = self;

但是,这没有任何作用。从我的 GameScene.m 中调用函数 showInterAd 就像它应该的那样,但没有出现广告。在我的 Ads.swift 中,我添加了一个日志来查看 presentingViewController 是否确实为 nil。

print("iAd inter showing")
if (self.presentingViewController != nil) {
print("presentingViewController != nil")
iAdInterAdView.frame = presentingViewController.view.bounds
presentingViewController.view.addSubview(iAdInterAdView)
iAdInterAd!.presentInView(iAdInterAdView)
UIViewController.prepareInterstitialAds()
iAdInterAdView.addSubview(iAdInterAdCloseButton)
} else {
print("presentingViewController == nil")
}

log每次都是这样出来的:"presentingViewController == nil"。我不确定这里出了什么问题。

最佳答案

您可以使用我在 github 上发布的助手。它是专门为 spritekit 制作的,您可以从任何地方调用广告而无需委托(delegate)等或更改 View Controller 。

https://github.com/crashoverride777/Swift-2-iAds-and-AdMob-Helper

我认为看看这个帮助程序应该会让您清楚地知道从哪里开始。这是一个简化的示例,说明它如何仅适用于 iAds Inter 广告。

这是快速的,所以我不确定它是否仍然对你有帮助。

import iAd

class Ads: NSObject {

// MARK: - Static Properties

/// Shared instance
static let sharedInstance = Ads()

// MARK: - Properties

/// Presenting view controller
var presentingViewController: UIViewController!

/// iAd inter ad
private var iAdInterAd: ADInterstitialAd?

/// iAd inter ad view
private var iAdInterAdView = UIView()

/// iAd inter ad close button
private var iAdInterAdCloseButton = UIButton(type: UIButtonType.System)

// MARK: - Init
private override init() {
super.init()
print("Ads helper init")

iAdInterAd = iAdLoadInterAd()
}

// MARK: - User Methods

/// Show inter ad
func showInterAd() {
iAdShowInterAd()
}

/// Show inter ad randomly (33% chance)
func showInterAdRandomly() {
let randomInterAd = Int(arc4random() % 3)
print("randomInterAd = \(randomInterAd)")
if randomInterAd == 1 {
iAdShowInterAd()
}
}

/// Remove all ads
func removeAllAds() {
print("Removed all ads")

if iAdInterAd != nil {
iAdInterAd!.delegate = nil
iAdInterAdCloseButton.removeFromSuperview()
iAdInterAdView.removeFromSuperview()
}
}

// MARK: - Internal Methods

/// iAd load inter ad
private func iAdLoadInterAd() -> ADInterstitialAd {
print("iAd inter ad loading...")
let iAdInterAd = ADInterstitialAd()
iAdInterAd.delegate = self

if UIDevice.currentDevice().userInterfaceIdiom == .Pad {
iAdInterAdCloseButton.frame = CGRectMake(18, 18, 27, 27)
} else {
iAdInterAdCloseButton.frame = CGRectMake(13, 13, 22, 22)
}

iAdInterAdCloseButton.layer.cornerRadius = 11
iAdInterAdCloseButton.setTitle("X", forState: .Normal)
iAdInterAdCloseButton.setTitleColor(UIColor.grayColor(), forState: .Normal)
iAdInterAdCloseButton.backgroundColor = UIColor.whiteColor()
iAdInterAdCloseButton.layer.borderColor = UIColor.grayColor().CGColor
iAdInterAdCloseButton.layer.borderWidth = 2
iAdInterAdCloseButton.addTarget(self, action: "iAdPressedInterAdCloseButton:", forControlEvents: UIControlEvents.TouchDown)

return iAdInterAd
}

/// iAd show inter ad
private func iAdShowInterAd() {
guard iAdInterAd != nil else {
print("iAd inter is nil, reloading")
iAdInterAd = iAdLoadInterAd()
return
}

if iAdInterAd!.loaded {
print("iAd inter showing")
iAdInterAdView.frame = presentingViewController.view.bounds
presentingViewController.view.addSubview(iAdInterAdView)
iAdInterAd!.presentInView(iAdInterAdView)
UIViewController.prepareInterstitialAds()
iAdInterAdView.addSubview(iAdInterAdCloseButton)

//pauseTasks() // not really needed for inter as you tend to show them when not playing.
} else {
print("iAd inter not ready, reloading again...")
iAdInterAd = iAdLoadInterAd()

}
}

/// iAd inter ad pressed close button
func iAdPressedInterAdCloseButton(sender: UIButton) { // dont make private as its called with a selector
print("iAd inter closed")
iAdInterAd!.delegate = nil
iAdInterAdCloseButton.removeFromSuperview()
iAdInterAdView.removeFromSuperview()
iAdInterAd = iAdLoadInterAd()

//resumeTasks() // not really needed for inter as you tend to not show them during gameplay
}

/// Pause tasks in the app/game
private func pauseTasks() {
// Pause app/game, music etc here.
// you could use NSNotifactionCenter or Delegates to call methods in other SKScenes / ViewControllers
}

/// Resume tasks in the app/game
private func resumeTasks() {
// Resume app/game, music etc here.
// you could use NSNotifactionCenter or Delegates to call methods in other SKScenes / ViewControllers
}
}

// MARK: - Delegates iAd Inter
extension Ads: ADInterstitialAdDelegate {

func interstitialAdDidLoad(interstitialAd: ADInterstitialAd!) {
print("iAd inter did load")
}

func interstitialAdDidUnload(interstitialAd: ADInterstitialAd!) {
print("iAd inter did unload")
}

func interstitialAd(interstitialAd: ADInterstitialAd!, didFailWithError error: NSError!) {
print("iAd inter error \(error)")
iAdInterAd!.delegate = nil
iAdInterAdCloseButton.removeFromSuperview()
iAdInterAdView.removeFromSuperview()
}
}

现在你只需调用

Ads.sharedInstance.presentingViewController = self 

在做任何事情之前在你的 GameViewController 中。这将初始化助手并预加载第一个 inter 广告。

比起您只需在您希望展示广告间的任何地方调用这些

GameData.sharedInstance.showInterAd()

GameData.sharedInstance.showInterAdRandomly() // 33% chance for ad

关于ios - SpriteKit 中的插页式广告?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33766047/

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