GAD adLoader 委托未被调用

GAD adLoader Delegates not get Called

我正在尝试实施原生广告,但没有调用 adLoader 委托。更有趣的是,由于某种原因,委托变成了打印 nil。没有错误打印没有收到。非常感谢任何建议。

func getAd(){
let adLoader = GADAdLoader(adUnitID: adUnitID, rootViewController: self, adTypes: [.native], options: [options])
    adLoader.delegate = self
    print(adLoader.delegate)
    adLoader.load(GADRequest())
}
extension ViewController:GADNativeAdDelegate, GADAdLoaderDelegate{
func adLoader(_ adLoader: GADAdLoader, didReceive nativeAd: GADNativeAd) {
    print("did receive")
  // A native ad has loaded, and can be displayed.
}

func adLoaderDidFinishLoading(_ adLoader: GADAdLoader) {
    print("finish Loading")
    // The adLoader has finished loading ads, and a new request can be sent.
}


func adLoader(_ adLoader: GADAdLoader, didFailToReceiveAdWithError error: Error) {
  print(error)
}
}

目前,您的 GADAdLoader 对象范围在 getAd 函数内。一旦功能结束,所有东西都会释放。

尝试在 class 中设置 GADAdLoader 对象引用。

class ViewController: UIViewController {
    
    var adLoader: GADAdLoader!
    
    func getAd(){
        self.adLoader = GADAdLoader(adUnitID: adUnitID, rootViewController: self, adTypes: [.native], options: [options])
        adLoader.delegate = self
        print(adLoader.delegate)
        adLoader.load(GADRequest())
    }
}

发现问题。对于以后遇到相同问题的任何人,正确的委托是 GADNativeAdLoaderDelegate 而不是 GADNativeAdDelegate 或 GADAdLoaderDelegate