-
Notifications
You must be signed in to change notification settings - Fork 810
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added swift preloading sample to APIDemo.
PiperOrigin-RevId: 689449591
- Loading branch information
1 parent
f25aa31
commit 8ad04e7
Showing
14 changed files
with
577 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="23089" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES"> | ||
<device id="retina6_12" orientation="portrait" appearance="light"/> | ||
<dependencies> | ||
<deployment identifier="iOS"/> | ||
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="23077"/> | ||
<capability name="System colors in document resources" minToolsVersion="11.0"/> | ||
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/> | ||
</dependencies> | ||
<objects> | ||
<placeholder placeholderIdentifier="IBFilesOwner" id="-1" userLabel="File's Owner"/> | ||
<placeholder placeholderIdentifier="IBFirstResponder" id="-2" customClass="UIResponder"/> | ||
<view verifyAmbiguity="ignoreSizes" contentMode="top" verticalCompressionResistancePriority="100" insetsLayoutMarginsFromSafeArea="NO" translatesAutoresizingMaskIntoConstraints="NO" id="iN0-l3-epB" customClass="AdmobPreloadView" customModule="APIDemo" customModuleProvider="target"> | ||
<rect key="frame" x="0.0" y="0.0" width="393" height="852"/> | ||
<subviews> | ||
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" fixedFrame="YES" text="{Status}" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="YNv-nJ-SSa"> | ||
<rect key="frame" x="10" y="24" width="229" height="20"/> | ||
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/> | ||
<fontDescription key="fontDescription" type="system" pointSize="14"/> | ||
<nil key="textColor"/> | ||
<nil key="highlightedColor"/> | ||
</label> | ||
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" fixedFrame="YES" text="{Title}" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="uUp-HP-rTJ"> | ||
<rect key="frame" x="10" y="0.0" width="229" height="25"/> | ||
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/> | ||
<fontDescription key="fontDescription" type="system" pointSize="20"/> | ||
<nil key="textColor"/> | ||
<nil key="highlightedColor"/> | ||
</label> | ||
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="system" lineBreakMode="middleTruncation" id="VIV-5R-Bjd"> | ||
<rect key="frame" x="250" y="0.0" width="113" height="44"/> | ||
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/> | ||
<state key="normal" title="Button"/> | ||
<buttonConfiguration key="configuration" style="filled" title="Show Ad"/> | ||
<connections> | ||
<action selector="show:" destination="iN0-l3-epB" eventType="touchUpInside" id="vSe-Bl-Ut7"/> | ||
</connections> | ||
</button> | ||
</subviews> | ||
<color key="backgroundColor" systemColor="systemBackgroundColor"/> | ||
<constraints> | ||
<constraint firstAttribute="height" constant="50" id="G9L-bE-Q0n"/> | ||
<constraint firstAttribute="width" relation="greaterThanOrEqual" constant="393" id="tIb-Wo-lHu"/> | ||
</constraints> | ||
<connections> | ||
<outlet property="showButton" destination="VIV-5R-Bjd" id="xL8-Eq-gKO"/> | ||
<outlet property="statusText" destination="YNv-nJ-SSa" id="dh2-hj-qwD"/> | ||
<outlet property="titleText" destination="uUp-HP-rTJ" id="po3-qS-sJz"/> | ||
</connections> | ||
<point key="canvasLocation" x="-104.58015267175573" y="-295.77464788732397"/> | ||
</view> | ||
</objects> | ||
<resources> | ||
<systemColor name="systemBackgroundColor"> | ||
<color white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/> | ||
</systemColor> | ||
</resources> | ||
</document> |
193 changes: 193 additions & 0 deletions
193
Swift/advanced/APIDemo/APIDemo/AdmobPreloadViewController.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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.") | ||
} | ||
} |
Oops, something went wrong.