【问题标题】:How to implement iAd interstitial with swift如何使用 swift 实现 iAd 插页式广告
【发布时间】:2015-09-27 09:41:22
【问题描述】:

我想在我的 Spritekit 游戏中实现 iAd 插页式广告,但添加它时遇到了麻烦。我就是这样做的:

import iAd
class GameViewController: UIViewController, ADInterstitialAdDelegate {

var interAd:ADInterstitialAd?
var interAdView = UIView()
var closeButton = UIButton.buttonWithType(UIButtonType.System) as! UIButton

override func viewDidLoad() {
    super.viewDidLoad()

    closeButton.frame = CGRectMake(20, 20, 70, 44)
    closeButton.layer.cornerRadius = 10
    closeButton.backgroundColor = UIColor.whiteColor()
    closeButton.layer.borderColor = UIColor.blackColor().CGColor
    closeButton.layer.borderWidth = 1
    closeButton.setTitleColor(UIColor.blackColor(), 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()

    NSNotificationCenter.defaultCenter().addObserver(self, selector: "showAd", name: "showIntAD", object: nil)

func prepareAd() {
    println(" --- AD: Try Load ---")
    // Attempt to load a 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 interAd?.loaded ?? false
}

func closeAd(sender: UIButton) {
    adFinished()
}

func adFinished() {
    closeButton.removeFromSuperview()
    interAdView.removeFromSuperview()
}

func interstitialAdDidLoad(interstitialAd: ADInterstitialAd!) {
    println(" --- AD: Load Success ---")
}

func interstitialAdDidUnload(interstitialAd: ADInterstitialAd!) {
    println(" --- AD: Unload --- ")
}

func interstitialAdActionDidFinish(interstitialAd: ADInterstitialAd!) {
    println(" --- ADD: Action Finished --- ")
    adFinished()
}

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

func interstitialAd(interstitialAd: ADInterstitialAd!, didFailWithError error: NSError!) {
    println(" --- AD: Error --- ")
    println(error.localizedDescription)
    }
}

我正在尝试在游戏场景中召唤 iAd:

class GameScene: SKScene, SKPhysicsContactDelegate, ADInterstitialAdDelegate {

 func showAds(){
    NSNotificationCenter.defaultCenter().postNotificationName("showIntAD", object: nil)
}

//Those two are required by Apple to conform "AdInterstitial" protocol
func interstitialAdDidUnload(interstitialAd: ADInterstitialAd!) {
    println(" --- AD: Unload --- ")
}

func interstitialAd(interstitialAd: ADInterstitialAd!, didFailWithError error: NSError!) {
    println(" --- AD: Error --- ")
    println(error.localizedDescription)
}
}

我有一个 testButton,如果我点击它应该会显示一个广告。它什么也没显示!请帮助我,我正在尝试这样做将近一个星期!

【问题讨论】:

    标签: swift sprite-kit iad


    【解决方案1】:

    您可以更轻松地做到这一点。在您的viewDidLoad 方法中启用Automatic 展示策略并使用requestInterstitialAdPresentation 方法展示插页式广告。我创建了一个showIntersitialAd,它就是这样做的。

    override func viewDidLoad() {
        super.viewDidLoad()
        self.interstitialPresentationPolicy = ADInterstitialPresentationPolicy.Automatic
    }
    
    private func showIntersitialAd() { //call this method whenever you want to show an interstitial
        self.requestInterstitialAdPresentation()
    }
    

    【讨论】:

    • 但是我仍然需要发布func interstitialAdfunc interstitialAdDidUnload,对吧?因为否则它会给我一个错误并说gameviewcontroller doesn't conform to AdInterstitialAD protocol
    • 从您的声明中删除 ADInterstitialAdDelegate
    • 我试过了,把 Automatic 放在 viewDidLoad 中,当我尝试在 GameScene 中创建 showInterstitialAd() 函数时,它给了我一个错误:GameScene doesn't have a member named 'requestInterstitialAdPresentation'
    • 也许我应该在 GameViewController 中执行 showInterstitialAd 并使用 NSNotificationCenter 在 GameScene 中访问它?
    【解决方案2】:

    如果您正在使用 gameviewcontroller 和 SKScenes 编写游戏,那么我在这里的回答可能会对您有所帮助,因为我也是这样做的。

    here.

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-12
      相关资源
      最近更新 更多