如何在Swift中实现插页式iAd(Xcode 6.1)

时间:2014-11-08 00:25:09

标签: swift ios8 iad xcode6.1 interstitial

我正在尝试弄清楚如何从我的横幅视图iAds切换到插页式iAds,以释放标签式控制器的空间。出于某种原因,我完全无法找到任何资源,甚至无法使用swift开始使用这些广告。

有谁可以请给我一些有关Swift的插页式广告的信息,以及如何在项目中实现它们。

3 个答案:

答案 0 :(得分:3)

以下是我在项目中的使用方法

//添加iAd框架

 import iAd

//遵守iAd委托

class ViewController: UIViewController,ADInterstitialAdDelegate 


//create instance variable
var interstitial:ADInterstitialAd!

//默认iAd插页式广告不提供关闭按钮,因此我们需要手动创建一个

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

}

答案 1 :(得分:1)

我知道这已经过时了 - 但我只是遇到了

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

let destination = segue.destinationViewController 
            as UIViewController
destination.interstitialPresentationPolicy = 
    ADInterstitialPresentationPolicy.Automatic
}

答案 2 :(得分:1)

如果您将此添加到您的应用代表,您将避免上面建议的3秒延迟。

 func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        UIViewController.prepareInterstitialAds()
        return true
    }