swift - Admob 插页式广告不起作用

标签 swift admob gadinterstitial

我正在处理 Admob 插页式广告,特别是当我的应用的特定 ViewController 加载时,我尝试显示插页式广告。我使用了官方 Admob Interstitial 指南提供的代码,但它不起作用:https://developers.google.com/admob/ios/interstitial?hl=it 。我也在这里关注了这个视频:https://youtu.be/ahmQQ3OeY0Y?t=787 (13.04 分钟停止视频)。如果你看一下代码,它与指南中的相同。我的目标是在 RequestViewController 出现时显示广告,因此我尝试在 viewDidAppear 函数中展示广告。无论如何它不起作用,控制台显示此错误:

To get test ads on this device, call: request.testDevices = @[ kGADSimulatorID ]

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-*******")
    }
}

这是我展示广告的 ViewController:

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

最佳答案

你需要这样写

appDelegate.createAndLoadInterstitial()

代替,

appDelegate.myInterstitial?.present(fromRootViewController: self)

关于swift - Admob 插页式广告不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44305086/

相关文章:

android - 欧洲经济区 (EEA) 广告 (AdMob Android iOS)

flutter - 异常 : The plugin firebase_admob could not be built due to the issue above

ios - 如何检测用户何时关闭 Swift iOS 中的插页式广告?

ios - 解析 XLSX 期间找不到 Xcode 文件错误

ios - UILabels 和自定义 UITableView 单元格

swift - 如何以编程方式添加导航 Controller ,我正在快速使用 XIB 文件

ios - 如何使用 swift iOS(不使用 UITextView)进行多行扩展 UTextField 或文本输入

android - 删除logcat中的admob日志

ios - 是否可以在全屏 UICollectionViewCell 上呈现 GADInterstitial?