Integrate Google Ads in SwiftUI
Monetizing your SwiftUI app with Google Ads (AdMob) can be a great way to generate revenue. But integrating ads into a SwiftUI app is a bit tricky because AdMob’s SDK is primarily built for UIKit.
In this tutorial, I’ll show you step-by-step how to seamlessly integrate banner ads and interstitial ads into your SwiftUI project!
Let’s dive in ๐
๐ Prerequisites
Before we begin, make sure you have:
- A Google AdMob account
- Created an app in AdMob console
- Your AdMob App ID and Ad Unit IDs ready
- Xcode installed (Version 15+ recommended)
- A basic SwiftUI app setup
๐ ️ Step 1: Install the Google Mobile Ads SDK
First, install the Google-Mobile-Ads-SDK using Swift Package Manager:
- Open your Xcode project.
- Go to File > Add Packages.
- Search for:
https://github.com/googleads/swift-package-manager-google-mobile-ads
- Select the latest version and add it to your app.
✅ Tip: No need to mess with Cocoapods or manual frameworks anymore!
๐ ️ Step 2: Configure AdMob in Your App
Add your AdMob App ID to your app’s Info.plist
:
<key>GADApplicationIdentifier</key>
<string>ca-app-pub-xxxxxxxxxxxxxxxx~yyyyyyyyyy</string>
Also, if your app targets iOS 14+, add this for tracking permission:
<key>NSUserTrackingUsageDescription</key>
<string>We use your data to deliver better ads.</string>
๐ ️ Step 3: Initialize Google Mobile Ads SDK
In your App
file (the entry point of SwiftUI apps), initialize the SDK.
import SwiftUI
import GoogleMobileAds
@main
struct YourAppNameApp: App {
init() {
GADMobileAds.sharedInstance().start(completionHandler: nil)
}
var body: some Scene {
WindowGroup {
ContentView()
}
}
}
๐บ Step 4: Create a Banner Ad View for SwiftUI
Since AdMob’s GADBannerView
is a UIKit view, we’ll wrap it in a SwiftUI UIViewRepresentable
.
import SwiftUI
import GoogleMobileAds
struct BannerAdView: UIViewRepresentable {
let adUnitID: String
func makeUIView(context: Context) -> GADBannerView {
let banner = GADBannerView(adSize: GADAdSizeBanner)
banner.adUnitID = adUnitID
banner.rootViewController = UIApplication.shared.windows.first?.rootViewController
banner.load(GADRequest())
return banner
}
func updateUIView(_ uiView: GADBannerView, context: Context) {}
}
✅ Now you can simply use BannerAdView
inside your SwiftUI screens!
Example:
struct ContentView: View {
var body: some View {
VStack {
Text("Hello, SwiftUI + Ads!")
.font(.largeTitle)
.padding()
Spacer()
BannerAdView(adUnitID: "ca-app-pub-xxxxxxxxxxxxxxxx/zzzzzzzzzz")
.frame(width: 320, height: 50)
}
}
}
๐ Step 5: Load and Show Interstitial Ads
Interstitials are full-screen ads shown at natural break points.
First, create a SwiftUI-friendly interstitial handler:
import GoogleMobileAds
class InterstitialAd: NSObject, GADFullScreenContentDelegate, ObservableObject {
private var interstitial: GADInterstitialAd?
func loadAd() {
let request = GADRequest()
GADInterstitialAd.load(withAdUnitID: "ca-app-pub-xxxxxxxxxxxxxxxx/yyyyyyyyyy", request: request) { [self] ad, error in
if let error = error {
print("Failed to load interstitial ad: \(error.localizedDescription)")
return
}
interstitial = ad
interstitial?.fullScreenContentDelegate = self
}
}
func showAd(from rootViewController: UIViewController) {
if let ad = interstitial {
ad.present(fromRootViewController: rootViewController)
} else {
print("Ad wasn't ready")
}
}
}
Now use it inside your SwiftUI view:
struct ContentView: View {
@StateObject private var interstitial = InterstitialAd()
var body: some View {
VStack(spacing: 20) {
Button("Load Interstitial") {
interstitial.loadAd()
}
Button("Show Interstitial") {
if let root = UIApplication.shared.windows.first?.rootViewController {
interstitial.showAd(from: root)
}
}
}
.padding()
}
}
๐จ Bonus Tip: Make the Ads Feel Native
To avoid annoying users:
- Show interstitials after key actions, not randomly.
- Place banners in logical places, like bottom of the screen.
- Respect AdMob policies to avoid app bans.
๐งน Step 6: Testing with Test Ad Units
Never use real ads while developing.
Use Google’s test ad unit IDs:
- Banner Test ID:
ca-app-pub-3940256099942544/2934735716
- Interstitial Test ID:
ca-app-pub-3940256099942544/4411468910
Example:
BannerAdView(adUnitID: "ca-app-pub-3940256099942544/2934735716")
๐ฏ Final Thoughts
Integrating Google Ads into a SwiftUI app is very achievable by bridging UIKit components into SwiftUI.
Once you wrap your banners and interstitials properly, you can easily plug them into any SwiftUI view hierarchy.
Now you’re ready to start monetizing your app like a pro ๐.
If you found this helpful, please drop a ๐ฌ comment and let’s connect!
Happy coding ✨
Comments
Post a Comment