首页 > 解决方案 > iOS 有没有 Swift 语言的 Rewarded interstitial Ad's Samples

问题描述

之前,我在 iOS 应用中使用了 AdMob 的横幅广告和打开应用广告。现在,我想展示它的奖励式插页式广告。我查看了(AdMob 的相关网页),但该页面上的示例都是用 Objective C 语言编写的。我不熟悉Objective C语言。任何人都可以提供 Swift 语言的指导吗?提前致谢。

标签: iosswiftadmob

解决方案


import UIKit
import GoogleMobileAds
class ViewController: UIViewController, GADFullScreenContentDelegate  {
    @IBOutlet weak var btnRwdClick: UIButton!
    @IBOutlet weak var btnInters: UIButton!
    //var rewadAd: GADRewardedAd?
    var rewardInterstitialAd: GADRewardedInterstitialAd?
    var interstitial: GADInterstitialAd?
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        GADRewardedInterstitialAd.load(
            withAdUnitID: "ca-app-pub-3940256099942544/6978759866", request: GADRequest()
        ) { (ad, error) in
            if let error = error {
                print("Rewarded ad failed to load with error: \(error.localizedDescription)")
                return
            }
            print("Loading Succeeded")
            self.rewardInterstitialAd = ad
            self.rewardInterstitialAd?.fullScreenContentDelegate = self
        }
        let request = GADRequest()
        GADInterstitialAd.load(withAdUnitID:"ca-app-pub-3940256099942544/4411468910",
                                        request: request,
                              completionHandler: { [self] ad, error in
                                if let error = error {
                                  print("Failed to load interstitial ad with error: \(error.localizedDescription)")
                                  return
                                }
                                interstitial = ad
                                interstitial?.fullScreenContentDelegate = self
                                                      
                              }
            )
    }
    
    @IBAction func rewadAdTouched(_ sender: Any) {
        if let ad = rewardInterstitialAd {
            
            ad.present(fromRootViewController: self,
                           userDidEarnRewardHandler: {
                             let reward = ad.adReward
                             // TODO: Reward the user.
                            print("Reward received with currency \(reward.amount), amount \(reward.amount.doubleValue)")
                           }
                  )
        } else {
            //Failed
        }
    }
    
    @IBAction func interAdTouched(_ sender: Any) {
        if interstitial != nil {
            interstitial!.present(fromRootViewController: self)
          } else {
            print("Ad wasn't ready")
          }
    }
    func adDidPresentFullScreenContent(_ ad: GADFullScreenPresentingAd) {
        print("Rewarded ad presented.")
    }
    
    func adDidDismissFullScreenContent(_ ad: GADFullScreenPresentingAd) {

            print("Ad dismiss full screen ad")

                if type(of: ad) == GADInterstitialAd.self {

                  print("InterstitialAd")

                   

                }else if  type(of: ad) == GADRewardedAd.self {

                  print("RewardedAd")

                }else if  type(of: ad) == GADRewardedInterstitialAd.self {
                    
                    print("Rewarded InterstitialAd")

                  }

        }
    
    func ad(
        _ ad: GADFullScreenPresentingAd,
        didFailToPresentFullScreenContentWithError error: Error
    ) {
        print("Rewarded ad failed to present with error: \(error.localizedDescription).")
        
    }
    
}

推荐阅读