Reputation: 644
I am trying to show an Open Ad from Google Admob in my SwiftUI app. I am not too familiar with UIKit and stuff...
I am keep getting this error in my console: " Status bar could not be hidden for full screen ad. Ensure that your app is configured to allow full screen ads to control the status bar. For example, consider whether you need to set the childViewControllerForStatusBarHidden property on your ad's rootViewController."
How do I solve this?
// Extending Application to get RootView..
extension UIApplication {
func getRootViewController() -> UIViewController {
guard let scene = self.connectedScenes.first as? UIWindowScene else {
return .init()
}
guard let root = scene.windows.first?.rootViewController else {
return .init()
}
return root
}
}
final class OpenAd: NSObject, GADFullScreenContentDelegate {
var appOpenAd: GADAppOpenAd?
var loadTime = Date()
func currentDeviceOrientation() -> UIInterfaceOrientation {
let currentOrientation = UIDevice.current.orientation
switch currentOrientation {
case .unknown:
return .unknown
case .portrait:
return .portrait
case .portraitUpsideDown:
return .portraitUpsideDown
case .landscapeLeft:
return .landscapeLeft
case .landscapeRight:
return .landscapeRight
case .faceUp:
return .portrait
case .faceDown:
return .portrait
@unknown default:
return .unknown
}
}
func showAdForFirstLaunch() {
let request = GADRequest()
GADAppOpenAd.load(withAdUnitID: "ca-app-pub-3940256099942544/5662855259",
request: request,
orientation: UIInterfaceOrientation.portrait,
completionHandler: { (appOpenAdIn, _) in
self.appOpenAd = appOpenAdIn
self.appOpenAd?.fullScreenContentDelegate = self
self.loadTime = Date()
self.appOpenAd?.present(fromRootViewController: UIApplication.shared.getRootViewController())
})
}
func requestAppOpenAd() {
let request = GADRequest()
GADAppOpenAd.load(withAdUnitID: "ca-app-pub-3940256099942544/5662855259",
request: request,
orientation: UIInterfaceOrientation.portrait,
completionHandler: { (appOpenAdIn, _) in
self.appOpenAd = appOpenAdIn
self.appOpenAd?.fullScreenContentDelegate = self
self.loadTime = Date()
print("[OPEN AD] Ad is ready")
})
}
func tryToPresentAd() {
if let gOpenAd = self.appOpenAd, wasLoadTimeLessThanNHoursAgo(thresholdN: 4) {
gOpenAd.present(fromRootViewController: UIApplication.shared.getRootViewController())
} else {
self.requestAppOpenAd()
}
}
func wasLoadTimeLessThanNHoursAgo(thresholdN: Int) -> Bool {
let now = Date()
let timeIntervalBetweenNowAndLoadTime = now.timeIntervalSince(self.loadTime)
let secondsPerHour = 3600.0
let intervalInHours = timeIntervalBetweenNowAndLoadTime / secondsPerHour
return intervalInHours < Double(thresholdN)
}
func ad(_ ad: GADFullScreenPresentingAd, didFailToPresentFullScreenContentWithError error: Error) {
print("[OPEN AD] Failed: \(error)")
requestAppOpenAd()
}
func adDidDismissFullScreenContent(_ ad: GADFullScreenPresentingAd) {
requestAppOpenAd()
print("[OPEN AD] Ad dismissed")
}
}
Upvotes: 1
Views: 894
Reputation: 121
I recently had the same problem and I solved it by hiding the status bar myself while the ad is displayed. Here is an excerpt from my root view controller, which is also the GADFullScreenContentDelegate:
private var adShown = false
override var prefersStatusBarHidden: Bool {
return adShown
}
func adWillPresentFullScreenContent(_ ad: GADFullScreenPresentingAd) {
adShown = true
setNeedsStatusBarAppearanceUpdate()
}
func adDidDismissFullScreenContent(_ ad: GADFullScreenPresentingAd) {
adShown = false
setNeedsStatusBarAppearanceUpdate()
}
Note that UIViewControllerBasedStatusBarAppearance must be set to true in your info.plist for this to work.
Upvotes: -1