-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRandom Dude Admob.swift
54 lines (45 loc) · 1.74 KB
/
Random Dude Admob.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
//
// Insterstitial.swift
//
//
// Created by Gweppi on 23/08/2021.
//
import GoogleMobileAds
class InterstitialAd: NSObject, GADFullScreenContentDelegate, ObservableObject {
private static var interstitial: GADInterstitialAd?
static func loadAd(completion: (() -> Void)? = nil) {
let request = GADRequest()
request.scene = UIApplication.shared.connectedScenes.first(where: { $0 is UIWindowScene }) as? UIWindowScene
GADInterstitialAd.load(withAdUnitID: "AdUnitID", request: request, completionHandler: { [self] ad, error in
guard error == nil else { return }
interstitial = ad
interstitial?.fullScreenContentDelegate = self
if let completion = completion {
completion
}
})
}
static func showAd() {
guard interstitial != nil else { return }
// This is how I present the add, an interstitial is a view itself.
interstitial?.present(fromRootViewController: UIApplication.keyWindow)
}
private static func adDidDismissFullScreenContent(_ ad: GADFullScreenPresentingAd) {
interstitial = nil
loadAd()
}
}
extension UIApplication {
var keyWindow: UIWindow? {
// Get connected scenes
return UIApplication.shared.connectedScenes
// Keep only active scenes, onscreen and visible to the user
.filter { $0.activationState == .foregroundActive }
// Keep only the first `UIWindowScene`
.first(where: { $0 is UIWindowScene })
// Get its associated windows
.flatMap({ $0 as? UIWindowScene })?.windows
// Finally, keep only the key window
.first(where: \.isKeyWindow)
}
}