Skip to content

Commit

Permalink
[ADD] BKGoogleAdView (#150)
Browse files Browse the repository at this point in the history
  • Loading branch information
kimkyuchul committed Dec 1, 2024
1 parent 576bcfb commit 2d895e5
Showing 1 changed file with 63 additions and 0 deletions.
63 changes: 63 additions & 0 deletions Projects/Feature/Scene/Common/BKGoogleAdView.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
//
// BKGoogleAdView.swift
// Feature
//
// Created by kyuchul on 11/30/24.
// Copyright © 2024 com.kyuchul.blink. All rights reserved.
//

import SwiftUI

import Services

import GoogleMobileAds

struct BKGoogleAdView: UIViewControllerRepresentable {
@Binding private var isPresented: Bool
@Binding private var interstitialAd: GoogleAd?
private let dismissAdScreen: () -> Void
private let viewController: UIViewController

init(isPresented: Binding<Bool>,
interstitialAd: Binding<GoogleAd?>,
dismissAdScreen: @escaping () -> Void
) {
self._isPresented = isPresented
self._interstitialAd = interstitialAd
self.dismissAdScreen = dismissAdScreen
self.viewController = UIViewController()
}

func makeUIViewController(context: Context) -> UIViewController {
DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(1)) {
if let interstitialAd {
interstitialAd.ad.present(fromRootViewController: viewController)
}
}

return viewController
}

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

func makeCoordinator() -> Coordinator {
Coordinator(parent: self)
}
}

extension BKGoogleAdView {
final class Coordinator: NSObject, GADFullScreenContentDelegate {
private let parent: BKGoogleAdView

init(parent: BKGoogleAdView) {
self.parent = parent
super.init()
parent.interstitialAd?.ad.fullScreenContentDelegate = self
}

func adDidDismissFullScreenContent(_ ad: GADFullScreenPresentingAd) {
parent.isPresented.toggle()
parent.dismissAdScreen()
}
}
}

0 comments on commit 2d895e5

Please sign in to comment.