简体   繁体   English

Admob非页内广告无效

[英]Admob interstitial doesn't work

I'm dealing with the Admob interstitials and in particular I'm trying to display an interstitial when a particular ViewController of my app loads. 我正在处理Admob插页式广告,尤其是我尝试在加载应用程序的特定ViewController时显示插页式广告。 I used the code provided by the official Admob Interstitial guide but it doesn't work : https://developers.google.com/admob/ios/interstitial?hl=it . 我使用的是Admob插页式官方指南提供的代码,但无效: https : //developers.google.com/admob/ios/interstitial?hl= it。 I also followed this video here : https://youtu.be/ahmQQ3OeY0Y?t=787 (minute 13.04 stop the video). 我也在这里观看了此视频: https ://youtu.be/ahmQQ3OeY0Y?t = 787(第13.04分钟停止播放视频)。 If you look at the code is the same as in the guide. 如果您查看的代码与指南中的代码相同。 My objective is to display the ad when the RequestViewController appears so I try to present the ad in the viewDidAppear function. 我的目标是在RequestViewController出现时显示广告,因此我尝试在viewDidAppear函数中展示广告。 Anyway it doesn't work, the console displays this error: 无论如何,控制台将显示以下错误:

To get test ads on this device, call: request.testDevices = @[ kGADSimulatorID ] 要在此设备上获取测试广告,请致电:request.testDevices = @ [kGADSimulatorID]

AppDelegate.swift AppDelegate.swift

import UIKit
import UserNotifications
import GoogleMobileAds

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate,GADInterstitialDelegate {

var window: UIWindow?

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.

        GADMobileAds.configure(withApplicationID: "ca-app-pub-*******")
    }
}

This is the ViewController where I present the ad: 这是我展示广告的ViewController:

RequestviewController.swift RequestviewController.swift

class RequestViewController: UIViewController {

var myInterstitial : GADInterstitial?

override func viewDidLoad() {
    super.viewDidLoad()
}

override func viewDidAppear(_ animated: Bool) {

    //display ad

    myInterstitial = createAndLoadInterstitial()

    if (myInterstitial?.isReady)!{
        print("\n\n\n\n\nReady")
        myInterstitial?.present(fromRootViewController: self)
    }else { print("\n\n\n\nAd wasn't ready") }
}

func createAndLoadInterstitial()->GADInterstitial {
    let interstitial = GADInterstitial(adUnitID: "ca-app-pub-3940256099942544/4411468910")  //test Ad unit id
    interstitial.delegate = self
    interstitial.load(GADRequest())
    return interstitial
}
func interstitialDidReceiveAd(ad: GADInterstitial!) {
    print("interstitialDidReceiveAd")
}

func interstitial(ad: GADInterstitial!, didFailToReceiveAdWithError error: GADRequestError!) {
    print(error.localizedDescription)
}

func interstitialDidDismissScreen(ad: GADInterstitial!) {
    print("\n\n\n\n\n\ninterstitialDidDismissScreen")
    myInterstitial = createAndLoadInterstitial()
}

you need to write it like, 你需要这样写

appDelegate.createAndLoadInterstitial()

in place of, 代替

appDelegate.myInterstitial?.present(fromRootViewController: self)

It looks like the root problem is this section of RequestViewController: 看起来根本的问题是RequestViewController的以下部分:

myInterstitial = createAndLoadInterstitial()

if (myInterstitial?.isReady)!{
    print("\n\n\n\n\nReady")
    myInterstitial?.present(fromRootViewController: self)
}else { print("\n\n\n\nAd wasn't ready") }

Interstitials are loaded asynchronously, which means the call to load() will return before the ad is loaded and ready to display. 插页式广告是异步加载的,这意味着对load()的调用将在广告加载并准备显示之前返回。 If your app is calling load() in createAndLoadInterstitial and then checking isReady immediately afterwards, there's no time for the ad to actually load, and isReady will always be false. 如果您的应用在createAndLoadInterstitial调用load() ,然后立即检查isReady ,则该广告没有时间实际加载,并且isReady始终为false。

A good way to deal with this could be to create the interstitial in your first view controller, and then show it before transitioning to RequestViewController . 解决此问题的一种好方法是在第一个视图控制器中创建插页式广告,然后过渡到RequestViewController 之前显示它。 That would give the ad time to load, and would still display it during the same transition. 这样可以给广告加载时间,并且仍然可以在相同的过渡期间展示广告。

FWIW, transition time between ViewControllers is a great place in the flow of your app to use an interstitial, so well done there. FWIW,ViewControllers之间的过渡时间是使用插页式广告在应用程序流程中的绝佳位置,在此做得很好。

Also, the line: 另外,该行:

To get test ads on this device, call: request.testDevices = @[ kGADSimulatorID ] 要在此设备上获取测试广告,请致电:request.testDevices = @ [kGADSimulatorID]

isn't an error. 不是错误。 The Android and iOS SDKs always print that to the log so publishers will know how to get test ads for a particular device or emulator. Android和iOS SDK始终将其打印到日志中,因此发布者将知道如何获取特定设备或模拟器的测试广告。 If you were running on a hardware device instead of the iOS simulator, you'd see a unique identifier for the device in that line, which you could then use in your code to get test ads. 如果您是在硬件设备而不是iOS模拟器上运行,则在该行中会看到该设备的唯一标识符,然后可以在代码中使用该标识符来获取测试广告。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM