SwiftUIAdmobPro is a Swift package designed to integrate Google AdMob seamlessly with SwiftUI, making it feel as native as any other SwiftUI view or component. Unlike other libraries, SwiftUIAdmobPro provides a clean, SwiftUI-first API that makes managing AdMob banners and interstitial ads simple and efficient.
- Pure SwiftUI support for AdMob integration.
- Declarative modifiers for handling Ad events.
- Adaptive banner sizes and interstitial ads.
- Simple initialization and configuration.
- Easily extensible with callback support.
- Option to choose between personalized and non-personalized ads.
- In Xcode, go to File > Add Packages.
- Enter the repository URL:
https://github.com/X901/SwiftUIAdmobPro. - Select the branch
mainto install the library. - Add it to your desired targets.
Note: The AdMob SDK is already included in
SwiftUIAdmobPro. You do not need to add it separately.
To ensure proper ad attribution and privacy compliance, you need to configure your App Identifier and SKAdNetworkIdentifier in the Info.plist file.
Follow the detailed steps provided in Google's official guide:
AdMob iOS Privacy Strategies
Important: Adding SKAdNetworkIdentifiers is required for accurate ad attribution and reporting on iOS devices.
- Add the following key to your
Info.plistfile:
<key>NSUserTrackingUsageDescription</key>
<string>This identifier will be used to deliver personalized ads to you.</string>- Configure AdMob UI:
- Add a GDPR message.
- Add an IDFA explainer.
Configure AdMob Privacy Messaging
- Initialize AdMob with Personalized Ads
import SwiftUIAdmobPro
@main
struct MyApp: App {
init() {
Task {
try await AdMobInitializer.initialize(adPreference: .personalized)
}
}
var body: some Scene {
WindowGroup {
ContentView()
}
}
}Note: Everything is handled automatically by
AdMobInitializer.initialize(adPreference: .personalized)including UMP SDK andATTrackingManagersetup.
- No additional configuration is required.
Example:
import SwiftUIAdmobPro
@main
struct MyApp: App {
init() {
Task {
try await AdMobInitializer.initialize(adPreference: .nonPersonalized)
}
}
var body: some Scene {
WindowGroup {
ContentView()
}
}
}Add a banner ad to your SwiftUI view:
import SwiftUIAdmobPro
struct ContentView: View {
var body: some View {
VStack {
Text("Welcome to SwiftUIAdmobPro")
.font(.headline)
BannerView(adUnitID: "ca-app-pub-3940256099942544/2435281174")
.backgroundColor(.black)
.onAdLoaded {
print("Banner Ad Loaded")
}
.onAdFailedToLoad { error in
print("Failed to load ad: \(error)")
}
.onAdClicked {
print("Ad Clicked")
}
.onAdClosed {
print("Ad Closed")
}
.frame(height: 50)
}
}
}Note: Replace the test ad unit ID with your actual Banner Ad Unit ID.
.onAdLoadedTriggered when the banner ad successfully loads..onAdFailedToLoadTriggered when the banner ad fails to load..onAdClickedTriggered when the ad is clicked..onAdClosedTriggered when the ad is closed by the user
Note: All event modifiers are optional.
To show interstitial ads, the InterstitialAdManager must be available in the SwiftUI environment. You achieve this by applying the .interstitialAd modifier to a parent view. This modifier sets up the InterstitialAdManager, which child views can access via the @Environment.
Here's an example of where to apply the .interstitialAd modifier and how to access it in child views:
import SwiftUI
import SwiftUIAdmobPro
@main
struct MyApp: App {
init() {
AdMobInitializer.initialize()
}
var body: some Scene {
WindowGroup {
ContentView()
.interstitialAd(adUnitID: "ca-app-pub-3940256099942544/4411468910")
}
}
}Note: Replace test ads with your Interstitial adUnitID
To show an interstitial ad from within your SwiftUI view, use the @Environment property wrapper to retrieve the InterstitialAdManager. Then, call the showAd() method on the manager to present the ad.
Here’s an example of how to display interstitial ads:
struct ContentView: View {
@Environment(\.interstitialAdManager) var adManager
var body: some View {
VStack {
Text("SwiftUIAdmobPro")
VStack(spacing: 20) {
Button(action: {
adManager?.showAd()
}) {
Text("Tap Me")
.padding()
.background(Color.blue)
.foregroundColor(.white)
.cornerRadius(10)
}
}
}
.onInterstitialAdLoaded { print("Interstitial loaded") }
.onInterstitialAdFailedToLoad { error in print("Interstitial failed: \(error)") }
.onInterstitialAdPresented { print("Interstitial presented") }
.onInterstitialAdFailedToPresent { error in print("Presentation failed: \(error)") }
.onInterstitialAdDismissed { print("Interstitial dismissed") }
}
}Note: Environment Access: Use @Environment(.interstitialAdManager) to retrieve the InterstitialAdManager in child views. This allows you to present ads by calling showAd() on the manager.
onInterstitialAdLoadedTriggered when the interstitial ad successfully loads.onInterstitialAdFailedToLoad: Handles errors when loading the interstitial ad.onInterstitialAdPresentedTriggered when the interstitial ad is displayed.onInterstitialAdFailedToPresentHandles errors when presenting the interstitial ad.onInterstitialAdDismissedTriggered when the interstitial ad is dismissed.
Note: All event modifiers are optional.