Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
//
// Copyright (C) 2025 Google, Inc.
//
// 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/Foundation.h>
#import <GoogleMobileAds/GoogleMobileAds.h>

// Replace with your own placement ID.
static long adPlacementID = 2500718471;
static NSString *const adUnitID = @"ca-app-pub-3940256099942544/4411468910";

@interface AdPlacementsSnippets : UIViewController

@end

@interface AdPlacementsSnippets ()
- (void)loadInterstitial;
- (void)showBannerView:(GADBannerView *)bannerView;
- (void)showAd:(GADInterstitialAd *)ad;
- (void)configureView:(GADNativeAdView *)nativeAdView withNativeAd:(GADNativeAd *)nativeAd;
@end

@implementation AdPlacementsSnippets

// [START load_interstitial]
- (void)loadInterstitial {
GADRequest *request = [GADRequest request];
request.placementID = adPlacementID;
[GADInterstitialAd loadWithAdUnitID:adUnitID
request:request
completionHandler:^(GADInterstitialAd *ad, NSError *error) {
if (!error) {
NSLog(@"Placement ID: %lld", ad.placementID);
return;
}
}];
}
// [END load_interstitial]

// [START show_banner]
- (void)showBannerView:(GADBannerView *)bannerView {
bannerView.placementID = adPlacementID;
[self.view addSubview:bannerView];
}
// [END show_banner]

// [START show_interstitial]
- (void)showAd:(GADInterstitialAd *)ad {
ad.placementID = adPlacementID;
[ad presentFromRootViewController:self];
}
// [END show_interstitial]

// [START show_native]
- (void)configureView:(GADNativeAdView *)nativeAdView withNativeAd:(GADNativeAd *)nativeAd {
nativeAd.placementID = adPlacementID;
nativeAdView.nativeAd = nativeAd;
}
// [END show_native]

@end
58 changes: 58 additions & 0 deletions Swift/advanced/APIDemo/APIDemo/Snippets/AdPlacementsSnippets.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
//
// Copyright (C) 2025 Google, Inc.
//
// 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

private class AdPlacementsSnippets: UIViewController {

// Replace with your own placement ID.
private let adPlacementID: Int64 = 2500718471
private let adUnitID = "ca-app-pub-3940256099942544/6978759866"

// [START load_interstitial]
private func loadInterstitial() async {
do {
let interstitial = try await InterstitialAd.load(
with: adUnitID, request: Request())
interstitial.placementID = adPlacementID
print("Placement ID set to: \(interstitial.placementID)")
} catch {
print("Failed to load interstitial ad with error: \(error.localizedDescription)")
}
}
// [END load_interstitial]

// [START show_banner]
private func showBanner(_ bannerView: BannerView) {
bannerView.placementID = adPlacementID
view.addSubview(bannerView)
}
// [END show_banner]

// [START show_interstitial]
private func showInterstitial(_ ad: InterstitialAd) {
ad.placementID = adPlacementID
ad.present(from: self)
}
// [END show_interstitial]

// [START show_native]
private func configureView(_ nativeAdView: NativeAdView, withAd nativeAd: NativeAd) {
nativeAd.placementID = adPlacementID
nativeAdView.nativeAd = nativeAd
}
// [END show_native]
}
Loading