Add the Google ads dependency
Learn how to support monetization with the OpenWeb iOS SDK by adding the Google Mobile Ads SDK.
When the Google Mobile Ads SDK is integrated into your app, this sets the foundation for you to display ads and to earn revenue.
Since conflicts can occur when the Google Mobile Ads SDK is a dependency of both the host app and the OpenWeb iOS SDK, Google-Mobile-Ads-SDK is not included as a direct dependency of the OpenWeb iOS SDK.
Add and initiate the dependency
Use the following steps to support monetization with the OpenWeb iOS SDK:
- In a text editor, open Podfile.
- Add
pod 'Google-Mobile-Ads-SDK', '8.0'
.
target 'OpenWeb-SDK-iOS-Demo' do
# Use one of those options
use_frameworks!
# use_frameworks! :linkage => :static
# Pods for OpenWeb-SDK-iOS-Demo
pod 'SpotIMCore'
pod 'Google-Mobile-Ads-SDK', '8.0'
end
-
In Terminal at the terminal prompt of your project directory, execute
pod install
to install the dependency. -
In the app Info.plist, add a
GADApplicationIdentifier
key and aSKAdNetworkItems
key:- Set the string value of the
GADApplicationIdentifier
key to your Ad Manager app ID. - Set the string value of the
SKAdNetworkItems
key tocstr6suwn9.skadnetwork
.
- Set the string value of the
<key>GADApplicationIdentifier</key>
<string>ca-app-pub-***************~**********</string>
<key>SKAdNetworkItems</key>
<array>
<dict>
<key>SKAdNetworkIdentifier</key>
<string>cstr6suwn9.skadnetwork</string>
</dict>
</array>
- Add the GoogleAdsProvider.swift file to your project.
import UIKit
import GoogleMobileAds
import SpotImCore
final class GoogleAdsProvider: NSObject, AdsProvider {
weak var bannerDelegate: AdsProviderBannerDelegate?
weak var interstitialDelegate: AdsProviderInterstitialDelegate?
private var banner: GAMBannerView?
private var interstitial: GAMInterstitialAd?
private var spotId: String = ""
override init() {
super.init()
}
func version() -> String { return "1.0" }
func setSpotId(spotId: String) {
self.spotId = spotId
}
func setupAdsBanner(with adId: String = Configuration.testBannerID, in controller: UIViewController, validSizes: Set<AdSize>) {
var sizes: [NSValue] = validSizes.map { (size) -> NSValue in
let gadSize = parseAdSizeToGADAdSize(adSize: size)
return NSValueFromGADAdSize(gadSize)
}
if sizes.isEmpty {
let defaultSize = parseAdSizeToGADAdSize(adSize: .small)
sizes.append(NSValueFromGADAdSize(defaultSize))
}
banner = GAMBannerView()
banner?.validAdSizes = sizes
banner?.adUnitID = adId
banner?.delegate = self
banner?.rootViewController = controller
let req = GAMRequest()
req.customTargeting = ["bannerConvSdkSpotId":spotId]
banner?.load(req)
}
func setupInterstitial(with adId: String = Configuration.testInterstitialID) {
let req = GAMRequest()
req.customTargeting = ["interConvSdkSpotId":spotId]
GAMInterstitialAd.load(withAdManagerAdUnitID: adId, request: req) { ad, error in
if let error = error {
self.interstitialDelegate?.interstitialFailedToLoad(error: error)
return
}
self.interstitial = ad
self.interstitial?.fullScreenContentDelegate = self
self.interstitialDelegate?.interstitialLoaded()
}
}
func showInterstitial(in controller: UIViewController) -> Bool {
guard let interstitial = interstitial else { return false }
interstitial.present(fromRootViewController: controller)
return true
}
private func parseAdSizeToGADAdSize(adSize: AdSize) -> GADAdSize {
switch adSize {
case .small:
return kGADAdSizeBanner
case .medium:
return kGADAdSizeLargeBanner
case .large:
return kGADAdSizeMediumRectangle
}
}
@objc public class func setSpotImSDKWithProvider() {
SpotIm.setGoogleAdsProvider(googleAdsProvider: GoogleAdsProvider())
}
}
extension GoogleAdsProvider: GADFullScreenContentDelegate {
func adDidPresentFullScreenContent(_ ad: GADFullScreenPresentingAd) {
interstitialDelegate?.interstitialWillBeShown()
}
func ad(_ ad: GADFullScreenPresentingAd, didFailToPresentFullScreenContentWithError error: Error) {
interstitialDelegate?.interstitialDidDismiss()
}
func adDidDismissFullScreenContent(_ ad: GADFullScreenPresentingAd) {
interstitialDelegate?.interstitialDidDismiss()
}
}
extension GoogleAdsProvider: GADBannerViewDelegate {
func bannerViewDidReceiveAd(_ bannerView: GADBannerView) {
bannerDelegate?.bannerLoaded(bannerView: bannerView, adBannerSize: bannerView.adSize.size, adUnitID: bannerView.adUnitID ?? "")
}
func bannerView(_ bannerView: GADBannerView, didFailToReceiveAdWithError error: Error) {
bannerDelegate?.bannerFailedToLoad(error: error)
}
}
private extension GoogleAdsProvider {
private enum Configuration {
static let testInterstitialID: String = "/6499/example/interstitial"
static let testBannerID: String = "/6499/example/banner"
}
}
- Immediately after
SpotIm.initialize(spotId: spotId)
, callsetGoogleAdsProvider
to set the Google ads provider to theSpotIm
class.
SpotIm.setGoogleAdsProvider(googleAdsProvider: GoogleAdsProvider())
Updated 5 months ago