Skip to content

Commit

Permalink
Add includecode comments for UMP SDK integration
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 686987300
  • Loading branch information
Justin Malandruccolo authored and copybara-github committed Oct 17, 2024
1 parent 6908d41 commit 69fb7c6
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,12 @@ - (BOOL)canRequestAds {
return UMPConsentInformation.sharedInstance.canRequestAds;
}

// [START is_privacy_options_required]
- (BOOL)isPrivacyOptionsRequired {
return UMPConsentInformation.sharedInstance.privacyOptionsRequirementStatus ==
UMPPrivacyOptionsRequirementStatusRequired;
}
// [END is_privacy_options_required]

- (void)gatherConsentFromConsentPresentationViewController:(UIViewController *)viewController
consentGatheringComplete:
Expand All @@ -50,6 +52,7 @@ - (void)gatherConsentFromConsentPresentationViewController:(UIViewController *)v
// debugSettings.geography = UMPDebugGeographyEEA;
parameters.debugSettings = debugSettings;

// [START gather_consent]
// Requesting an update to consent information should be called on every app launch.
[UMPConsentInformation.sharedInstance
requestConsentInfoUpdateWithParameters:parameters
Expand All @@ -68,13 +71,16 @@ - (void)gatherConsentFromConsentPresentationViewController:(UIViewController *)v
}];
}
}];
// [END gather_consent]
}

- (void)presentPrivacyOptionsFormFromViewController:(UIViewController *)viewController
completionHandler:
(void (^)(NSError *_Nullable))completionHandler {
// [START present_privacy_options_form]
[UMPConsentForm presentPrivacyOptionsFormFromViewController:viewController
completionHandler:completionHandler];
// [END present_privacy_options_form]
}

@end
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ - (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 @@ -101,16 +102,21 @@ - (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 @@ -133,6 +139,7 @@ - (void)viewWillTransitionToSize:(CGSize)size
completion:nil];
}

// [START request_ads]
- (void)startGoogleMobileAdsSDK {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
Expand All @@ -141,6 +148,7 @@ - (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,9 +30,11 @@ 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 @@ -47,6 +49,7 @@ class GoogleMobileAdsConsentManager: NSObject {
// debugSettings.geography = UMPDebugGeography.EEA
parameters.debugSettings = debugSettings

// [START gather_consent]
// Requesting an update to consent information should be called on every app launch.
UMPConsentInformation.sharedInstance.requestConsentInfoUpdate(with: parameters) {
requestConsentError in
Expand All @@ -64,12 +67,15 @@ class GoogleMobileAdsConsentManager: NSObject {
}
}
}
// [END gather_consent]
}

/// Helper method to call the UMP SDK method to present the privacy options form.
@MainActor func presentPrivacyOptionsForm(from viewController: UIViewController? = nil)
async throws
{
// [START present_privacy_options_form]
try await UMPConsentForm.presentPrivacyOptionsForm(from: viewController)
// [END present_privacy_options_form]
}
}
8 changes: 8 additions & 0 deletions Swift/admob/BannerExample/BannerExample/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ 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 @@ -45,15 +46,20 @@ 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 @@ -108,6 +114,7 @@ class ViewController: UIViewController, GADBannerViewDelegate {
}
}

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

func loadBannerAd() {
let viewWidth = view.frame.inset(by: view.safeAreaInsets).width
Expand Down

0 comments on commit 69fb7c6

Please sign in to comment.