SpriteKit中的插页式iAd? [英] Interstitial iAds in SpriteKit?

查看:101
本文介绍了SpriteKit中的插页式iAd?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将SpriteKit用于我的iOS应用,并且需要一些全屏(有时也称为插页式" iAd)才能经常弹出.我可以弄清楚发生这种情况的代码,但我需要知道如何添加iAd而不必更改View Controller.

I'm using SpriteKit for my iOS app, and need some full-screen (or sometimes called "interstitial" iAds to pop up every so often. I can figure out the code for when it happens, but I need to know how to add iAds without necessarily changing the View Controller.

我尝试了Techotopia的Interstitial iAds的[tutorial] [1],但是要做到这一点,我需要在实际的View Controller之间进行转换.好吧,我尝试了一下,但是每当从iAd视图控制器过渡到GameViewController时,一切都搞砸了. GameViewController.m中的代码说,它最初是设置在游戏主菜单(SKScene)中的.因此,当我尝试从iAd过渡回来时,与其保持相同的SKScene,不如将其停滞在我的主菜单场景中.

I tried a [tutorial][1] by Techotopia on Interstitial iAds, but to do it I need to transition between actual View Controllers. Well, I tried this, but whenever transitioning back from the iAd view controller to the GameViewController, it messed it all up. The code in the GameViewController.m says that it is initially set to the main menu of my game (an SKScene). So when I try transitioning back from the iAd, instead of keeping the same SKScene up, it goes strait to my main menu scene.

我需要这些答案:

  1. 除了使用两个View Controller展示插页式iAd之外,还有其他方法吗?
  2. 如果没有,我该如何解决这个问题?
  3. 如果我将"Interstitial iAds"更改为"Pre-roll Video iAds",也会发生此问题吗?

-

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

-

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

I ended up deciding to use crashoverride777's answer, and creating an Objective-C bridging header to convert his Swift code to Objective-C. But when referencing the Ads.swift's variable presentingViewController, it ends up being nil. Here is my code in GameViewController.m:

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

但是,它什么也没做.函数showInterAd可以从我的GameScene.m调用,但应该不会显示任何广告.在我的Ads.swift中,添加了一个日志,以查看presentingViewController是否确实为零.

This, however, doesn't do anything. The function showInterAd gets called from my GameScene.m like it's supposed to, but no ad appears. In my Ads.swift, I added a log to see if presentingViewController is indeed 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")
}

每次都会这样显示日志:"presentingViewController == nil" .我不确定这是怎么回事.

The log comes out like this every time: "presentingViewController == nil". I'm not sure what's going wrong, here.

推荐答案

您可以利用我在github上发布的帮助程序.它是专门为spritekit设计的,您可以从任何地方调用广告,而无需委托等,也无需更改视图控制器.

You can make use of my helper I posted on github. Its was made specifically for spritekit and you can call ads from anywhere without delegates etc or changing view controllers.

https://github.com/crashoverride777/Swift-2- iAds和AdMob帮助器

我认为看看该帮助程序应该使您对从何处开始有了一个很好的了解.这是一个简化的示例,展示了它仅适用于iAds Inter广告的外观.

I think having a look at that helper should give you a good idea of where to start. Here is a cut down example of how it could look just for iAds Inter ads.

这很快,所以我不确定它是否仍然对您有帮助.

This is in swift so I am not sure if it is still helpful for you.

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中

.这将初始化帮助程序并预加载第一个帧间广告.

in your GameViewController before doing anything else. This will init the helper and preload the first inter ad.

比起您想在其间展示广告的任何地方简单地调用这些

Than you simply call these anywhere you would like inter ads to show

GameData.sharedInstance.showInterAd()

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

这篇关于SpriteKit中的插页式iAd?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆