iOS Tutorial

Add AdMob with Swift Package Manager

Add Google AdMob to an existing Swift or SwiftUI project using Swift Package Manager — without using CocoaPods for the AdMob SDK.

SwiftSwiftUISwift Package ManagerGoogle AdMob

Before you start: CocoaPods cleanup

If your existing Xcode project uses CocoaPods only for AdMob, you can run pod deintegrate to remove the CocoaPods integration. You can also remove the .xcworkspace file if it is no longer needed.

If the project uses CocoaPods for AdMob and other dependencies, remove pod 'Google-Mobile-Ads-SDK' from your Podfile and run pod update so the other pods remain intact.

Step 1

Add a package in Xcode

Open your Xcode project, choose File → Add Packages…, and Xcode will open the package dependency window.

Step 2

Add the Google Mobile Ads package

In the package window, use the Search or Enter Package URL field and paste the Google Mobile Ads Swift Package URL:

https://github.com/googleads/swift-package-manager-google-mobile-ads.git
Step 3

Set the dependency rule

Set the dependency rule to Up to Next Major Version, then click Add Package. Wait for Swift Package Manager to finish fetching and resolving the required files before continuing.

iOS App Templates

Explore complete iOS app projects

With the AdMob package installed, the next step is applying monetization inside a real app. These complete iOS app templates can be customized with your own features, branding, and monetization strategy.

Step 4

Initialize the AdMob SDK

Once the package is installed, initialize the SDK when your app launches. For a UIKit app, add the required setup to your AppDelegate.

import GoogleMobileAds

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
    func application(_ application: UIApplication,
        didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

        GADMobileAds.sharedInstance().start(completionHandler: nil)
        return true
    }
}
View the UIKit example on GitHub Gist →

Add the AdMob import and SDK start call to didFinishLaunchingWithOptions.

For SwiftUI, open your main AppNameApp.swift file and initialize AdMob from the app entry point.

import SwiftUI
import GoogleMobileAds

@main
struct FuelTrackerApp: App {

    @StateObject private var manager: DataManager = DataManager(preview: false)

    init() {
        GADMobileAds.sharedInstance().start(completionHandler: nil)
    }

    var body: some Scene {
        WindowGroup {
            DashboardContentView()
                .environmentObject(manager)
        }
    }
}
View the SwiftUI example on GitHub Gist →
Step 5

Configure Info.plist

Before presenting ads, create your app in AdMob and obtain its App ID. Then configure the required AdMob values in your project’s Info.plist.

The original setup uses GADApplicationIdentifier for the AdMob App ID, GADIsAdManagerApp as a Boolean value set to YES, and SKAdNetworkItems with the required network identifier.

Google’s required Info.plist and SKAdNetwork configuration can change as the SDK evolves. Before shipping your app, compare this setup with the current Google Mobile Ads documentation.
Step 6

Present an interstitial ad

In a Swift/UIKit project, create an ad request using your interstitial Ad Unit ID, load the ad, and present it at the appropriate point in your app.

import GoogleMobileAds
import UIKit

class ViewController: UIViewController {

    private var interstitial: GADInterstitialAd?

    override func viewDidLoad() {
        super.viewDidLoad()

        let request = GADRequest()
        GADInterstitialAd.load(
            withAdUnitID: "ca-app-pub-3940256099942544/4411468910",
            request: request
        ) { [self] ad, error in
            if let error = error {
                print("Failed to load interstitial ad: \(error.localizedDescription)")
                return
            }
            interstitial = ad
        }
    }

    @IBAction func doSomething(_ sender: Any) {
        if let interstitial {
            interstitial.present(fromRootViewController: self)
        } else {
            print("Ad wasn't ready")
        }
    }
}
View the UIKit interstitial example on GitHub Gist →

For SwiftUI, the original tutorial uses a separate Interstitial class so the ad can be loaded and presented from SwiftUI code.

// MARK: - Google AdMob Interstitial support class
class Interstitial: NSObject, GADFullScreenContentDelegate {
    private var interstitial: GADInterstitialAd?
    static var shared: Interstitial = Interstitial()

    override init() {
        super.init()
        loadInterstitial()
    }

    func loadInterstitial() {
        let request = GADRequest()
        GADInterstitialAd.load(
            withAdUnitID: AppConfig.adMobAdId,
            request: request
        ) { [self] ad, error in
            if ad != nil { interstitial = ad }
            interstitial?.fullScreenContentDelegate = self
        }
    }

    func showInterstitialAds() {
        if interstitial != nil, let root = rootController {
            interstitial?.present(fromRootViewController: root)
        }
    }

    func adDidDismissFullScreenContent(_ ad: GADFullScreenPresentingAd) {
        loadInterstitial()
    }
}

var rootController: UIViewController? {
    var root = UIApplication.shared.windows.first?.rootViewController
    if let presenter = root?.presentedViewController {
        root = presenter
    }
    return root
}
View the SwiftUI interstitial example on GitHub Gist →

The example exposes methods for loading and presenting the interstitial ad.

You’re ready to build on it

This covers the basic Swift Package Manager integration and an interstitial-ad example. AdMob supports additional ad formats and configuration options, so you can expand the implementation based on how advertising fits into your app.

FAQ

Why do I get Swift Package Manager errors when building?

Make sure Xcode has finished resolving every package dependency. If a package still shows a loading indicator or no resolved version, wait for package resolution to complete before building again.

How do I update the AdMob package?

Use Xcode’s package update command to update package dependencies to their latest allowed versions. Resetting package caches can also help when Xcode is holding onto stale package data.

Need help building your iOS app?

Apps4World provides custom native iOS development in Swift, SwiftUI, and UIKit — from the first screen through App Store delivery.

Discuss your project