插页式iAd在关闭添加时提供断点

时间:2015-02-25 23:09:14

标签: xcode swift

我编写了代码,以便弹出插页式广告。添加弹出窗口很好,但是当我尝试关闭它时,在nowInterlude函数中有一个断点"如果是interstitial.loaded"。任何人都知道为什么会这样吗? 我似乎无法弄明白。

致命错误:在解包可选值时意外发现nil

这是它给出的错误。

var interstitial:ADInterstitialAd!
var closeButton:UIButton!
var placeHolderView:UIView!


override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

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


}

//iAD interstitial
func runAd(notification:NSNotification){
    var timer = NSTimer.scheduledTimerWithTimeInterval(0.1, target: self, selector: Selector("dislayiAdInterstitial"), userInfo: nil, repeats: true)
    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 个答案:

答案 0 :(得分:0)

您的问题是,在您调用方法cycleInterstitial的那一刻。您的interstitial未初始化。您需要更改以下呼叫的顺序:

var timer = NSTimer.scheduledTimerWithTimeInterval(0.1, target: self, selector: Selector("dislayiAdInterstitial"), userInfo: nil, repeats: true)
cycleInterstitial()

要:

cycleInterstitial()
var timer = NSTimer.scheduledTimerWithTimeInterval(0.1, target: self, selector: Selector("dislayiAdInterstitial"), userInfo: nil, repeats: true)

这样,您的ADInterstitialAd已初始化,可以在presentInterlude方法中使用。

此外,我会打开interlude对象以处理可能性,即插曲是nil。因此,使用if let打开它。如果加载了展开的值,你也可以在同一行代码上直接测试它。

所以取而代之的是:

if (interstitial!.loaded) {

有了这个:

if let unwrappedInterstitial = interstitial where unwrappedInterstitial.loaded{

此外,您需要在if-cause中使用unwrappedInterstitial,而不是插页式广告。

所以代码行基本上意味着: 1.检查插页式广告是否存在且不是nil,如果没有nil则检查未打包的对象,是否已加载展开的对象。