Skip to content

Commit

Permalink
Internal change
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 699162284
  • Loading branch information
Justin Malandruccolo authored and copybara-github committed Nov 22, 2024
1 parent 043de5a commit 9776dd3
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,26 +52,35 @@ - (void)gatherConsentFromConsentPresentationViewController:(UIViewController *)v
// debugSettings.geography = UMPDebugGeographyEEA;
parameters.debugSettings = debugSettings;

// [START gather_consent]
// [START request_consent_info_update]
// Requesting an update to consent information should be called on every app launch.
[UMPConsentInformation.sharedInstance
requestConsentInfoUpdateWithParameters:parameters
completionHandler:^(NSError *_Nullable requestConsentError) {
// [START_EXCLUDE]
if (requestConsentError) {
consentGatheringComplete(requestConsentError);
} else {
[UMPConsentForm
loadAndPresentIfRequiredFromViewController:viewController
completionHandler:^(
NSError
*_Nullable loadAndPresentError) {
// Consent has been gathered.
consentGatheringComplete(
loadAndPresentError);
}];
[self loadAndPresentIfRequiredFromViewController:viewController
completionHandler:consentGatheringComplete];
}
// [END_EXCLUDE]
}];
// [END gather_consent]
// [END request_consent_info_update]
}

- (void)loadAndPresentIfRequiredFromViewController:(UIViewController *)viewController
completionHandler:(void (^)(NSError *_Nullable))completionHandler {
// [START load_and_present_consent_form]
[UMPConsentForm
loadAndPresentIfRequiredFromViewController:viewController
completionHandler:^(NSError *_Nullable loadAndPresentError) {
// Consent gathering process is complete.
// [START_EXCLUDE silent]
completionHandler(loadAndPresentError);
// [END_EXCLUDE]
}];
// [END load_and_present_consent_form]
}

- (void)presentPrivacyOptionsFormFromViewController:(UIViewController *)viewController
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ - (void)viewDidLoad {
self.bannerView.delegate = self;

__weak __typeof__(self) weakSelf = self;
// [START can_request_ads]
[GoogleMobileAdsConsentManager.sharedInstance
gatherConsentFromConsentPresentationViewController:self
consentGatheringComplete:^(NSError *_Nullable consentError) {
Expand All @@ -102,21 +101,16 @@ - (void)viewDidLoad {
if (GoogleMobileAdsConsentManager.sharedInstance.canRequestAds) {
[strongSelf startGoogleMobileAdsSDK];
}
// [START_EXCLUDE]

// [START add_privacy_options]
strongSelf.privacySettingsButton.enabled =
GoogleMobileAdsConsentManager.sharedInstance
.isPrivacyOptionsRequired;
// [END add_privacy_options]
// [END_EXCLUDE]
}];

// This sample attempts to load ads using consent obtained in the previous session.
if (GoogleMobileAdsConsentManager.sharedInstance.canRequestAds) {
[self startGoogleMobileAdsSDK];
}
// [END can_request_ads]
}

- (void)viewDidAppear:(BOOL)animated {
Expand All @@ -139,7 +133,6 @@ - (void)viewWillTransitionToSize:(CGSize)size
completion:nil];
}

// [START request_ads]
- (void)startGoogleMobileAdsSDK {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
Expand All @@ -148,7 +141,6 @@ - (void)startGoogleMobileAdsSDK {
[self loadBannerAd];
});
}
// [END request_ads]

- (void)loadBannerAd {
// Here safe area is taken into account, hence the view frame is used after the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,9 @@ class GoogleMobileAdsConsentManager: NSObject {
return UMPConsentInformation.sharedInstance.canRequestAds
}

// [START is_privacy_options_required]
var isPrivacyOptionsRequired: Bool {
return UMPConsentInformation.sharedInstance.privacyOptionsRequirementStatus == .required
}
// [END is_privacy_options_required]

/// Helper method to call the UMP SDK methods to request consent information and load/present a
/// consent form if necessary.
Expand All @@ -49,25 +47,29 @@ class GoogleMobileAdsConsentManager: NSObject {
// debugSettings.geography = UMPDebugGeography.EEA
parameters.debugSettings = debugSettings

// [START gather_consent]
// [START request_consent_info_update]
// Requesting an update to consent information should be called on every app launch.
UMPConsentInformation.sharedInstance.requestConsentInfoUpdate(with: parameters) {
requestConsentError in
// [START_EXCLUDE]
guard requestConsentError == nil else {
return consentGatheringComplete(requestConsentError)
}

Task { @MainActor in
do {
// [START load_and_present_consent_form]
try await UMPConsentForm.loadAndPresentIfRequired(from: viewController)
// [END load_and_present_consent_form]
// Consent has been gathered.
consentGatheringComplete(nil)
} catch {
consentGatheringComplete(error)
}
}
// [END_EXCLUDE]
}
// [END gather_consent]
// [END request_consent_info_update]
}

/// Helper method to call the UMP SDK method to present the privacy options form.
Expand Down
8 changes: 0 additions & 8 deletions Swift/admob/BannerExample/BannerExample/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ class ViewController: UIViewController, GADBannerViewDelegate {
bannerView.rootViewController = self
bannerView.delegate = self

// [START can_request_ads]
GoogleMobileAdsConsentManager.shared.gatherConsent(from: self) { [weak self] consentError in
guard let self else { return }

Expand All @@ -46,20 +45,15 @@ class ViewController: UIViewController, GADBannerViewDelegate {
if GoogleMobileAdsConsentManager.shared.canRequestAds {
self.startGoogleMobileAdsSDK()
}
// [START_EXCLUDE]

// [START add_privacy_options]
self.privacySettingsButton.isEnabled =
GoogleMobileAdsConsentManager.shared.isPrivacyOptionsRequired
// [END add_privacy_options]
// [END_EXCLUDE]
}

// This sample attempts to load ads using consent obtained in the previous session.
if GoogleMobileAdsConsentManager.shared.canRequestAds {
startGoogleMobileAdsSDK()
}
// [END can_request_ads]
}

override func viewDidAppear(_ animated: Bool) {
Expand Down Expand Up @@ -114,7 +108,6 @@ class ViewController: UIViewController, GADBannerViewDelegate {
}
}

// [START request_ads]
private func startGoogleMobileAdsSDK() {
DispatchQueue.main.async {
guard !self.isMobileAdsStartCalled else { return }
Expand All @@ -129,7 +122,6 @@ class ViewController: UIViewController, GADBannerViewDelegate {
}
}
}
// [END request_ads]

func loadBannerAd() {
let viewWidth = view.frame.inset(by: view.safeAreaInsets).width
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,25 +46,29 @@ class GoogleMobileAdsConsentManager: NSObject {
// debugSettings.geography = UMPDebugGeography.EEA
parameters.debugSettings = debugSettings

// [START gather_consent]
// [START request_consent_info_update]
// Requesting an update to consent information should be called on every app launch.
UMPConsentInformation.sharedInstance.requestConsentInfoUpdate(with: parameters) {
requestConsentError in
// [START_EXCLUDE]
guard requestConsentError == nil else {
return consentGatheringComplete(requestConsentError)
}

Task { @MainActor in
do {
// [START load_and_present_consent_form]
try await UMPConsentForm.loadAndPresentIfRequired(from: nil)
// [END load_and_present_consent_form]
// Consent has been gathered.
consentGatheringComplete(nil)
} catch {
consentGatheringComplete(error)
}
}
// [END_EXCLUDE]
}
// [END gather_consent]
// [END request_consent_info_update]
}

/// Helper method to call the UMP SDK method to present the privacy options form.
Expand All @@ -74,7 +78,6 @@ class GoogleMobileAdsConsentManager: NSObject {
// [END present_privacy_options_form]
}

// [START request_ads]
/// Method to initialize the Google Mobile Ads SDK. The SDK should only be initialized once.
func startGoogleMobileAdsSDK() {
guard canRequestAds, !isMobileAdsStartCalled else { return }
Expand All @@ -84,5 +87,4 @@ class GoogleMobileAdsConsentManager: NSObject {
// Initialize the Google Mobile Ads SDK.
GADMobileAds.sharedInstance().start()
}
// [END request_ads]
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ struct MenuView: View {
}
}
.onAppear {
// [START can_request_ads]
GoogleMobileAdsConsentManager.shared.gatherConsent { consentError in
if let consentError {
// Consent gathering failed.
Expand All @@ -53,17 +52,14 @@ struct MenuView: View {
// Check if you can request ads in `startGoogleMobileAdsSDK` before initializing the
// Google Mobile Ads SDK.
GoogleMobileAdsConsentManager.shared.startGoogleMobileAdsSDK()
// [START_EXCLUDE]
// Update the state of the menu items and privacy options button.
isMenuItemDisabled = !GoogleMobileAdsConsentManager.shared.canRequestAds
isPrivacyOptionsButtonDisabled = !GoogleMobileAdsConsentManager.shared
.isPrivacyOptionsRequired
// [END_EXCLUDE]
}

// This sample attempts to load ads using consent obtained in the previous session.
GoogleMobileAdsConsentManager.shared.startGoogleMobileAdsSDK()
// [END can_request_ads]
}
}
}
Expand Down

0 comments on commit 9776dd3

Please sign in to comment.