Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Swift(Xcode 6.1)中实施间隙式iAds_Swift_Ios8_Iad_Xcode6.1_Interstitial - Fatal编程技术网

如何在Swift(Xcode 6.1)中实施间隙式iAds

如何在Swift(Xcode 6.1)中实施间隙式iAds,swift,ios8,iad,xcode6.1,interstitial,Swift,Ios8,Iad,Xcode6.1,Interstitial,我正试图找出如何从我的横幅视图IAD切换到间隙IAD,以便为选项卡式控制器腾出空间。由于某些原因,我完全无法找到任何资源,甚至无法开始使用swift发布这些广告 有谁能给我一些关于Swift的间隙IAD的信息,以及我如何在项目中实施这些信息。以下是我在项目中的使用方法 //添加iAd框架 import iAd //符合iAd代表 class ViewController: UIViewController,ADInterstitialAdDelegate //create instan

我正试图找出如何从我的横幅视图IAD切换到间隙IAD,以便为选项卡式控制器腾出空间。由于某些原因,我完全无法找到任何资源,甚至无法开始使用swift发布这些广告


有谁能给我一些关于Swift的间隙IAD的信息,以及我如何在项目中实施这些信息。

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

//添加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

}

这是一种相对更干净、更容易遵循的方法来实现间隙式广告,因为这种方法不需要使用
nsnotificationcenter

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(“函数名”)
,只是为了跟踪您的中间Ads过程,这可能会有所帮助。如果您有任何问题和/或改进此代码块的方法,请留下评论。谢谢你

我知道这很旧,但我只是偶然发现的

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

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

如果您将此项添加到应用程序代理中,您将避免上述建议的3秒延迟

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

伟大的代码!我实现了这一点,但我很好奇为什么行中有3.0:NSTimer.scheduledTimerWithTimeInterval(3.0,target:self,selector:selector(“dislayiadinterstitual”),userInfo:nil,repeats:false),所以广告基本上使用数据。所以,你需要等待3秒钟,让广告有机会被下载,以便使用,即使服务不好。(iAds正在关闭。)