如何在 Google AdMob RewardAd 显示完成后保持 Bool 状态?
How to keep a Bool state after Google AdMob RewardAd has finished displaying?
我目前正在使用 SwiftUI 开发应用程序。
我正在尝试使用 google AdMob reward Ad
。
我编写了代码来显示参考 this article 的奖励广告。
我试图在完全使用来自 RewardedAdDelegate
class 的 Bool state
观看奖励广告后显示 alert
,但它不起作用。 ..
我该如何解决这个问题?
代码如下:
AdShow.swift
import SwiftUI
struct AdShow: View {
@ObservedObject var adDelegate = RewardedAdDelegate()
var body: some View {
RewardedAd()
.alert(isPresented: $adDelegate.adFullyWatched){
Alert(title: Text("reward Ad finished"),
message: Text("reward Ad finished"),
dismissButton: .default(Text("OK")))
}
}
}
RewardedAd.swift
import SwiftUI
import GoogleMobileAds
struct RewardedAd: View {
@EnvironmentObject var appState: AppState
@ObservedObject var adDelegate = RewardedAdDelegate()
var body: some View {
if adDelegate.adLoaded && !adDelegate.adFullyWatched {
let root = UIApplication.shared.windows.first?.rootViewController
self.adDelegate.rewardedAd!.present(fromRootViewController: root!, delegate: adDelegate)
}
return Text("Load ad").onTapGesture {
self.adDelegate.loadAd()
}
}
}
RewardedAdDelegate
import Foundation
import GoogleMobileAds
class RewardedAdDelegate: NSObject, GADRewardedAdDelegate, ObservableObject {
@Published var adLoaded: Bool = false
@Published var adFullyWatched: Bool = false
var rewardedAd: GADRewardedAd? = nil
func loadAd() {
rewardedAd = GADRewardedAd(adUnitID: "ca-app-pub-3940256099942544/1712485313")
rewardedAd!.load(GADRequest()) { error in
if error != nil {
self.adLoaded = false
} else {
self.adLoaded = true
}
}
}
/// Tells the delegate that the user earned a reward.
func rewardedAd(_ rewardedAd: GADRewardedAd, userDidEarn reward: GADAdReward) {
adFullyWatched = true
print("Reward received with currency: \(reward.type), amount \(reward.amount).")
}
/// Tells the delegate that the rewarded ad was presented.
func rewardedAdDidPresent(_ rewardedAd: GADRewardedAd) {
self.adLoaded = false
print("Rewarded ad presented.")
}
/// Tells the delegate that the rewarded ad was dismissed.
func rewardedAdDidDismiss(_ rewardedAd: GADRewardedAd) {
print("Rewarded ad dismissed.")
}
/// Tells the delegate that the rewarded ad failed to present.
func rewardedAd(_ rewardedAd: GADRewardedAd, didFailToPresentWithError error: Error) {
print("Rewarded ad failed to present.")
}
}
已更新
AdShow.swift
import SwiftUI
struct AdShow: View {
@ObservedObject var adDelegate = RewardedAdDelegate()
var body: some View {
VStack{
RewardedAd(adDelegate: self.adDelegate)
.alert(isPresented: $adDelegate.adFullyWatched){
Alert(title: Text(""),
message: Text(""),
dismissButton: .default(Text("OK")))
}
// check adDelegate.adFullyWatched state -> after Ad finish this text shows
if adDelegate.adFullyWatched{
Text("Checked")
}
}
}
}
RewardedAd.swift
import SwiftUI
import GoogleMobileAds
struct RewardedAd: View {
@ObservedObject var adDelegate : RewardedAdDelegate
var body: some View {
if adDelegate.adLoaded && !adDelegate.adFullyWatched {
let root = UIApplication.shared.windows.first?.rootViewController
self.adDelegate.rewardedAd!.present(fromRootViewController: root!, delegate: adDelegate)
}
return Text("Load ad").onTapGesture {
self.adDelegate.loadAd()
}
}
}
重新更新
AdShow.swift
import SwiftUI
struct AdShow: View {
@ObservedObject var adDelegate = RewardedAdDelegate()
@State var isAlert = false
var body: some View {
VStack{
Button(action: {
self.isAlert = true
}){
Text("alert")
.padding()
}
RewardedAd(adDelegate: self.adDelegate)
.alert(isPresented: self.$isAlert){
Alert(title: Text(""),
message: Text(""),
dismissButton: .default(Text("OK")))
}
// check adDelegate.adFullyWatched state -> after Ad finish this text shows
if adDelegate.adFullyWatched{
Text("Checked")
}
}
.onAppear(){
if adDelegate.adFullyWatched{
self.isAlert = true
}
}
}
}
Xcode: 版本 12.0.1
iOS: 13.0
您在视图中使用不同的委托实例,而必须将委托从第一个视图注入到第二个视图中。
struct AdShow: View {
@ObservedObject var adDelegate = RewardedAdDelegate()
var body: some View {
RewardedAd(adDelegate: self.adDelegate) // << here inject !!
// ... other code
和
struct RewardedAd: View {
@EnvironmentObject var appState: AppState
@ObservedObject var adDelegate: RewardedAdDelegate // << here declare !!
// ... other code
我目前正在使用 SwiftUI 开发应用程序。
我正在尝试使用 google AdMob reward Ad
。
我编写了代码来显示参考 this article 的奖励广告。
我试图在完全使用来自 RewardedAdDelegate
class 的 Bool state
观看奖励广告后显示 alert
,但它不起作用。 ..
我该如何解决这个问题?
代码如下:
AdShow.swift
import SwiftUI
struct AdShow: View {
@ObservedObject var adDelegate = RewardedAdDelegate()
var body: some View {
RewardedAd()
.alert(isPresented: $adDelegate.adFullyWatched){
Alert(title: Text("reward Ad finished"),
message: Text("reward Ad finished"),
dismissButton: .default(Text("OK")))
}
}
}
RewardedAd.swift
import SwiftUI
import GoogleMobileAds
struct RewardedAd: View {
@EnvironmentObject var appState: AppState
@ObservedObject var adDelegate = RewardedAdDelegate()
var body: some View {
if adDelegate.adLoaded && !adDelegate.adFullyWatched {
let root = UIApplication.shared.windows.first?.rootViewController
self.adDelegate.rewardedAd!.present(fromRootViewController: root!, delegate: adDelegate)
}
return Text("Load ad").onTapGesture {
self.adDelegate.loadAd()
}
}
}
RewardedAdDelegate
import Foundation
import GoogleMobileAds
class RewardedAdDelegate: NSObject, GADRewardedAdDelegate, ObservableObject {
@Published var adLoaded: Bool = false
@Published var adFullyWatched: Bool = false
var rewardedAd: GADRewardedAd? = nil
func loadAd() {
rewardedAd = GADRewardedAd(adUnitID: "ca-app-pub-3940256099942544/1712485313")
rewardedAd!.load(GADRequest()) { error in
if error != nil {
self.adLoaded = false
} else {
self.adLoaded = true
}
}
}
/// Tells the delegate that the user earned a reward.
func rewardedAd(_ rewardedAd: GADRewardedAd, userDidEarn reward: GADAdReward) {
adFullyWatched = true
print("Reward received with currency: \(reward.type), amount \(reward.amount).")
}
/// Tells the delegate that the rewarded ad was presented.
func rewardedAdDidPresent(_ rewardedAd: GADRewardedAd) {
self.adLoaded = false
print("Rewarded ad presented.")
}
/// Tells the delegate that the rewarded ad was dismissed.
func rewardedAdDidDismiss(_ rewardedAd: GADRewardedAd) {
print("Rewarded ad dismissed.")
}
/// Tells the delegate that the rewarded ad failed to present.
func rewardedAd(_ rewardedAd: GADRewardedAd, didFailToPresentWithError error: Error) {
print("Rewarded ad failed to present.")
}
}
已更新
AdShow.swift
import SwiftUI
struct AdShow: View {
@ObservedObject var adDelegate = RewardedAdDelegate()
var body: some View {
VStack{
RewardedAd(adDelegate: self.adDelegate)
.alert(isPresented: $adDelegate.adFullyWatched){
Alert(title: Text(""),
message: Text(""),
dismissButton: .default(Text("OK")))
}
// check adDelegate.adFullyWatched state -> after Ad finish this text shows
if adDelegate.adFullyWatched{
Text("Checked")
}
}
}
}
RewardedAd.swift
import SwiftUI
import GoogleMobileAds
struct RewardedAd: View {
@ObservedObject var adDelegate : RewardedAdDelegate
var body: some View {
if adDelegate.adLoaded && !adDelegate.adFullyWatched {
let root = UIApplication.shared.windows.first?.rootViewController
self.adDelegate.rewardedAd!.present(fromRootViewController: root!, delegate: adDelegate)
}
return Text("Load ad").onTapGesture {
self.adDelegate.loadAd()
}
}
}
重新更新
AdShow.swift
import SwiftUI
struct AdShow: View {
@ObservedObject var adDelegate = RewardedAdDelegate()
@State var isAlert = false
var body: some View {
VStack{
Button(action: {
self.isAlert = true
}){
Text("alert")
.padding()
}
RewardedAd(adDelegate: self.adDelegate)
.alert(isPresented: self.$isAlert){
Alert(title: Text(""),
message: Text(""),
dismissButton: .default(Text("OK")))
}
// check adDelegate.adFullyWatched state -> after Ad finish this text shows
if adDelegate.adFullyWatched{
Text("Checked")
}
}
.onAppear(){
if adDelegate.adFullyWatched{
self.isAlert = true
}
}
}
}
Xcode: 版本 12.0.1
iOS: 13.0
您在视图中使用不同的委托实例,而必须将委托从第一个视图注入到第二个视图中。
struct AdShow: View {
@ObservedObject var adDelegate = RewardedAdDelegate()
var body: some View {
RewardedAd(adDelegate: self.adDelegate) // << here inject !!
// ... other code
和
struct RewardedAd: View {
@EnvironmentObject var appState: AppState
@ObservedObject var adDelegate: RewardedAdDelegate // << here declare !!
// ... other code