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
Unknown Selected Data Source for Port Speaker (Type: Speaker)
Duplicating a Particle Emitter Effect in Sprite Kit
How to Attach an Object to Your Camera Position with Arkit Swift
"Missing Required Entitlement" for Nfctagreadersession
Using Foreach with a an Array of Bindings (Swiftui)
Pop to Root View Using Tab Bar in Swiftui
Arkit - How to Put 3D Object on Qrcode
Why Do Some Types (E.G. Float80) Have a Memory Alignment Bigger Than Word Size
Swiftui MACos Nswindow Instance
How in Swift to Convert Int16 to Two Uint8 Bytes
Conflicting Definition of Swift Struct and Array
Value for Swift_Version Cannot Be Empty
How to Fix Error: This Class Is Not Key Value Coding-Compliant for the Key Tableview.'
How to Implement a Spritekit Timer
Detecting iOS Dark Mode Change