Admob interstitial广告代表

时间:2015-06-23 10:26:03

标签: ios swift admob interstitial

我正在尝试将代理人分配给admob插页式广告,以便处理广告事件。

GADInterstitialDelegate实施类:

class AdDelegate: NSObject, GADInterstitialDelegate
{
//Interstitial delegate
func interstitial(ad: GADInterstitial!, didFailToReceiveAdWithError error: GADRequestError!) {
    println("interstitialDidFailToReceiveAdWithError:\(error.localizedDescription)")

}

func interstitialDidReceiveAd(ad: GADInterstitial!) {
    println("interstitialDidReceiveAd")

}

func interstitialWillDismissScreen(ad: GADInterstitial!) {
    println("interstitialWillDismissScreen")

}

func interstitialDidDismissScreen(ad: GADInterstitial!) {
    println("interstitialDidDismissScreen")
}

func interstitialWillLeaveApplication(ad: GADInterstitial!) {
    println("interstitialWillLeaveApplication")
}

func interstitialWillPresentScreen(ad: GADInterstitial!) {
    println("interstitialWillPresentScreen")
}

}

将代理人分配给广告:

add.delegate = AdDelegate()

问题是广告事件未在AdDelegate实施类中触发(广告显示正确)任何想法为什么?

我无法在与分配给广告对象相同的类中实现GADInterstitialDelegate,因为广告对象是在类func中创建的,这会给出编译器错误

1 个答案:

答案 0 :(得分:3)

GADInterstitial delegate是一个弱存储属性,因此您应该在类中创建一个存储属性:

var adDelegate : AdDelegate? // to store the delegate 

func createInterstitial()->GADInterstitial {
    var interstitial = GADInterstitial()
    interstitial.adUnitID = "ca-app-pub-6938332798224330/6206234808";
    adDelegate = AdDelegate()
    interstitial.delegate = adDelegate //delegate is weak so we need to store the property

    return interstitial
}

请查看文档以更好地了解ARC的工作原理https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/AutomaticReferenceCounting.html