-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
576bcfb
commit 2d895e5
Showing
1 changed file
with
63 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
} | ||
} |