Admob 插页式广告委托

Admob interstitial ad delegate

我正在尝试为 admob 插页式广告分配委托,以便我可以处理广告事件。

GADInterstitialDelegate 实现 class:

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()

问题是广告事件没有在实施 class 的 AdDelegate 中触发(广告正确显示)知道为什么吗?

我无法在分配给广告对象的 class 中实现 GADInterstitialDelegate,因为广告对象是在 class 函数中创建的,这会导致编译器错误

GADInterstitial 委托是一个弱存储 属性,因此您应该在 class:

中创建一个存储 属性
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