在视频奖励广告关闭时如何显示视图?

huangapple go评论69阅读模式
英文:

How do I show a view on dismiss of a video reward ad?

问题

在此SwiftUI代码中,当点击播放按钮时,将显示一个视频奖励广告。在此视频广告被关闭后,我想显示一个视图,上面写着"你观看了广告"。

在视频奖励广告被关闭后,我想显示一个视图,向用户解释奖励,并由右上角的"x"关闭。

这是我的代码:

struct EarnRewards: View {
    
    private var rewardedAdView = RewardedAdView()
    
    fileprivate func videoAdButton(_ closure: @escaping () -> Void) -> some View {
        return Button {
            print("Tapped Video Button")
            closure()
        } label: {
            
            Image(systemName: "play.fill")
                .foregroundColor(.black)
        }
    }
    
    var body: some View {
        
        VStack {
           
            videoAdButton {
                self.rewardedAdView.showAd {
                    print("Reward the user")
                    
                    // 在这里显示一个视图,例如一个弹出框,内容为"你观看了广告"
                }
            }
        }
        
    }
}
final class RewardedAdView: NSObject, GADFullScreenContentDelegate {

    var rewardedAd: GADRewardedAd?
    var rewardFunction: (() -> Void)? = nil

    override init() {
        super.init()
        loadRewarded()
    }

    func loadRewarded(){
        let request = GADRequest()
        GADRewardedAd.load(withAdUnitID: Constants.AdMob.rewardedAdUnitId,
                                  request: request, completionHandler: { (ad, error) in
                                    if let error = error {
                                      print("Rewarded ad failed to load with error: \(error.localizedDescription)")
                                      return
                                    }
                                    self.rewardedAd = ad
                                    self.rewardedAd?.fullScreenContentDelegate = self
                                  })
    }

    func showAd(_ rewardFunction: @escaping () -> Void){
        if let ad = rewardedAd,
           let root = UIApplication.shared.windows.first?.rootViewController {
              ad.present(fromRootViewController: root,
                       userDidEarnRewardHandler: {
                            let reward = ad.adReward
                            print(reward)
                            rewardFunction()
                       })
          } else {
            print("Ad wasn't ready")
          }
    }

    func rewardedAd(_ rewardedAd: GADRewardedAd, userDidEarn reward: GADAdReward) {
        if let rewardFunction = rewardFunction {
            rewardFunction()
        }
    }

    /// Tells the delegate that the ad failed to present full screen content.
    func ad(_ ad: GADFullScreenPresentingAd, didFailToPresentFullScreenContentWithError error: Error) {
      print("Ad did fail to present full screen content.")
    }

    /// Tells the delegate that the ad dismissed full screen content.
    func adDidDismissFullScreenContent(_ ad: GADFullScreenPresentingAd) {
      print("Ad did dismiss full screen content.")
      
      // 在这里触发显示你想要的视图,例如解释奖励的视图
    }
}

你可以在adDidDismissFullScreenContent方法中触发显示你想要的视图,例如一个解释奖励的视图。

英文:

In this SwiftUI code, when the play button is tapped, a video reward ad is shown. After this video ad has been dismissed, I want to show a view saying ("you watched an ad").

The view I want to show after the video reward ad has been dismissed will explain the rewards to the user and will be dismissed by an "x" in the top right.

Here is my code:

struct EarnRewards: View {
    
    private var rewardedAdView = RewardedAdView()
    
    fileprivate func videoAdButton(_ closure: @escaping () -> Void) -> some View {
        return Button {
            print("Tapped Video Button")
            closure()
        } label: {
            
            Image(systemName: "play.fill")
                .foregroundColor(.black)
        }
    }
    
    var body: some View {
        
        VStack {
           
            videoAdButton {
                self.rewardedAdView.showAd {
                    print("Reward the user")
                }
            }
        }
        
    }
}
final class RewardedAdView: NSObject, GADFullScreenContentDelegate {

    var rewardedAd: GADRewardedAd?
    var rewardFunction: (() -> Void)? = nil

    override init() {
        super.init()
        loadRewarded()
    }

    func loadRewarded(){
        let request = GADRequest()
        GADRewardedAd.load(withAdUnitID: Constants.AdMob.rewardedAdUnitId,
                                  request: request, completionHandler: { (ad, error) in
                                    if let error = error {
                                      print("Rewarded ad failed to load with error: \(error.localizedDescription)")
                                      return
                                    }
                                    self.rewardedAd = ad
                                    self.rewardedAd?.fullScreenContentDelegate = self
                                  })
    }

    func showAd(_ rewardFunction: @escaping () -> Void){
        if let ad = rewardedAd,
           let root = UIApplication.shared.windows.first?.rootViewController {
              ad.present(fromRootViewController: root,
                       userDidEarnRewardHandler: {
                            let reward = ad.adReward
                            print(reward)
                            rewardFunction()
                       })
          } else {
            print("Ad wasn't ready")
          }
    }

    func rewardedAd(_ rewardedAd: GADRewardedAd, userDidEarn reward: GADAdReward) {
        if let rewardFunction = rewardFunction {
            rewardFunction()
        }
    }

    /// Tells the delegate that the ad failed to present full screen content.
    func ad(_ ad: GADFullScreenPresentingAd, didFailToPresentFullScreenContentWithError error: Error) {
      print("Ad did fail to present full screen content.")
    }

    /// Tells the delegate that the ad dismissed full screen content.
    func adDidDismissFullScreenContent(_ ad: GADFullScreenPresentingAd) {
      print("Ad did dismiss full screen content.")
    }
}

How do I display a view on dismiss of the video ad?

答案1

得分: 1

你可以在EarnRewards视图中添加一个@State属性,用于跟踪视频广告的关闭。

// 定义这个属性
@State private var adDismissed = false

var body: some View {
    ZStack {
        VStack {
            if !adDismissed {
                videoAdButton {
                    self.rewardedAdView.showAd {
                        print("奖励用户")
                        self.adDismissed = true
                    }
                }
            }
        }
        
        if adDismissed {
            VStack {
                Text("您观看了一则广告")
                Button(action: {
                    self.adDismissed = false
                }) {
                    Image(systemName: "xmark.circle.fill")
                }
            }
            .padding()
            .background(Color.white)
            .cornerRadius(10)
        }
    }
}
英文:

You can add a @State property in your EarnRewards View which will track the dismiss of the video ad

// Define this
@State private var adDismissed = false

 var body: some View {
ZStack {
        VStack {
            if !adDismissed {
                videoAdButton {
                    self.rewardedAdView.showAd {
                        print("Reward the user")
                        self.adDismissed = true
                    }
                }
            }
        }
        
        if adDismissed {
            VStack {
                Text("You watched an ad")
                Button(action: {
                    self.adDismissed = false
                }) {
                    Image(systemName: "xmark.circle.fill")
                }
            }
            .padding()
            .background(Color.white)
            .cornerRadius(10)
        }
    }
}

huangapple
  • 本文由 发表于 2023年7月6日 19:20:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/76628284.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定