'SwiftUI Google Interstitial admob + Webkit

So I am trying to get Interstitial ads working within my app, and I seem to be hitting a dead wall - well the error I get is that I don't have the webkit entitlement, and I can't find how to add it to my app.

import GoogleMobileAds
import SwiftUI
import UIKit
import WebKit

class InterstitialAd: NSObject {
    var interstitialAd: GADInterstitialAd?
    
    static let shared = InterstitialAd()
    
    func loadAd(withAdUnitId id: String) {
        let req = GADRequest()
        
        GADInterstitialAd.load(withAdUnitID: id, request: req) { interstitialAd, err in
            if let err = err {
                print("Failed to load ad with error: \(err.localizedDescription)")
                return
            }
            
            self.interstitialAd = interstitialAd
        }
    }
}

final class InterstitialAdView: NSObject, UIViewControllerRepresentable, GADFullScreenContentDelegate {
    
    let interstitialAd = InterstitialAd.shared.interstitialAd
    @Binding var isPresented: Bool
    var adUnitId: String
    
    init(isPresented: Binding<Bool>, adUnitId: String) {
        self._isPresented = isPresented
        self.adUnitId = adUnitId
        super.init()
        
        interstitialAd?.fullScreenContentDelegate = self
    }
    
    func makeUIViewController(context: Context) -> UIViewController {
        let view = UIViewController()
        
        DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(1)) {
            self.showAd(from: view)
        }
        
        return view
    }

    func updateUIViewController(_ uiViewController: UIViewController, context: Context) {
        
    }
    
    func showAd(from root: UIViewController) {
        
        if let ad = interstitialAd {
            ad.present(fromRootViewController: root)
        } else {
            print("Ad not ready")
            self.isPresented.toggle()
        }
    }
    
    func adDidDismissFullScreenContent(_ ad: GADFullScreenPresentingAd) {
        InterstitialAd.shared.loadAd(withAdUnitId: adUnitId)
        
        isPresented.toggle()
    }
}

however when I add the following into a SwiftUI.View I get no ad showing

struct MediaPlayerView: SwiftUI.View {
    
    private var fullScreenAd: InterstitialAd?
        init() {
            fullScreenAd = InterstitialAd()
    
        }
    

then

.onAppear{
                
                //RUN AD
                
                self.fullScreenAd?.loadAd(withAdUnitId: "ca-app-pub-XXXXXXXXXX/XXXXX")
              } 

I don't see how I am calling the view - because this clearly is not calling the right function.

sometimes I am getting the following error com.apple.runningboard.assertions.webkit But I have no idea how to add that to my entitlements



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source