首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在Swift中实现间隙iAds (Xcode6.1)

如何在Swift中实现间隙iAds (Xcode6.1)
EN

Stack Overflow用户
提问于 2014-11-08 08:25:09
回答 4查看 5K关注 0票数 4

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

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

EN

回答 4

Stack Overflow用户

发布于 2015-01-03 00:35:15

这里是一种相对干净和容易遵循的实现间隙广告的方法,因为这种方法不需要使用NSNotificationCentre

代码语言:javascript
复制
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")放在每个函数中,仅仅是为了跟踪你的间隙广告过程,这可能会有所帮助。如果你有任何问题,或者有办法改进这段代码,请留下评论。谢谢

票数 17
EN

Stack Overflow用户

发布于 2014-11-28 20:42:51

下面是我如何在我的项目中使用

//添加iAd框架

代码语言:javascript
复制
 import iAd

//符合iAd委托

代码语言:javascript
复制
class ViewController: UIViewController,ADInterstitialAdDelegate 


//create instance variable
var interstitial:ADInterstitialAd!

//默认的iAd interstitials不提供close按钮,需要手动创建一个

代码语言:javascript
复制
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
EN

Stack Overflow用户

发布于 2015-03-01 06:50:47

我知道这很老套-但我只是偶然发现

代码语言:javascript
复制
override func prepareForSegue(segue: UIStoryboardSegue, 
               sender: AnyObject?) {

let destination = segue.destinationViewController 
            as UIViewController
destination.interstitialPresentationPolicy = 
    ADInterstitialPresentationPolicy.Automatic
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26812285

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档