Skip to content

Instantly share code, notes, and snippets.

@tsuzukihashi
Last active June 14, 2023 12:41
Show Gist options
  • Save tsuzukihashi/33e520a7af365635a4de6b326942ecc9 to your computer and use it in GitHub Desktop.
Save tsuzukihashi/33e520a7af365635a4de6b326942ecc9 to your computer and use it in GitHub Desktop.
SwiftUI Admob Banner
import SwiftUI
import GoogleMobileAds
enum BannerType {
case main
var id: String {
switch self {
case .main:
return ""
}
}
}
struct BannerView: UIViewRepresentable {
private let type: BannerType
init(type: BannerType) {
self.type = type
}
func makeUIView(context: Context) -> GADBannerView {
let view = GADBannerView(adSize: GADAdSizeBanner)
#if DEBUG
// NOTE: DEBUG
view.adUnitID = "ca-app-pub-3940256099942544/2934735716"
#else
view.adUnitID = type.id
#endif
let scenes = UIApplication.shared.connectedScenes
let windowScene = scenes.first as? UIWindowScene
let window = windowScene?.windows.first
view.rootViewController = window?.rootViewController
view.load(GADRequest())
return view
}
func updateUIView(_ uiView: GADBannerView, context: Context) {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment