4

我试图弄清楚如何从我的横幅视图 iAd 切换到插页式 iAd,以便为选项卡式控制器腾出空间。出于某种原因,我完全找不到任何资源来快速开始这些广告。

谁能给我一些关于 Swift 插页式 iAd 的信息,以及我如何在项目中实现它们。

4

4 回答 4

17

这是实施插页式广告的一种相对清洁且易于遵循的方式,因为这种方式不需要使用NSNotificationCentre

import UIKit
import iAd

class ViewController: UIViewController, ADInterstitialAdDelegate {
var interstitialAd:ADInterstitialAd!
var interstitialAdView: UIView = UIView()

override func viewDidLoad() {
    super.viewDidLoad()

    loadInterstitialAd()
}

func loadInterstitialAd() {
    interstitialAd = ADInterstitialAd()
    interstitialAd.delegate = self
}

func interstitialAdWillLoad(interstitialAd: ADInterstitialAd!) {

}

func interstitialAdDidLoad(interstitialAd: ADInterstitialAd!) {
    interstitialAdView = UIView()
    interstitialAdView.frame = self.view.bounds
    view.addSubview(interstitialAdView)

    interstitialAd.presentInView(interstitialAdView)
    UIViewController.prepareInterstitialAds()
}

func interstitialAdActionDidFinish(interstitialAd: ADInterstitialAd!) {
    interstitialAdView.removeFromSuperview()
}

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

func interstitialAd(interstitialAd: ADInterstitialAd!, didFailWithError error: NSError!) {

}

func interstitialAdDidUnload(interstitialAd: ADInterstitialAd!) {
    interstitialAdView.removeFromSuperview()
}

如果您println("Function Name")添加每个功能只是为了跟踪您的插页式广告流程,这可能会有所帮助。如果您有任何问题和/或改进此代码块的方法,请发表评论。谢谢你

于 2015-01-02T16:35:14.613 回答
3

这是我在项目中使用的方式

//添加iAd框架

 import iAd

//符合 iAd 委托

class ViewController: UIViewController,ADInterstitialAdDelegate 


//create instance variable
var interstitial:ADInterstitialAd!

//默认iAd interstitials不提供关闭按钮,所以我们需要手动创建一个

var placeHolderView:UIView!
var closeButton:UIButton!



override func viewDidLoad() {
    super.viewDidLoad()

    //iAD interstitial
    NSNotificationCenter.defaultCenter().addObserver(self, selector: ("runAd:"),    name:UIApplicationWillEnterForegroundNotification, object: nil)

}




//iAD interstitial
func runAd(notification:NSNotification){
var timer = NSTimer.scheduledTimerWithTimeInterval(3.0, target: self, selector: Selector("dislayiAdInterstitial"), userInfo: nil, repeats: false)
cycleInterstitial()
}

func cycleInterstitial(){

    // Clean up the old interstitial...
    //  interstitial.delegate = nil;
    // and create a new interstitial. We set the delegate so that we can be notified of when
    interstitial = ADInterstitialAd()
    interstitial.delegate = self;
}

func presentInterlude(){
    // If the interstitial managed to load, then we'll present it now.
    if (interstitial.loaded) {

        placeHolderView = UIView(frame: self.view.frame)
        self.view.addSubview(placeHolderView)

        closeButton = UIButton(frame: CGRect(x: 270, y:  25, width: 25, height: 25))
        closeButton.setBackgroundImage(UIImage(named: "error"), forState: UIControlState.Normal)
        closeButton.addTarget(self, action: Selector("close"), forControlEvents: UIControlEvents.TouchDown)
        self.view.addSubview(closeButton)


        interstitial.presentInView(placeHolderView)
    }
}

// iAd Delegate Mehtods

// When this method is invoked, the application should remove the view from the screen and tear it down.
// The content will be unloaded shortly after this method is called and no new content will be loaded in that view.
// This may occur either when the user dismisses the interstitial view via the dismiss button or
// if the content in the view has expired.

func interstitialAdDidUnload(interstitialAd: ADInterstitialAd!){
    placeHolderView.removeFromSuperview()
    closeButton.removeFromSuperview()
    interstitial = nil

    cycleInterstitial()
}


func interstitialAdActionDidFinish(_interstitialAd: ADInterstitialAd!){
    placeHolderView.removeFromSuperview()
    closeButton.removeFromSuperview()
    interstitial = nil

    println("called just before dismissing - action finished")

}

// This method will be invoked when an error has occurred attempting to get advertisement content.
// The ADError enum lists the possible error codes.
func interstitialAd(interstitialAd: ADInterstitialAd!,
    didFailWithError error: NSError!){
        cycleInterstitial()
}


//Load iAd interstitial
func dislayiAdInterstitial() {
    //iAd interstitial
    presentInterlude()
}


func close() {
    placeHolderView.removeFromSuperview()
    closeButton.removeFromSuperview()
    interstitial = nil

}
于 2014-11-28T12:42:51.303 回答
1

我知道这是旧的 - 但我刚刚遇到

override func prepareForSegue(segue: UIStoryboardSegue, 
               sender: AnyObject?) {

let destination = segue.destinationViewController 
            as UIViewController
destination.interstitialPresentationPolicy = 
    ADInterstitialPresentationPolicy.Automatic
}
于 2015-02-28T22:50:46.857 回答
1

如果您将此添加到您的应用程序委托,您将避免上面建议的 3 秒延迟。

 func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        UIViewController.prepareInterstitialAds()
        return true
    }
于 2015-10-03T19:22:34.863 回答