I haven't found anything related to implementation of the AdMob SDK here so figured I'd see if anyone can help.
It seems that at the time of writing this, SwiftUI doesn't play very well with Google's AdMob SDK for adding adverts to an app. All the resources I can find require a lot of UIView hacking with UIKit and such.
Even with those approaches, I still cannot get a simple Interstitial ad working in my app. Here's Google docs realated to this:
https://developers.google.com/admob/ios/interstitial?hl=en-US
I've tried building a class to load the advert using these docs, but no mixture of UIViewControllerRepresentable or other attempts seem to work.
import GoogleMobileAds
import SwiftUI
import UIKit
class InterstitialAd: UIViewControllerRepresentable {
let withAdUnitID: String = "ca-app-pub-3940256099942544/4411468910"
var interstitialAd: GADInterstitialAd?
func loadAd() {
let req = GADRequest()
GADInterstitialAd.load(withAdUnitID: withAdUnitID, request: req) { interstitialAd, err in
if let err = err {
print("Failed to load ad with error: \(err)")
return
}
self.interstitialAd = interstitialAd
}
}
}
I get this error:
Protocol 'View' requirement '_makeView(view:inputs:)' cannot be satisfied by a non-final class ('InterstitialAd') because it uses 'Self' in a non-parameter, non-result type position
I try making it a final
class and the error says Type 'InterstitialAd' does not conform to protocol 'UIViewControllerRepresentable'
I feel like I'm missing something obvious when it comes to bridging the gap of SwiftUI and UIKit, but I'm still too new to see what's wrong.