How to Use the Google Mobile Ads Sdk in Swiftui, or Use the Uikit Uiviewcontroller Within a Swiftui View

Google AdMob integration in SwiftUI

in the Apple SwiftUI tutorial - integration in SwiftUI

you can find that how to solve this question with UIViewControllerRepresentable

and I create an example like this

import GoogleMobileAds
import SwiftUI
import UIKit

struct GADBannerViewController: UIViewControllerRepresentable {
func makeUIViewController(context: Context) -> UIViewController {
let view = GADBannerView(adSize: kGADAdSizeBanner)
let viewController = UIViewController()
view.adUnitID = "your ad unit id in there."
view.rootViewController = viewController
viewController.view.addSubview(view)
viewController.view.frame = CGRect(origin: .zero, size: kGADAdSizeBanner.size)
view.load(GADRequest())
return viewController
}

func updateUIViewController(_ uiViewController: UIViewController, context: Context) {}
}

then you can using GADBannerViewController in your SwiftUI view's body like that

HStack {
Spacer()
GADBannerViewController()
.frame(width: kGADAdSizeBanner.size.width, height: kGADAdSizeBanner.size.height)
Spacer()
}

if you have any questions, please let me know./p>

How to keep a Bool state after Google AdMob RewardAd has finished displaying?

You use different instances of delegate in your views, instead you have to inject delegate from first view into second one.

struct AdShow: View {

@ObservedObject var adDelegate = RewardedAdDelegate()

var body: some View {

RewardedAd(adDelegate: self.adDelegate) // << here inject !!

// ... other code

and

struct RewardedAd: View {

@EnvironmentObject var appState: AppState
@ObservedObject var adDelegate: RewardedAdDelegate // << here declare !!

// ... other code

How to add code to AppDelegate using SwiftUI Life Cycle

The init might be too early, try in app delegate as follows

import GoogleMobileAds

class AppDelegate: NSObject, UIApplicationDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {

GADMobileAds.sharedInstance().start(completionHandler: nil) // << here !!
return true
}
}

@main
struct YourApp: App {

@UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate

var body: some Scene {
WindowGroup {
ContentView()
}
}
}

Admob Native Ads in SwiftUI

The answer to this was to use self.view instead of the placeholder code

    // Remove the previous ad view.
nativeAdView = adView
self.view.addSubview(nativeAdView)
nativeAdView.translatesAutoresizingMaskIntoConstraints = false

// Layout constraints for positioning the native ad view to stretch the entire width and height
// of the nativeAdPlaceholder.
let viewDictionary = ["_nativeAdView": nativeAdView!]
self.view.addConstraints(
NSLayoutConstraint.constraints(
withVisualFormat: "H:|[_nativeAdView]|",
options: NSLayoutConstraint.FormatOptions(rawValue: 0), metrics: nil, views: viewDictionary)
)
self.view.addConstraints(
NSLayoutConstraint.constraints(
withVisualFormat: "V:|[_nativeAdView]|",
options: NSLayoutConstraint.FormatOptions(rawValue: 0), metrics: nil, views: viewDictionary)
)


Related Topics



Leave a reply



Submit