[英]Main view reloading while showing intersitital ad in swiftui
I want to design a swiftui view MainView() where an interstitial ad will be presented without reloading MainView().我想设计一个 swiftui 视图 MainView(),其中将在不重新加载 MainView() 的情况下显示插页式广告。
My strategy is:我的策略是:
But every time while the ad presented, the MainView() goes to.onDisappear state, and after closing the ad, it goes to.onAppear state. For that reason the MainView() is fully reloaded.但是每次在展示广告时,MainView() 都会转到.onDisappear state,而在关闭广告后,它会转到.onAppear state。因此,MainView() 会完全重新加载。 I want to stop this reloading issue.
我想停止这个重新加载问题。 How should I actually present the ad in my view?
我应该如何在我的视图中实际展示广告?
Update: Here I have added AdView for more clarification.更新:我在此处添加了 AdView 以获得更多说明。
struct nterstitialAdView: UIViewControllerRepresentable {
var interstitialAd: GADInterstitialAd?
var callback: () -> Void
func makeUIViewController(context: Context) -> UIViewController {
let view = UIViewController()
DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(1)) {
self.showAd(from: view, callback: callback)
}
return view
}
func updateUIViewController(_ uiViewController: UIViewController, context: Context) {
}
func showAd(from root: UIViewController, callback: () -> Void) {
if let ad = interstitialAd {
ad.present(fromRootViewController: root)
callback()
print(":: Ad Presented")
} else {
print(":: Ad not ready")
callback()
}
}
}
Rather than present using fullScreenCover
, how about displaying the ViewController in a ZStack
, for example:与其使用
fullScreenCover
显示,不如在ZStack
中显示 ViewController,例如:
struct ContentView: View {
@State private var showAd = false
var body: some View {
ZStack {
VStack {
Text("Main View")
}
.task {
// Show AdView after a delay
try? await Task.sleep(nanoseconds: 2_000_000_000)
showAd = true
}
.onAppear {
print("Appearing") // This is only called once
}
if showAd {
AdView {
showAd = false
}
.edgesIgnoringSafeArea(.all)
}
}
}
}
// Just an example view for testing
struct AdView: UIViewControllerRepresentable {
let dismiss: () -> Void
func makeUIViewController(context: Context) -> UIViewController {
let vc = UIViewController()
vc.view.backgroundColor = .red
let button = UIButton(type: .custom, primaryAction: UIAction(handler: { _ in
dismiss()
}))
button.setTitle("Dismiss", for: .normal)
button.translatesAutoresizingMaskIntoConstraints = false
vc.view.addSubview(button)
vc.view.centerXAnchor.constraint(equalTo: button.centerXAnchor).isActive = true
vc.view.centerYAnchor.constraint(equalTo: button.centerYAnchor).isActive = true
return vc
}
func updateUIViewController(_ uiViewController: UIViewController, context: Context) {
}
}
It should be pretty straightforward to animate the AdView in, if that's what's required.如果需要的话,为 AdView 设置动画应该非常简单。
You can track in a Bool
whether your page has already loaded and not reload it if it did.您可以在
Bool
中跟踪您的页面是否已经加载,如果加载了则不重新加载。
Other than that The iOS is made to execute OnAppear
in that case, that happens both in UIKit
and SwiftUI
, so even calling UIKit
in help won't work.除此之外 iOS 在这种情况下执行
OnAppear
,这发生在UIKit
和SwiftUI
中,因此即使在帮助中调用UIKit
也不起作用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.