diff --git a/Swift/advanced/APIDemo/APIDemo.xcodeproj/project.pbxproj b/Swift/advanced/APIDemo/APIDemo.xcodeproj/project.pbxproj index 7d2749dc..e8fcf9b5 100644 --- a/Swift/advanced/APIDemo/APIDemo.xcodeproj/project.pbxproj +++ b/Swift/advanced/APIDemo/APIDemo.xcodeproj/project.pbxproj @@ -7,6 +7,9 @@ objects = { /* Begin PBXBuildFile section */ + 1C04FB9F2CCACE7B00D6EEE6 /* AdmobPreloadView.xib in Resources */ = {isa = PBXBuildFile; fileRef = 1C04FB9D2CCACE7B00D6EEE6 /* AdmobPreloadView.xib */; }; + 1C04FBA02CCACE7B00D6EEE6 /* AdmobPreloadViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1C04FB9E2CCACE7B00D6EEE6 /* AdmobPreloadViewController.swift */; }; + 1C04FBA12CCACE7B00D6EEE6 /* AdmobPreloadView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1C04FB9C2CCACE7B00D6EEE6 /* AdmobPreloadView.swift */; }; 1CD352682C9CC97200534FCC /* CustomControls.xib in Resources */ = {isa = PBXBuildFile; fileRef = 1CD352672C9CC97200534FCC /* CustomControls.xib */; }; 4A7A6CD71C76237500FB1A32 /* Constants.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4A7A6CD61C76237500FB1A32 /* Constants.swift */; }; 4AA7D6911C625A1200DFD2EB /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4AA7D6901C625A1200DFD2EB /* AppDelegate.swift */; }; @@ -34,6 +37,15 @@ /* Begin PBXFileReference section */ 15CB61211C7D0256000212DE /* APIDemo-Bridging-Header.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "APIDemo-Bridging-Header.h"; sourceTree = ""; }; + 1C04FB9C2CCACE7B00D6EEE6 /* AdmobPreloadView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AdmobPreloadView.swift; sourceTree = ""; }; + 1C04FB9D2CCACE7B00D6EEE6 /* AdmobPreloadView.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = AdmobPreloadView.xib; sourceTree = ""; }; + 1C04FB9E2CCACE7B00D6EEE6 /* AdmobPreloadViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AdmobPreloadViewController.swift; sourceTree = ""; }; + 1C04FBA22CCACE8A00D6EEE6 /* GADAppOpenAd_Preview.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = GADAppOpenAd_Preview.h; sourceTree = ""; }; + 1C04FBA32CCACE8A00D6EEE6 /* GADInterstitialAd_Preview.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = GADInterstitialAd_Preview.h; sourceTree = ""; }; + 1C04FBA42CCACE8A00D6EEE6 /* GADMobileAds_Preview.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = GADMobileAds_Preview.h; sourceTree = ""; }; + 1C04FBA52CCACE8A00D6EEE6 /* GADPreloadConfiguration_Preview.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = GADPreloadConfiguration_Preview.h; sourceTree = ""; }; + 1C04FBA62CCACE8A00D6EEE6 /* GADPreloadEventDelegate_Preview.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = GADPreloadEventDelegate_Preview.h; sourceTree = ""; }; + 1C04FBA72CCACE8A00D6EEE6 /* GADRewardedAd_Preview.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = GADRewardedAd_Preview.h; sourceTree = ""; }; 1CD352672C9CC97200534FCC /* CustomControls.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = CustomControls.xib; sourceTree = ""; }; 4A7A6CD61C76237500FB1A32 /* Constants.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Constants.swift; sourceTree = ""; }; 4AA7D68D1C625A1200DFD2EB /* APIDemo.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = APIDemo.app; sourceTree = BUILT_PRODUCTS_DIR; }; @@ -120,6 +132,7 @@ 4AA7D6AD1C626F2D00DFD2EB /* AdMobBannerSizesViewController.swift */, AE3068C921221F7E006D7843 /* AdMobNativeCustomMuteThisAdViewController.swift */, AED11115203213DF00EA4BEE /* AdManagerCustomVideoControls */, + AED11115203213DF00EA4BEC /* AdmobPreload */, ); name = Controllers; sourceTree = ""; @@ -137,6 +150,22 @@ name = AdManagerCustomVideoControls; sourceTree = ""; }; + AED11115203213DF00EA4BEC /* AdmobPreload */ = { + isa = PBXGroup; + children = ( + 1C04FB9C2CCACE7B00D6EEE6 /* AdmobPreloadView.swift */, + 1C04FB9D2CCACE7B00D6EEE6 /* AdmobPreloadView.xib */, + 1C04FB9E2CCACE7B00D6EEE6 /* AdmobPreloadViewController.swift */, + 1C04FBA22CCACE8A00D6EEE6 /* GADAppOpenAd_Preview.h */, + 1C04FBA32CCACE8A00D6EEE6 /* GADInterstitialAd_Preview.h */, + 1C04FBA42CCACE8A00D6EEE6 /* GADMobileAds_Preview.h */, + 1C04FBA52CCACE8A00D6EEE6 /* GADPreloadConfiguration_Preview.h */, + 1C04FBA62CCACE8A00D6EEE6 /* GADPreloadEventDelegate_Preview.h */, + 1C04FBA72CCACE8A00D6EEE6 /* GADRewardedAd_Preview.h */, + ); + name = AdmobPreload; + sourceTree = ""; + }; E67BC192F0B0F683C00B6670 /* Pods */ = { isa = PBXGroup; children = ( @@ -206,6 +235,7 @@ files = ( AED1111F203213FC00EA4BEE /* SimpleNativeAdView.xib in Resources */, 4AA7D69B1C625A1200DFD2EB /* LaunchScreen.storyboard in Resources */, + 1C04FB9F2CCACE7B00D6EEE6 /* AdmobPreloadView.xib in Resources */, 4AA7D6981C625A1200DFD2EB /* Assets.xcassets in Resources */, 4AA7D6961C625A1200DFD2EB /* Main.storyboard in Resources */, 1CD352682C9CC97200534FCC /* CustomControls.xib in Resources */, @@ -230,6 +260,8 @@ 5038FC6F251144E300750F9C /* MainViewController.swift in Sources */, 507818F8219A418100E5A44A /* AdManagerCustomVideoControlsController.swift in Sources */, 507818FA219A418100E5A44A /* AdManagerFluidAdSizeViewController.swift in Sources */, + 1C04FBA02CCACE7B00D6EEE6 /* AdmobPreloadViewController.swift in Sources */, + 1C04FBA12CCACE7B00D6EEE6 /* AdmobPreloadView.swift in Sources */, 4A7A6CD71C76237500FB1A32 /* Constants.swift in Sources */, 507818F6219A418100E5A44A /* AdManagerPPIDViewController.swift in Sources */, EFC712462AD853D80014A2BF /* CollapsibleBannerViewController.swift in Sources */, diff --git a/Swift/advanced/APIDemo/APIDemo/APIDemo-Bridging-Header.h b/Swift/advanced/APIDemo/APIDemo/APIDemo-Bridging-Header.h index 55478d56..a05654af 100644 --- a/Swift/advanced/APIDemo/APIDemo/APIDemo-Bridging-Header.h +++ b/Swift/advanced/APIDemo/APIDemo/APIDemo-Bridging-Header.h @@ -15,3 +15,10 @@ // #import + +#import "GADAppOpenAd_Preview.h" +#import "GADInterstitialAd_Preview.h" +#import "GADMobileAds_Preview.h" +#import "GADPreloadConfiguration_Preview.h" +#import "GADPreloadEventDelegate_Preview.h" +#import "GADRewardedAd_Preview.h" diff --git a/Swift/advanced/APIDemo/APIDemo/AdmobPreloadView.swift b/Swift/advanced/APIDemo/APIDemo/AdmobPreloadView.swift new file mode 100644 index 00000000..23b7bf14 --- /dev/null +++ b/Swift/advanced/APIDemo/APIDemo/AdmobPreloadView.swift @@ -0,0 +1,47 @@ +// +// Copyright 2024 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +import Foundation +import UIKit + +class AdmobPreloadView: UIView { + + var showDelegate: (() -> Void)! + + @IBOutlet weak var titleText: UILabel! + @IBOutlet weak var statusText: UILabel! + @IBOutlet weak var showButton: UIButton! + + @IBAction func show(_ sender: Any) { + showDelegate() + } + + static func load(title: String, showDelegate: @escaping () -> Void) + -> AdmobPreloadView! + { + guard + let preloadView = Bundle.main.loadNibNamed("AdmobPreloadView", owner: self, options: nil)? + .first + as? AdmobPreloadView + else { + print("Error loading AdmobPreloadView nib file.") + return nil + } + preloadView.titleText.text = title + preloadView.showDelegate = showDelegate + return preloadView + } +} diff --git a/Swift/advanced/APIDemo/APIDemo/AdmobPreloadView.xib b/Swift/advanced/APIDemo/APIDemo/AdmobPreloadView.xib new file mode 100644 index 00000000..f3dd2b1b --- /dev/null +++ b/Swift/advanced/APIDemo/APIDemo/AdmobPreloadView.xib @@ -0,0 +1,58 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/Swift/advanced/APIDemo/APIDemo/AdmobPreloadViewController.swift b/Swift/advanced/APIDemo/APIDemo/AdmobPreloadViewController.swift new file mode 100644 index 00000000..a288b870 --- /dev/null +++ b/Swift/advanced/APIDemo/APIDemo/AdmobPreloadViewController.swift @@ -0,0 +1,193 @@ +// +// Copyright 2024 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +import GoogleMobileAds +import UIKit + +class AdmobPreloadViewController: UIViewController, GADPreloadEventDelegate, + GADFullScreenContentDelegate +{ + + // Status messages for preload views. + private final var preloadAvailable = "Is available." + private final var preloadExhausted = "Is exhausted." + + @IBOutlet weak var preloadContainer: UIStackView! + + private var isMobileAdsStartCalled = false + private var interstitialView: AdmobPreloadView! + private var rewardedView: AdmobPreloadView! + private var appOpenView: AdmobPreloadView! + + override func viewDidLoad() { + super.viewDidLoad() + startGoogleMobileAdsPreload() + configurePreloadViews() + } + + // [START start_preload] + private func startGoogleMobileAdsPreload() { + // Define a list of PreloadConfigurations of ad units and formats you want to preload. + let interstitialConfig = GADPreloadConfiguration.init( + adUnitID: Constants.admobPreloadInterstitialAdUnitID, + adFormat: GADAdFormat.interstitial + ) + + let rewardedConfig = GADPreloadConfiguration.init( + adUnitID: Constants.admobPreloadRewardedAdUnitID, + adFormat: GADAdFormat.rewarded + ) + + let appOpenConfig = GADPreloadConfiguration.init( + adUnitID: Constants.admobPreloadAppOpenAdUnitID, + adFormat: GADAdFormat.appOpen + ) + + // Optionally set a custom GADRequest for each configuration. + //interstitialConfig.request = GADRequest() + //rewardedConfig.request = GADRequest() + //appOpenConfig.request = GADRequest() + + // Optionally set the quantity of ads that can be cached for each ad units. + interstitialConfig.bufferSize = 2 + rewardedConfig.bufferSize = 2 + appOpenConfig.bufferSize = 2 + + // Start the preloading initialization process. + GADMobileAds.sharedInstance().preload( + with: [interstitialConfig, rewardedConfig, appOpenConfig], + delegate: self + ) + } + + // [END start_preload] + + // [START isAdAvailable] + private func isInterstitialAvailable() -> Bool { + return GADInterstitialAd.isPreloadedAdAvailable(Constants.admobPreloadInterstitialAdUnitID) + } + + // [END isAdAvailable] + + private func isRewardedAvailable() -> Bool { + // Verify that an ad is available before polling. + return GADRewardedAd.isPreloadedAdAvailable(Constants.admobPreloadRewardedAdUnitID) + } + + private func isAppOpenAvailable() -> Bool { + // Verify that an ad is available before polling. + return GADAppOpenAd.isPreloadedAdAvailable(Constants.admobPreloadAppOpenAdUnitID) + } + + // [START pollAndShowAd] + private func showInterstitialAd() { + // Verify that the preloaded ad is available before polling. + guard isInterstitialAvailable() else { + printAndShowAlert("Preload interstitial ad is exhausted.") + return + } + + // Polling returns the next available ad and load another ad in the background. + let ad = GADInterstitialAd.preloadedAd(withAdUnitID: Constants.admobPreloadInterstitialAdUnitID) + ad?.fullScreenContentDelegate = self + ad?.present(fromRootViewController: self) + } + + // [END pollAndShowAd] + + private func showRewardedAd() { + // Verify that the preloaded ad is available before polling. + guard isRewardedAvailable() else { + printAndShowAlert("Preloaded rewarded ad is exhausted.") + return + } + + // Polling returns the next available ad and load another ad in the background. + let ad = GADRewardedAd.preloadedAd(withAdUnitID: Constants.admobPreloadRewardedAdUnitID) + ad?.fullScreenContentDelegate = self + ad?.present(fromRootViewController: self) { + if let reward = ad?.adReward { + print("User was rewarded \(reward.amount) \(reward.type)") + } + } + } + + private func showAppOpenAd() { + // Verify that the preloaded ad is available before polling. + guard isAppOpenAvailable() else { + printAndShowAlert("Preload app open ad is exhausted.") + return + } + + // Polling returns the next available ad and load another ad in the background. + let ad = GADAppOpenAd.preloadedAd(withAdUnitID: Constants.admobPreloadAppOpenAdUnitID) + ad?.fullScreenContentDelegate = self + ad?.present(fromRootViewController: self) + } + + private func configurePreloadViews() { + interstitialView = AdmobPreloadView.load( + title: "Interstitial", showDelegate: showInterstitialAd) + preloadContainer.addArrangedSubview(interstitialView) + rewardedView = AdmobPreloadView.load(title: "Rewarded", showDelegate: showRewardedAd) + preloadContainer.addArrangedSubview(rewardedView) + appOpenView = AdmobPreloadView.load(title: "App open", showDelegate: showAppOpenAd) + preloadContainer.addArrangedSubview(appOpenView) + + updatePreloadViews() + } + + private func updatePreloadViews() { + updatePreloadView(preloadView: interstitialView, isAvailable: isInterstitialAvailable()) + updatePreloadView(preloadView: rewardedView, isAvailable: isRewardedAvailable()) + updatePreloadView(preloadView: appOpenView, isAvailable: isAppOpenAvailable()) + } + + private func updatePreloadView(preloadView: AdmobPreloadView, isAvailable: Bool) { + preloadView.showButton.isEnabled = isAvailable + preloadView.statusText.text = isAvailable ? preloadAvailable : preloadExhausted + preloadView.statusText.textColor = isAvailable ? UIColor.blue : UIColor.red + } + + private func printAndShowAlert(_ message: String) { + let alert = UIAlertController(title: "Alert", message: message, preferredStyle: .alert) + let dismissAction = UIAlertAction(title: "Dismiss", style: .default) + alert.addAction(dismissAction) + present(alert, animated: true, completion: nil) + } + + // MARK: GADPreloadEventDelegate + + func adAvailable(for configuration: GADPreloadConfiguration) { + print("Ad preloaded successfully for ad unit ID: \(configuration.adUnitID)") + updatePreloadViews() + } + + func adsExhausted(for configuration: GADPreloadConfiguration) { + print("Ad exhausted for ad unit ID: \(configuration.adUnitID)") + updatePreloadViews() + } + + // MARK: GADFullScreenContentDelegate + + func adWillPresentFullScreenContent(_ ad: GADFullScreenPresentingAd) { + print("Preloaded ad will be presented.") + } + + func adDidDismissFullScreenContent(_ ad: GADFullScreenPresentingAd) { + print("Preloaded ad dismissed.") + } +} diff --git a/Swift/advanced/APIDemo/APIDemo/Base.lproj/Main.storyboard b/Swift/advanced/APIDemo/APIDemo/Base.lproj/Main.storyboard index 1228f63c..177b23ea 100644 --- a/Swift/advanced/APIDemo/APIDemo/Base.lproj/Main.storyboard +++ b/Swift/advanced/APIDemo/APIDemo/Base.lproj/Main.storyboard @@ -2,7 +2,7 @@ - + @@ -33,6 +33,9 @@ + + + @@ -1119,6 +1122,34 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/Swift/advanced/APIDemo/APIDemo/Constants.swift b/Swift/advanced/APIDemo/APIDemo/Constants.swift index ce473395..3b680fdd 100644 --- a/Swift/advanced/APIDemo/APIDemo/Constants.swift +++ b/Swift/advanced/APIDemo/APIDemo/Constants.swift @@ -49,4 +49,10 @@ struct Constants { /// Collapsible banner ad unit ID. static let collapsibleBannerAdUnitID = "ca-app-pub-3940256099942544/8388050270" + /// Preload ad unit IDs for testing. + static let admobPreloadAppOpenAdUnitID = "ca-app-pub-3940256099942544/5575463023" + + static let admobPreloadInterstitialAdUnitID = "ca-app-pub-3940256099942544/4411468910" + + static let admobPreloadRewardedAdUnitID = "ca-app-pub-3940256099942544/1712485313" } diff --git a/Swift/advanced/APIDemo/APIDemo/GADAppOpenAd_Preview.h b/Swift/advanced/APIDemo/APIDemo/GADAppOpenAd_Preview.h new file mode 100644 index 00000000..d6327fd9 --- /dev/null +++ b/Swift/advanced/APIDemo/APIDemo/GADAppOpenAd_Preview.h @@ -0,0 +1,29 @@ +// +// Copyright 2024 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +#import +#import + +@interface GADAppOpenAd () + +/// Returns if an app open ad is preloaded for the given ad unit ID. ++ (BOOL)isPreloadedAdAvailable:(nonnull NSString *)adUnitID; + +/// Returns the preloaded app open ad corresponding to the given ad unit ID. Returns nil if +/// an ad is not available. ++ (nullable GADAppOpenAd *)preloadedAdWithAdUnitID:(nonnull NSString *)adUnitID; + +@end diff --git a/Swift/advanced/APIDemo/APIDemo/GADInterstitialAd_Preview.h b/Swift/advanced/APIDemo/APIDemo/GADInterstitialAd_Preview.h new file mode 100644 index 00000000..f4ac7c73 --- /dev/null +++ b/Swift/advanced/APIDemo/APIDemo/GADInterstitialAd_Preview.h @@ -0,0 +1,29 @@ +// +// Copyright 2024 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +#import +#import + +@interface GADInterstitialAd () + +/// Returns whether an interstitial ad is preloaded for the given ad unit ID. ++ (BOOL)isPreloadedAdAvailable:(nonnull NSString *)adUnitID; + +/// Returns a preloaded interstitial ad corresponding to the given ad unit ID. Returns nil if +/// an ad is not available. ++ (nullable GADInterstitialAd *)preloadedAdWithAdUnitID:(nonnull NSString *)adUnitID; + +@end diff --git a/Swift/advanced/APIDemo/APIDemo/GADMobileAds_Preview.h b/Swift/advanced/APIDemo/APIDemo/GADMobileAds_Preview.h new file mode 100644 index 00000000..d6bf2811 --- /dev/null +++ b/Swift/advanced/APIDemo/APIDemo/GADMobileAds_Preview.h @@ -0,0 +1,27 @@ +// +// Copyright 2024 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +#import +#import "GADPreloadConfiguration_Preview.h" +#import "GADPreloadEventDelegate_Preview.h" + +@interface GADMobileAds () + +/// Starts preloading full screen ads from the configurations. +/// Ad loads and ad expiration events will be forwarded to the delegate provided. +- (void)preloadWithConfigurations:(nonnull NSArray *)configurations + delegate:(nonnull id)delegate; +@end diff --git a/Swift/advanced/APIDemo/APIDemo/GADPreloadConfiguration_Preview.h b/Swift/advanced/APIDemo/APIDemo/GADPreloadConfiguration_Preview.h new file mode 100644 index 00000000..246024f3 --- /dev/null +++ b/Swift/advanced/APIDemo/APIDemo/GADPreloadConfiguration_Preview.h @@ -0,0 +1,43 @@ +// +// Copyright 2024 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +#import +#import + +/// Configuration for preloading ads. +@interface GADPreloadConfiguration : NSObject + +/// The ad unit ID. +@property(nonatomic, nonnull, readonly) NSString *adUnitID; + +/// The GADRequest object. +@property(nonatomic, nonnull, readonly) GADRequest *request; + +/// The format. Interstitial, rewarded, and app open ads are supported. +@property(nonatomic, readonly) GADAdFormat format; + +/// The maximum amount of ads buffered for this configuration. +@property(nonatomic, readwrite) NSUInteger bufferSize; + +/// Initializes a GADPreloadConfiguration with ad unit ID, request, and format. +- (nonnull instancetype)initWithAdUnitID:(nonnull NSString *)adUnitID + adFormat:(GADAdFormat)format + request:(nonnull GADRequest *)request; + +/// Initializes a GADPreloadConfiguration with ad unit ID, format, and default request object. +- (nonnull instancetype)initWithAdUnitID:(nonnull NSString *)adUnitID adFormat:(GADAdFormat)format; + +@end diff --git a/Swift/advanced/APIDemo/APIDemo/GADPreloadEventDelegate_Preview.h b/Swift/advanced/APIDemo/APIDemo/GADPreloadEventDelegate_Preview.h new file mode 100644 index 00000000..22de4700 --- /dev/null +++ b/Swift/advanced/APIDemo/APIDemo/GADPreloadEventDelegate_Preview.h @@ -0,0 +1,28 @@ +// +// Copyright 2024 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +#import "GADPreloadConfiguration_Preview.h" + +/// Delegate for preloading events. +@protocol GADPreloadEventDelegate + +/// Called when an ad becomes available for the configuration. +- (void)adAvailableForPreloadConfiguration:(nonnull GADPreloadConfiguration *)configuration; + +/// Called when the last available ad is exhausted for the configuration. +- (void)adsExhaustedForPreloadConfiguration:(nonnull GADPreloadConfiguration *)configuration; + +@end diff --git a/Swift/advanced/APIDemo/APIDemo/GADRewardedAd_Preview.h b/Swift/advanced/APIDemo/APIDemo/GADRewardedAd_Preview.h new file mode 100644 index 00000000..3b6c1196 --- /dev/null +++ b/Swift/advanced/APIDemo/APIDemo/GADRewardedAd_Preview.h @@ -0,0 +1,29 @@ +// +// Copyright 2024 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +#import +#import + +@interface GADRewardedAd () + +/// Returns if a rewarded ad is preloaded for the given ad unit ID. ++ (BOOL)isPreloadedAdAvailable:(nonnull NSString *)adUnitID; + +/// Returns a rewarded ad associated with the ad unit ID. Returns nil if +/// an ad is not available. ++ (nullable GADRewardedAd *)preloadedAdWithAdUnitID:(nonnull NSString *)adUnitID; + +@end diff --git a/Swift/advanced/APIDemo/APIDemo/MainViewController.swift b/Swift/advanced/APIDemo/APIDemo/MainViewController.swift index 8f8409f5..e0faed11 100644 --- a/Swift/advanced/APIDemo/APIDemo/MainViewController.swift +++ b/Swift/advanced/APIDemo/APIDemo/MainViewController.swift @@ -27,17 +27,29 @@ class MainViewController: UITableViewController { override func viewDidLoad() { super.viewDidLoad() APIDemoNames = [ - "AdMob - Ad Delegate", "AdMob - Ad Targeting", "AdMob - Banner Sizes", + "AdMob - Ad Delegate", + "AdMob - Ad Targeting", + "AdMob - Banner Sizes", "AdMob - Native Custom Mute This Ad", + "AdMob - Preload ads", "AdManager - PPID", "AdManager - Custom Targeting", "AdManager - Category Exclusions", "AdManager - Multiple Ad Sizes", "AdManager - App Events", "AdManager - Fluid Ad Size", "AdManager - Custom Video Controls", "Collapsible Banner Ad", ] identifiers = [ - "adDelegateSegue", "adTargetingSegue", "bannerSizesSegue", "customMuteSegue", - "PPIDSegue", "customTargetingSegue", "categoryExclusionsSegue", - "multipleAdSizesSegue", "appEventsSegue", "fluidAdSizeSegue", - "customControlsSegue", "collapsibleSegue", + "adDelegateSegue", + "adTargetingSegue", + "bannerSizesSegue", + "customMuteSegue", + "preloadSegue", + "PPIDSegue", + "customTargetingSegue", + "categoryExclusionsSegue", + "multipleAdSizesSegue", + "appEventsSegue", + "fluidAdSizeSegue", + "customControlsSegue", + "collapsibleSegue", ] }