【问题标题】:How to implement Interstitial iAds in Swift(Xcode 6.1)如何在 Swift(Xcode 6.1)中实现插页式 iAd
【发布时间】:2014-11-08 00:25:09
【问题描述】:

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

谁能给我一些有关使用 Swift 的插页式 iAd 以及我如何在项目中实施它们的信息。

【问题讨论】:

  • 查看Swift AdMob Tutorial - 从字面上看,我自己只是经历了这整个磨难,直到我找到了这个。 (iAds 正在关闭。)

标签: swift ios8 iad xcode6.1 interstitial


【解决方案1】:

这是实施插页式广告的一种相对简洁且易于遵循的方式,因为这种方式不需要使用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") 以跟踪您的插页式广告流程,这可能会有所帮助。如果您有任何问题和/或改进此代码块的方法,请发表评论。谢谢你

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

    • 很棒的代码!我得到了这个工作,但我很好奇为什么行中有 3.0: NSTimer.scheduledTimerWithTimeInterval(3.0, target: self, selector: Selector("dislayiAdInterstitial"), userInfo: nil, repeats: false)
    • 所以基本上广告使用数据。因此,即使服务不好,您也需要等待 3 秒才能让广告有机会被下载以便使用。
    【解决方案3】:

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

    override func prepareForSegue(segue: UIStoryboardSegue, 
                   sender: AnyObject?) {
    
    let destination = segue.destinationViewController 
                as UIViewController
    destination.interstitialPresentationPolicy = 
        ADInterstitialPresentationPolicy.Automatic
    }
    

    【讨论】:

      【解决方案4】:

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

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

      【讨论】:

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