Skip to content

Instantly share code, notes, and snippets.

@kishan-vadoliya
Created December 26, 2023 10:15
Show Gist options
  • Save kishan-vadoliya/0e0a45f84de4fc1b270d0a677fca8867 to your computer and use it in GitHub Desktop.
Save kishan-vadoliya/0e0a45f84de4fc1b270d0a677fca8867 to your computer and use it in GitHub Desktop.
A google AdMob Banner implementation in SwiftUI
import SwiftUI
import GoogleMobileAds
import UIKit
final private class BannerVC: UIViewControllerRepresentable {
func makeUIViewController(context: Context) -> UIViewController {
let view = GADBannerView(adSize: kGADAdSizeBanner)
let viewController = UIViewController()
view.adUnitID = bannerID
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) {}
}
struct Banner:View{
var body: some View{
HStack{
Spacer()
BannerVC().frame(width: 320, height: 50, alignment: .center)
Spacer()
}
}
}
struct Banner_Previews: PreviewProvider {
static var previews: some View {
Banner()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment