Monetizing Your App Using AdMob: A Beginner’s Guide for SwiftUI Apps

Mustafa Mohammed
7 min readApr 15, 2023

As a developer, you’ve spent countless hours designing, coding, and testing your app. Now it’s time to make money from your hard work. One of the most popular ways to monetize your app is through advertising, and AdMob by Google is a leading mobile advertising platform that offers developers the opportunity to earn money by displaying ads in their apps. In this article, we’ll explore how you can monetize your app using AdMob and some tips to help you get started.

What is AdMob?

AdMob is a mobile advertising platform that was acquired by Google in 2010. It allows developers to display ads in their mobile apps and earn money from clicks and impressions. AdMob offers a variety of ad formats, including banners, interstitials, and rewarded ads, and supports both iOS and Android platforms.

Getting Started with AdMob

To get started with AdMob, you’ll need to create an AdMob account and integrate the AdMob SDK into your app. Here are the basic steps to follow:

  1. Create an AdMob account: Go to the AdMob website and sign up for an account. Once you’re signed in, you can create a new ad unit for your app.
  2. Integrate the AdMob SDK: The AdMob SDK is a set of libraries and tools that allows your app to display ads. You can integrate the SDK into your app using CocoaPods or manually by downloading the SDK and adding it to your Xcode project.
  3. Create an ad unit: An ad unit is a specific location in your app where you want to display ads. You can create multiple ad units for your app, depending on the ad formats you want to use and the locations where you want to display them.
  4. Configure your ad unit: Once you’ve created an ad unit, you can configure the ad format, size, and style. AdMob offers several options for ad formats, including banners, interstitials, and rewarded ads. You can choose the ad size and style that best fits your app’s design.
  5. Display ads in your app: Once you’ve configured your ad unit, you can display ads in your app using the AdMob SDK. AdMob provides APIs for loading and displaying ads, and you can customize the ad display to match your app’s design.

Tips for Maximizing Ad Revenue

Here are some tips to help you maximize your ad revenue with AdMob:

  1. Choose the right ad format: AdMob offers several ad formats, and you should choose the format that best fits your app’s design and user experience. For example, if your app is a game, you may want to use interstitials or rewarded ads, while if your app is a news app, you may want to use banners or native ads.
  2. Optimize ad placement: The placement of your ad unit can have a big impact on your ad revenue. You should experiment with different ad placements to see which ones perform best. For example, you may find that placing a banner ad at the bottom of your screen works better than placing it at the top.
  3. Use mediation: AdMob offers mediation, which allows you to display ads from multiple ad networks in your app. This can help increase your ad revenue by giving you access to a larger pool of advertisers.
  4. Test different ad styles: AdMob offers several ad styles, including text ads, image ads, and native ads. You should experiment with different ad styles to see which ones perform best in your app.
  5. Monitor your ad performance: AdMob provides detailed reports on your ad performance, including impressions, clicks, and revenue. You should monitor these reports regularly to see how your ads are performing and make adjustments as needed.
import SwiftUI

struct MainView: View {
var body: some View {
Spacer()
Text("Adaptive Banner")
.font(.largeTitle)
.fontWeight(.heavy)
BannerAdView(adUnit: .mainView, adFormat: .adaptiveBanner)
Text("Large Banner")
.font(.largeTitle)
.fontWeight(.heavy)
BannerAdView(adUnit: .mainView, adFormat: .largeBanner)
Text("Medium Rectangle")
.font(.largeTitle)
.fontWeight(.heavy)
BannerAdView(adUnit: .mainView, adFormat: .mediumRectangle)
Spacer()
}
}
import Foundation
import SwiftUI
import GoogleMobileAds

// Define a SwiftUI view that displays a banner ad using Google AdMob
struct BannerAdView: View {
let adUnit: AdUnit // The ad unit to be used
let adFormat: AdFormat // The ad format to be used
@State var adStatus: AdStatus = .loading // The status of the ad (loading, success, or failure)
@State var showAd: Bool = true // Whether to show the ad or not

// Define the body of the view
var body: some View {
HStack {
if showAd {
// Reserve the ad space while the ad is loading
if adStatus != .failure {
ZStack {
// Use a BannerViewController to load and display the ad
BannerViewController(adUnitID: adUnit.unitID, adSize: adFormat.adSize, adStatus: $adStatus)
.frame(width: adFormat.size.width, height: adFormat.size.height)
.zIndex(1)
Text("Ad is loading...")
.zIndex(0)
}
}
}
}
.frame(maxWidth: .infinity)
}
}

// Define a UIViewControllerRepresentable that wraps a GADBannerView and handles the loading and displaying of the ad
struct BannerViewController: UIViewControllerRepresentable {
let adUnitID: String // The ad unit ID to be used
let adSize: GADAdSize // The ad size to be used
@Binding var adStatus: AdStatus // The status of the ad (loading, success, or failure)

// Define a Coordinator class that handles the delegate methods of the GADBannerView
class Coordinator: NSObject, GADBannerViewDelegate {
var bannerViewController: BannerViewController

init(bannerViewController: BannerViewController) {
self.bannerViewController = bannerViewController
}

// Called when the ad fails to load
func bannerView(_ bannerView: GADBannerView, didFailToReceiveAdWithError error: Error) {
bannerViewController.adStatus = .failure
}

// Called when the ad is successfully loaded
func bannerViewDidReceiveAd(_ bannerView: GADBannerView) {
bannerViewController.adStatus = .success
}
}

// Create a Coordinator instance
func makeCoordinator() -> Coordinator {
Coordinator(bannerViewController: self)
}

// Create a UIViewController instance
func makeUIViewController(context: Context) -> UIViewController {
let viewController = UIViewController()

// Set the test device identifiers (you should add your own test device ID)
GADMobileAds.sharedInstance().requestConfiguration.testDeviceIdentifiers = [GADSimulatorID]

// Create a GADBannerView instance
let view = GADBannerView(adSize: self.adSize)
view.delegate = context.coordinator
view.adUnitID = self.adUnitID
view.rootViewController = viewController
view.load(GADRequest())

// Add the GADBannerView to the view controller's view
viewController.view.addSubview(view)
viewController.view.frame = CGRect(origin: .zero, size: self.adSize.size)

return viewController
}

// Update the UIViewController instance
func updateUIViewController(_ uiViewController: UIViewController, context: Context) {}
}

// Define an enum that specifies the status of the ad (loading, success, or failure)
enum AdStatus {
case loading
case success
case failure
}
// An enumeration of different types of ad units
enum AdUnit {
// Ad unit for the main view
case mainView
// Ad unit for a sub view
case subView
// Ad unit for another view
case anotherView

// Returns the ad unit ID for the corresponding AdUnit case
var unitID: String {
switch self {
// here you can add several ad units
// Replace with your ad unit ID here
case .mainView: return "ca-app-pub-3940256099942544/2934735716"
// Replace with your ad unit ID here
case .subView: return "ca-app-pub-3940256099942544/2934735716"
// Replace with your ad unit ID here
case .anotherView: return "ca-app-pub-3940256099942544/2934735716"
}
}
}

// An enumeration of different types of ad formats
enum AdFormat {
// Large banner ad format
case largeBanner
// Medium rectangle ad format
case mediumRectangle
// Adaptive banner ad format
case adaptiveBanner

// Returns the ad size for the corresponding AdFormat case
var adSize: GADAdSize {
switch self {
// Large banner ad size
case .largeBanner: return GADAdSizeLargeBanner
// Medium rectangle ad size
case .mediumRectangle: return GADAdSizeMediumRectangle
// Adaptive banner ad size that fits the screen width
case .adaptiveBanner:
return GADCurrentOrientationAnchoredAdaptiveBannerAdSizeWithWidth(
UIScreen.main.bounds.size.width)
}
}

// Returns the size of the ad format
var size: CGSize {
adSize.size
}
}

This is a Swift code that defines a BannerAdView struct and a BannerViewController struct. The BannerAdView is a SwiftUI view that displays a banner ad using Google AdMob. The BannerViewController is a UIViewControllerRepresentable that wraps a GADBannerView and handles the loading and displaying of the ad.

The BannerAdView takes in two parameters, adUnit and adFormat, which specify the ad unit and ad format to be used. It also defines two @State variables, adStatus and showAd, which are used to keep track of the status of the ad (loading, success, or failure) and whether to show the ad or not.

Inside the body of the BannerAdView, a HStack is used to display the ad, with the ZStack used to reserve the ad space while the ad is loading. A BannerViewController is used to load and display the ad inside the ZStack.

The BannerViewController takes in three parameters, adUnitID, adSize, and adStatus, which specify the ad unit ID, ad size, and ad status to be used. It also defines a Coordinator class that handles the delegate methods of the GADBannerView, which are called when the ad fails to load or when the ad is successfully loaded.

The AdStatus enum defines three cases, loading, success, and failure, which are used to keep track of the status of the ad.

The AdUnit enum defines three cases, mainView, subView, and anotherView, which specify the ad unit IDs to be used for each view.

The AdFormat enum defines three cases, largeBanner, mediumRectangle, and adaptiveBanner, which specify the ad sizes to be used for each format. The adSize computed property returns the GADAdSize for each format, and the size computed property returns the CGSize for each format.

To add a CocoaPods library to your Xcode project, you can follow these steps:

  1. Open Terminal on your Mac.
  2. Navigate to the root directory of your Xcode project using the cd command.
  3. Install CocoaPods on your machine if you haven’t already by running the following command:
sudo gem install cocoapods
  1. Once installed, initialize a Podfile for your project by running the following command in your project directory:
pod init
  1. Open the Podfile using a text editor of your choice and add the pod name(s) and version(s) you want to use, for example:
target 'YourProjectName' do
use_frameworks!
pod 'Google-Mobile-Ads-SDK'
end
  1. Save and close the Podfile, and then run the following command in Terminal to install the pods
pod install
  1. Once the installation is complete, open the .xcworkspace file generated by CocoaPods instead of the .xcodeproj file, and you should see the added library listed under the "Pods" project in Xcode's project navigator.

That’s it! You can now import the library in your Swift code and use its features.

--

--