如何在iAd插页式广告的场景之间转换?

时间:2015-12-02 06:11:00

标签: storyboard iad viewcontroller interstitial skscene

我有一个spritekit游戏,我没有使用故事板或场景的经验。我已将以下代码添加到我的GameViewController文件中:

  

class adScene:SKScene,ADInterstitialAdDelegate {       var interAd:ADInterstitialAd?       var interAdView = UIView()       var closeButton = UIButton.buttonWithType(UIButtonType.System)as!的UIButton

override func didMoveToView(view: SKView) {

    // Try to display ad
    let adShown = showAd()

    // If ad doesn't show
    if adShown == false {
        println("something broke")

    }

    closeButton.frame = CGRectMake(20, 20, 70, 44)
    closeButton.layer.cornerRadius = 10
    closeButton.backgroundColor = UIColor.whiteColor()
    closeButton.layer.borderColor = UIColor.whiteColor().CGColor
    closeButton.layer.borderWidth = 1
    closeButton.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal)
    closeButton.addTarget(self, action: "closeAd:", forControlEvents: UIControlEvents.TouchDown)
    closeButton.enabled = false
    closeButton.setTitle("skip", forState: UIControlState.Normal)
    closeButton.enabled = true
    closeButton.setNeedsLayout()
}

func prepareAd() {
    println(" --- AD: Try Load ---")
    // Attempt to load new ad:
    interAd = ADInterstitialAd()
    interAd?.delegate = self
}

func showAd() -> Bool {
    if interAd != nil && interAd!.loaded {
        interAdView = UIView()
        interAdView.frame = self.view!.bounds
        self.view?.addSubview(interAdView)

        interAd!.presentInView(interAdView)
        UIViewController.prepareInterstitialAds()

        interAdView.addSubview(closeButton)
    }

    // Return true if showing an ad, otherwise false:
    return interAd?.loaded ?? false
}

// When the user clicks the close button, route to the adFinished function:
func closeAd(sender: UIButton) {
    adFinished()
}

// A function of common functionality to run when the user returns to your app:
func adFinished() {
    closeButton.removeFromSuperview()
    interAdView.removeFromSuperview()
    // (Do whatever is next in your app)
}

// The ad loaded successfully
func interstitialAdDidLoad(interstitialAd: ADInterstitialAd!) {
    println(" --- AD: Load Success ---")
}

// The ad unloaded
func interstitialAdDidUnload(interstitialAd: ADInterstitialAd!) {
    println(" --- AD: Unload --- ")
}

// This is called if the user clicks into the interstitial, and then finishes interacting with the ad
// We'll call our adFinished function since we're returning to our app:
func interstitialAdActionDidFinish(interstitialAd: ADInterstitialAd!) {
    println(" --- ADD: Action Finished --- ")
    adFinished()
}

func interstitialAdActionShouldBegin(interstitialAd: ADInterstitialAd!, willLeaveApplication willLeave: Bool) -> Bool {
    return true
}

// Error in the ad load, print out the error
func interstitialAd(interstitialAd: ADInterstitialAd!, didFailWithError error: NSError!) {
    println(" --- AD: Error --- ")
    println(error.localizedDescription)
}

然后,在GameScene中,我创建var myInterAd = adScene(),然后按myInterAd.prepareAd()准备广告,然后尝试按self.view?.presentScene(myInterAd, transition: .crossFadeWithDuration(0.6))展示广告。

但是当场景加载时屏幕才会变黑。我不知道如何正确使用故事板,我创建了一个View Controller场景(将它链接到ViewController(),但我遗漏了一些东西。谢谢。

编辑:我的代码是否完成了切换到另一个场景(ViewController场景)然后回到GameScene的工作?或者我是否需要在故事板中做一些事情?

1 个答案:

答案 0 :(得分:0)

你真的不需要使用SKScene来展示广告。您希望使用viewController在当前场景上叠加添加。

如果您在此问题中查看我的市场答案,您应该能够立即整合插页式广告。它非常简单。

Interstitial iAds in SpriteKit?