If you have an existing Xcode project that's using CocoaPods for AdMob Ads ONLY, then feel free to run pod deintegrate command so you could clean all CocoaPods files. You can also remove the .xcworkspace file, since we will not need this anymore.
If you have an existing Xcode project that's using CocoaPods for AdMob Ads AND something else, then go ahead and update your podfile by removing the "pod 'Google-Mobile-Ads-SDK'" line and run the pod update command so you could clean AdMob related pods.
1) Choose File -> Add Packages...
In your Xcode project, when you click the File menu at the top, the selected 'Add Packages...', you will see the window that allows you to enter the Google AdMob Swift Package
2) Paste the Google AdMob Swift Package URL
On the right side, click the text field where it says "Search or Enter Package URL" and paste the following URL: https://github.com/googleads/swift-package-manager-google-mobile-ads.git
3) Set Dependency Rule
a) From the dropdown on the right, set the Dependecy Rule to Up to Next Major Version
b) Click the Add Package button on the bottom right
Now you have to wait until Swift Package Manager fetches all the necessary AdMob files.
4) Initialize the AdMob SDK
At this point you must have the AdMob package installed, and you can follow the code below to start the SDK in your AppDelegate class or 'AppName'App.swift class for SwiftUI
You have to add the line 1 and 9 in the AppDelegate's didFinishLaunchingWithOptions method
For SwiftUI apps, just open your main 'AppName'App.swift file. In this example, we have FuelTrackApp.swift
You have to add the line 2 and the init() method from line 9 to 11
5) Configure Info.plist with your AdMob App ID and Interstitial Unit ID
Before presenting any AdMob Ads, you must create your AdMob app apps.admob.com/apps/create
Once you have the app created, copy the App ID then create an Interstitial Ad Unit ID
Open your Info.plist file in Xcode and add the following keys:
GADApplicationIdentifier - set the value to your AdMob App ID that you just created, it starts with 'ca-app-pub-'
GADIsAdManagerApp - set the type to Boolean, and the value to YES
SKAdNetworkItems - set the type to Array, then add 1 item with the type of Dictionary, and inside this Dictionary create a key named 'SKAdNetworkIdentifier' with the value cstr6suwn9.skadnetwork
If you've done everything correctly, your Info.plist should look like this.
6) Present AdMob Interstitial Ad
To present an Interstitial Ad in a Swift project, it's pretty simple. You just need to create an Ad request with your Interstitial Ad Unit ID, then present it whenever you want.
You have to add the lines 1 and 6, then in copy the block of code from viewDidLoad() method
When you want to present the ad, just use 'present' method from line 26
In SwiftUI it's a little more complicated, because you have to create a separate class for Interstitial Ads that will load the Ad and allow you to present it from anywhere in the SwiftUI code. See example below:
Create a new class named 'Interstitial' and copy this entire code block.
When you want to present the ad, just call these 2 methods 'Interstitial.shared.loadInterstitial()' and 'Interstitial.shared.showInterstitialAds()'
This should be all. Of course, there are so many types of AdMob Ads and different ways to optimize this, but this is the basics on how to integrate AdMob Ads without using CocoaPods.
Q) Why do I get Swift Package Manager errors when trying to build?
A) Make sure that you wait until all Package Dependecies on the left side in Xcode are resolved, and besides each package there is a version. If there are no versions, or the packages shows a loading indicator, then you must wait until all packages are resolved.
Q) How do I update the version of AdMob package?
A) To update the package version, you can use the File -> Packages -> Update to Latest Package Versions. You can also use the Reset Package Caches, so any older versions are being cleaned up.
If you have any questions or suggestions, please feel free to send an email to email@example.com
Thanks for reading!