-
-
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
Showing
7 changed files
with
179 additions
and
91 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
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
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
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
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
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 |
---|---|---|
@@ -1,94 +1,66 @@ | ||
// | ||
// BannerView.swift | ||
// BannerViewController.swift | ||
// Admob-SwiftUI | ||
// | ||
// Created by Wesley de Groot on 11/02/2024. | ||
// Created by Wesley de Groot on 21/08/2024. | ||
// https://wesleydegroot.nl | ||
// | ||
// Usage & Example: https://wesleydegroot.nl/blog/post/Admob-in-SwiftUI | ||
|
||
import SwiftUI | ||
import GoogleMobileAds | ||
import AppTrackingTransparency | ||
import OSLog | ||
|
||
struct BannerView: UIViewControllerRepresentable { | ||
public struct BannerView: View { | ||
@EnvironmentObject | ||
private var adHelper: AdHelper | ||
|
||
@State | ||
private var viewWidth: CGFloat = .zero | ||
|
||
@State | ||
var adUnitID: String | ||
|
||
private let bannerView = GADBannerView() | ||
|
||
func makeUIViewController(context: Context) -> some UIViewController { | ||
let bannerViewController = BannerViewController() | ||
bannerView.adUnitID = adUnitID | ||
bannerView.rootViewController = bannerViewController | ||
bannerView.delegate = context.coordinator | ||
|
||
bannerViewController.view.backgroundColor = .clear | ||
bannerViewController.view.addSubview(bannerView) | ||
bannerViewController.delegate = context.coordinator | ||
|
||
return bannerViewController | ||
} | ||
|
||
func updateUIViewController(_ uiViewController: UIViewControllerType, context: Context) { | ||
guard viewWidth != .zero else { | ||
return | ||
var adHelper: AdHelper | ||
|
||
private let logger = Logger( | ||
subsystem: "nl.wesleydegroot.Admob-SwiftUI", | ||
category: "BannerView" | ||
) | ||
|
||
public init() { } | ||
|
||
public var body: some View { | ||
ZStack { | ||
AdConsentView() | ||
.onReceive(NotificationCenter.default.publisher(for: UIApplication.didBecomeActiveNotification)) { _ in | ||
ATTrackingManager.requestTrackingAuthorization(completionHandler: { status in | ||
logStatus(status: status) | ||
}) | ||
} | ||
.environmentObject(adHelper) | ||
|
||
if adHelper.haveConsent { | ||
InternalBannerView(adUnitID: adHelper.adUnitId) | ||
.frame( | ||
width: GADAdSizeBanner.size.width, | ||
height: GADAdSizeBanner.size.height | ||
) | ||
.opacity(adHelper.showingAd ? 1 : 0) | ||
.environmentObject(adHelper) | ||
} | ||
} | ||
|
||
// Request a banner ad with the updated viewWidth. | ||
bannerView.adSize = GADCurrentOrientationAnchoredAdaptiveBannerAdSizeWithWidth(viewWidth) | ||
bannerView.load(GADRequest()) | ||
} | ||
|
||
func makeCoordinator() -> Coordinator { | ||
Coordinator(self) | ||
.frame( | ||
width: adHelper.showingAd ? nil : 1, | ||
height: adHelper.showingAd ? nil : 1 | ||
) | ||
} | ||
|
||
class Coordinator: NSObject, BannerViewControllerWidthDelegate, GADBannerViewDelegate { | ||
let parent: BannerView | ||
|
||
init(_ parent: BannerView) { | ||
self.parent = parent | ||
} | ||
|
||
// MARK: - BannerViewControllerWidthDelegate methods | ||
|
||
func bannerViewController(_ bannerViewController: BannerViewController, didUpdate width: CGFloat) { | ||
// Pass the viewWidth from Coordinator to BannerView. | ||
parent.viewWidth = width | ||
} | ||
|
||
// MARK: - GADBannerViewDelegate methods | ||
|
||
func bannerViewDidReceiveAd(_ bannerView: GADBannerView) { | ||
print("DID RECEIVE AD") | ||
parent.adHelper.showingAd = true | ||
} | ||
|
||
func bannerView(_ bannerView: GADBannerView, didFailToReceiveAdWithError error: Error) { | ||
print("DID NOT RECEIVE AD: \(error.localizedDescription)") | ||
parent.adHelper.showingAd = false | ||
} | ||
|
||
func bannerViewDidRecordImpression(_ bannerView: GADBannerView) { | ||
print("\(#function) called") | ||
} | ||
|
||
func bannerViewWillPresentScreen(_ bannerView: GADBannerView) { | ||
print("\(#function) called") | ||
} | ||
|
||
func bannerViewWillDismissScreen(_ bannerView: GADBannerView) { | ||
print("\(#function) called") | ||
} | ||
|
||
func bannerViewDidDismissScreen(_ bannerView: GADBannerView) { | ||
print("\(#function) called") | ||
func logStatus(status: ATTrackingManager.AuthorizationStatus) { | ||
switch status { | ||
case .notDetermined: | ||
logger.debug("Status: Not determined") | ||
case .authorized: | ||
logger.debug("Status: Authorized") | ||
case .denied: | ||
logger.debug("Status: Denied") | ||
case .restricted: | ||
logger.debug("Status: Restricted") | ||
@unknown default: | ||
logger.debug("Status: Unknown") | ||
} | ||
} | ||
} |
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,100 @@ | ||
// | ||
// BannerView.swift | ||
// Admob-SwiftUI | ||
// | ||
// Created by Wesley de Groot on 11/02/2024. | ||
// https://wesleydegroot.nl | ||
// | ||
// Usage & Example: https://wesleydegroot.nl/blog/post/Admob-in-SwiftUI | ||
|
||
import SwiftUI | ||
import GoogleMobileAds | ||
import OSLog | ||
|
||
struct InternalBannerView: UIViewControllerRepresentable { | ||
@EnvironmentObject | ||
private var adHelper: AdHelper | ||
|
||
@State | ||
private var viewWidth: CGFloat = .zero | ||
|
||
@State | ||
var adUnitID: String | ||
|
||
private let bannerView = GADBannerView() | ||
|
||
private let logger = Logger( | ||
subsystem: "nl.wesleydegroot.Admob-SwiftUI", | ||
category: "InternalBannerView" | ||
) | ||
|
||
func makeUIViewController(context: Context) -> some UIViewController { | ||
let bannerViewController = BannerViewController() | ||
bannerView.adUnitID = adUnitID | ||
bannerView.rootViewController = bannerViewController | ||
bannerView.delegate = context.coordinator | ||
|
||
bannerViewController.view.backgroundColor = .clear | ||
bannerViewController.view.addSubview(bannerView) | ||
bannerViewController.delegate = context.coordinator | ||
|
||
return bannerViewController | ||
} | ||
|
||
func updateUIViewController(_ uiViewController: UIViewControllerType, context: Context) { | ||
guard viewWidth != .zero else { | ||
return | ||
} | ||
|
||
// Request a banner ad with the updated viewWidth. | ||
bannerView.adSize = GADCurrentOrientationAnchoredAdaptiveBannerAdSizeWithWidth(viewWidth) | ||
bannerView.load(GADRequest()) | ||
} | ||
|
||
func makeCoordinator() -> Coordinator { | ||
Coordinator(self) | ||
} | ||
|
||
class Coordinator: NSObject, BannerViewControllerWidthDelegate, GADBannerViewDelegate { | ||
let parent: InternalBannerView | ||
|
||
init(_ parent: InternalBannerView) { | ||
self.parent = parent | ||
} | ||
|
||
// MARK: - BannerViewControllerWidthDelegate methods | ||
|
||
func bannerViewController(_ bannerViewController: BannerViewController, didUpdate width: CGFloat) { | ||
// Pass the viewWidth from Coordinator to BannerView. | ||
parent.viewWidth = width | ||
} | ||
|
||
// MARK: - GADBannerViewDelegate methods | ||
|
||
func bannerViewDidReceiveAd(_ bannerView: GADBannerView) { | ||
parent.logger.debug("DID RECEIVE AD") | ||
parent.adHelper.showingAd = true | ||
} | ||
|
||
func bannerView(_ bannerView: GADBannerView, didFailToReceiveAdWithError error: Error) { | ||
parent.logger.error("DID NOT RECEIVE AD: \(error.localizedDescription)") | ||
parent.adHelper.showingAd = false | ||
} | ||
|
||
func bannerViewDidRecordImpression(_ bannerView: GADBannerView) { | ||
parent.logger.debug("\(#function) called") | ||
} | ||
|
||
func bannerViewWillPresentScreen(_ bannerView: GADBannerView) { | ||
parent.logger.debug("\(#function) called") | ||
} | ||
|
||
func bannerViewWillDismissScreen(_ bannerView: GADBannerView) { | ||
parent.logger.debug("\(#function) called") | ||
} | ||
|
||
func bannerViewDidDismissScreen(_ bannerView: GADBannerView) { | ||
parent.logger.debug("\(#function) called") | ||
} | ||
} | ||
} |