iAd's with Swift in SpriteKit

2019-03-02 13:24发布

问题:

I am working on an app and would like to include iAd's. I have managed to add banner ad's and they work well. However i want to a larger add that fills most of the screen but not full screen. I added the adBanner using code from Benzene's Question and given solution from erdekhayser.

Apple iAd Documentation (Page 3)

The link i have referenced above is from the apple documentation and apple refer to the iAd as an MREC ad. This is what i would like to have. I cannot work out how to create and add one though. I have tried resizing the adBanner but still cannot figure it out.

Any help would be appreciated

This is the code i have so far:

import UIKit
import SpriteKit
import iAd
import Twitter

var adBannerView: ADBannerView!

class GameViewController: UIViewController, ADBannerViewDelegate {

var scene: GameScene!

func loadAds() {

    adBannerView = ADBannerView(frame: CGRectZero)
    adBannerView.delegate = self
    adBannerView.hidden = true
    view.addSubview(adBannerView)
}

override func viewDidLoad() {
    super.viewDidLoad()

        // Configure the view.
        let skView = view as SKView
        skView.showsFPS = false
        skView.showsNodeCount = true
        skView.showsPhysics = false

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

        /* Set the scale mode to scale to fit the window */
        scene = GameScene(size: skView.bounds.size)
        scene.scaleMode = .AspectFill

        skView.presentScene(scene)

        //iAd
        loadAds()



}

//iAd
func bannerViewWillLoadAd(banner: ADBannerView!) {


    println("Ad about to load")

}

func bannerViewDidLoadAd(banner: ADBannerView!) {

    adBannerView.center = CGPoint(x: adBannerView.center.x, y: view.bounds.size.height - view.bounds.size.height + adBannerView.frame.size.height / 2)

    adBannerView.hidden = false
    println("Displaying the Ad")

}

func bannerViewActionDidFinish(banner: ADBannerView!) {


    println("Close the Ad")

}

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

    //pause game here


    println("Leave the application to the Ad")
    return true 
}

func bannerView(banner: ADBannerView!, didFailToReceiveAdWithError error: NSError!) {

    //move off bounds when add didnt load

    adBannerView.center = CGPoint(x: adBannerView.center.x, y: view.bounds.size.height + view.bounds.size.height)

    println("Ad is not available")

}

回答1:

Set canDisplayBannerAds to true.

override func viewDidLoad() {
super.viewDidLoad()

    // Configure the view.
    let skView = self.originalContentView as! SKView

    loadAds()

    self.canDisplayBannerAds = true // <--

    skView.showsFPS = false
    skView.showsNodeCount = true
    skView.showsPhysics = false

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

    /* Set the scale mode to scale to fit the window */
    scene = GameScene(size: skView.bounds.size)
    scene.scaleMode = .AspectFill

    skView.presentScene(scene)

}