From 88e80cc3b540a9a560d5d884b79eb5fe1dffe1b1 Mon Sep 17 00:00:00 2001 From: Jill Song Date: Wed, 14 Aug 2024 10:47:38 -0700 Subject: [PATCH] Add Ad Inspector support. PiperOrigin-RevId: 662977413 --- .../BannerExample/Base.lproj/Main.storyboard | 10 ++++++++-- .../BannerExample/ViewController.swift | 20 +++++++++++++++++-- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/Swift/admob/BannerExample/BannerExample/Base.lproj/Main.storyboard b/Swift/admob/BannerExample/BannerExample/Base.lproj/Main.storyboard index eb932808..ae91ad92 100644 --- a/Swift/admob/BannerExample/BannerExample/Base.lproj/Main.storyboard +++ b/Swift/admob/BannerExample/BannerExample/Base.lproj/Main.storyboard @@ -1,9 +1,9 @@ - + - + @@ -56,6 +56,11 @@ + + + + + @@ -64,6 +69,7 @@ + diff --git a/Swift/admob/BannerExample/BannerExample/ViewController.swift b/Swift/admob/BannerExample/BannerExample/ViewController.swift index 82d4bee7..e9844500 100644 --- a/Swift/admob/BannerExample/BannerExample/ViewController.swift +++ b/Swift/admob/BannerExample/BannerExample/ViewController.swift @@ -21,6 +21,7 @@ class ViewController: UIViewController, GADBannerViewDelegate { @IBOutlet weak var bannerView: GADBannerView! @IBOutlet weak var privacySettingsButton: UIBarButtonItem! + @IBOutlet weak var adInspectorButton: UIBarButtonItem! private var isMobileAdsStartCalled = false private var isViewDidAppearCalled = false @@ -76,10 +77,10 @@ class ViewController: UIViewController, GADBannerViewDelegate { }) } - // Handle changes to user consent. + /// Handle changes to user consent. @IBAction func privacySettingsTapped(_ sender: UIBarButtonItem) { GoogleMobileAdsConsentManager.shared.presentPrivacyOptionsForm(from: self) { - [weak self] (formError) in + [weak self] formError in guard let self, let formError else { return } let alertController = UIAlertController( @@ -90,6 +91,21 @@ class ViewController: UIViewController, GADBannerViewDelegate { } } + /// Handle ad inspector launch. + @IBAction func adInspectorTapped(_ sender: UIBarButtonItem) { + GADMobileAds.sharedInstance().presentAdInspector(from: self) { + // Error will be non-nil if there was an issue and the inspector was not displayed. + [weak self] error in + guard let self, let error else { return } + + let alertController = UIAlertController( + title: error.localizedDescription, message: "Please try again later.", + preferredStyle: .alert) + alertController.addAction(UIAlertAction(title: "OK", style: .cancel)) + self.present(alertController, animated: true) + } + } + private func startGoogleMobileAdsSDK() { DispatchQueue.main.async { guard !self.isMobileAdsStartCalled else { return }