This document outlines the process of integrating the Google Ad Manager iOS SDK in your native app using Swift. The official Google Mobile Ads SDK integration guide for iOS is available here.
|
Important: Ensure your project meets the following requirements: Xcode 15.3 or higher. iOS 12.0 or higher. |
1. Configure the SDK via CocoaPods
Add the SDK dependency to your project using CocoaPods.
Podfile: Ensure you are using the latest version of the Google Mobile Ads SDK.
Ruby
target 'MyApplication' do
use_frameworks!
# Add the Google Mobile Ads SDK dependency
pod 'Google-Mobile-Ads-SDK'
# If you are planning to use other ad sources, add them here.
# pod 'GoogleMobileAdsMediationFyber'
# pod 'GoogleMobileAdsMediationInMobi'
# pod 'GoogleMobileAdsMediationFacebook'
end In a terminal, run:
pod install --repo-updateMore information on additional ad sources can be found here.
If you are using SPM (Swift Package Manager), then follow the steps below.
https://developers.google.com/ad-manager/mobile-ads-sdk/ios/quick-start
To add a package dependency to your project, follow these steps:
- In Xcode, install the Google Mobile sdk Swift Package by navigating to File > Add Package Dependencies....
-
In the prompt that appears, search for the Google Mobile Ads Swift Package GitHub repository:
https://github.com/googleads/swift-package-manager-google-mobile-ads.git
Select the version of the Google Mobile Ads Swift Package you want to use. For new projects, we recommend using the Up to Next Major Version.
2. Update Info.plist
Add your Ad Manager app ID, as identified in the Ad Manager web interface, to your app's Info.plist file.
XML snippet for Info.plist:
<key>GADApplicationIdentifier</key>
<string>your_APP_ID</string>
<key>GADIsAdManagerApp</key>
<true/>- Sample ID: ca-app-pub-3940256099942544~3347511713
Update the SKAdnetwork key-value pair in info.plist for Google and its partners.
https://developers.google.com/ad-manager/mobile-ads-sdk/ios/quick-start#expandable-1
3. SDK Initialization
Before loading ads, initialize the SDK. This should be done only once, ideally at app launch in the AppDelegate.
If using mediation, wait until the completion handler is called to ensure all adapters are ready.
The link for iOS sample apps with Google Ad Manager integration can be found here.
Swift Implementation:
import GoogleMobileAds
@main
class AppDelegate: UIResponder, UIApplicationDelegate {
func application(_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Initialize the Google Mobile Ads SDK.
GADMobileAds.sharedInstance().start { status in
// Optional: Check status of individual mediation adapters
}
return true
}
}4. Setup Banner Ads
Official documentation: https://developers.google.com/ad-manager/mobile-ads-sdk/ios/banner
Implementation Example: Create a GAMBannerView and add it to your view hierarchy.
// Initialize the banner view.
bannerView = AdManagerBannerView()
bannerView.delegate = self
bannerView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(bannerView)
// This example doesn't give width or height constraints, as the ad size gives the banner an
// intrinsic content size to size the view.
NSLayoutConstraint.activate([
// Align the banner's bottom edge with the safe area's bottom edge
bannerView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor),
// Center the banner horizontally in the view
bannerView.centerXAnchor.constraint(equalTo: view.centerXAnchor),
])Once the GAMBannerView is in place and its properties, such as adUnitID, are configured, it's time to load an ad. This is done by calling loadRequest: on a GAMRequest object:
func loadBannerAd(bannerView: AdManagerBannerView) {
// Request a large anchored adaptive banner with a width of 375.
bannerView.adSize = largeAnchoredAdaptiveBanner(width: 375)
bannerView.load(AdManagerRequest())
}If you configured your ad unit to refresh, you don't need to request another ad when the ad fails to load. Google Mobile Ads SDK respects any refresh rate you specified in the Ad Manager UI. If you haven't enabled refresh, issue a new request. For more details on ad unit refresh, such as setting a refresh rate, see Refresh rate for ads in mobile apps.
When your app's screen orientation changes, such as from portrait mode to landscape, the available width for the banner often changes as well. To make sure you display an appropriately sized ad for the new layout, request a new banner. If your banner width is static, or if your layout constraints can handle the resize, skip this step.
override func viewWillTransition(
to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator
) {
coordinator.animate(alongsideTransition: { _ in
// Load a new ad for the new orientation.
})
}5. Ad Lifecycle Events
To register for banner ad events, set the delegate property on GAMBannerView to an object that implements the GADBannerViewDelegate protocol. Generally, the class that implements banner ads also acts as the delegate class, in which case, the delegate property can be set to self.
Swift Implementation:
bannerView.delegate = selfEach of the methods in GADBannerViewDelegate is marked as optional, so you only need to implement the methods you want. This example implements each method and logs a message to the console:
func bannerViewDidReceiveAd(_ bannerView: BannerView) {
print("Banner ad loaded.")
}
func bannerView(_ bannerView: BannerView, didFailToReceiveAdWithError error: Error) {
print("Banner ad failed to load: \(error.localizedDescription)")
}
func bannerViewDidRecordImpression(_ bannerView: BannerView) {
print("Banner ad recorded an impression.")
}
func bannerViewDidRecordClick(_ bannerView: BannerView) {
print("Banner ad recorded a click.")
}
func bannerViewWillPresentScreen(_ bannerView: BannerView) {
print("Banner ad will present screen.")
}
func bannerViewWillDismissScreen(_ bannerView: BannerView) {
print("Banner ad will dismiss screen.")
}
func bannerViewDidDismissScreen(_ bannerView: BannerView) {
print("Banner ad did dismiss screen.")
}Here are some example use cases for these ad event methods.
Add a banner to the view hierarchy once an ad is received
You may want to delay in adding a GAMBannerView to the view hierarchy until after an ad is received. You can do this by listening for the bannerViewDidReceiveAd: event:
func bannerViewDidReceiveAd(_ bannerView: BannerView) {
// Add banner to view and add constraints.
addBannerViewToView(bannerView)
}Collapsible banners
Collapsible banner ads are banner ads that are initially presented as a larger overlay, with a button to collapse the ad to a smaller size. Consider using it to further optimize your performance. See collapsible banner ads for more details.
Inline adaptive banners
Inline adaptive banners are larger, taller banners compared to anchored adaptive banners. They are of variable height, and can be as tall as the device screen. Inline adaptive banners are recommended over anchored adaptive banner ads for apps that place banner ads in scrollable content. See inline adaptive banners for more details.
|
IMPORTANT: If you run into any issues during integration or require any further assistance, Please don’t hesitate to contact the Freestar support team for assistance. |
Once completed, share a TestFlight build or your implementation files for verification.