UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SwiftUI And Google AdMob Setup - Interstitial, Banner, Etc.

Forums > SwiftUI

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.

3      

UIViewControllerRepresentable has protocol requirements you have to implement that I don't see here in your code snippet.

Where are your makeUIViewController and updateUIViewController methods?

I don't know anything about Google AdMob so that's pretty much the limit off my ability to help, but have you looked at what others have done?

Here are two I found through a quick search. The second one is a little old but might still be useful.

Good luck!

3      

Hacking with Swift is sponsored by Superwall

SPONSORED Superwall lets you build & test paywalls without shipping updates. Run experiments, offer sales, segment users, update locked features and more at the click of button. Best part? It's FREE for up to 250 conversions / mo and the Superwall team builds out 100% custom paywalls – free of charge.

Learn More

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.