Home > Enterprise >  iOS is there Rewarded interstitial Ad's Samples in swift language
iOS is there Rewarded interstitial Ad's Samples in swift language

Time:10-25

Before, I used AdMob's banner ad and open app ad in an iOS app. Now, I want to show its Rewarded interstitial ads. I checked the (relevant web page of AdMob), but the examples on this page are all in Objective C language. I am not familiar with Objective C language. Can anyone provide guidance in Swift language? Thanks in advance.

CodePudding user response:

here is a swift example taken from official AdMob documentation

this is Dedicated test ad unit ID for iOS interstitials: ca-app-pub-3940256099942544/4411468910

Load an ad

import GoogleMobileAds
import UIKit

class ViewController {

  private var interstitial: GADInterstitialAd?

  func viewDidLoad() {
    super.viewDidLoad()
    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
                      }
    )
  }
}

Register for callbacks

class ViewController: GADFullScreenContentDelegate {

  private var interstitial: GADInterstitialAd?

  func viewDidLoad() {
    super.viewDidLoad()
    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
                      }
    )
  }

  /// 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 presented full screen content.
  func adDidPresentFullScreenContent(_ ad: GADFullScreenPresentingAd) {
    print("Ad did 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.")
  }
}

Display the ad

@IBAction func doSomething(_ sender: Any) {
  if interstitial != nil {
    interstitial.present(fromRootViewController: self)
  } else {
    print("Ad wasn't ready")
  }
}

CodePudding user response:

Podfile

pod 'Google-Mobile-Ads-SDK'

Sample Code

import GoogleMobileAds
class ViewController:  UIViewController,GADFullScreenContentDelegate,GADBannerViewDelegate {
    var rewardAd: GADRewardedAd?
    //Below code is called to load the reward ad
    if let ad = self.rewardAd {
        ad.present(fromRootViewController: self,
                   userDidEarnRewardHandler: {
                       [self] in
                       //print("Reward received with currency \(reward.amount), amount \(reward.amount.doubleValue)")
                       self.view.makeToast("Reward Points Earned -> ")
                       self.view.makeToast("Thank you for watching ! Free coins Added.")
                       //Show thank you message 
                       //LoadReward Ad again
                       self.loadRewardedAd()
                   })
    }

    else {
        //Failed
        self.view.makeToast("Fail to Load Reward Video : Please try again")
    }

    //required methods
    func loadRewardedAd(){
        GADRewardedAd.load(
            withAdUnitID: Constants.Admob.RewardedAdId, request: GADRequest())
        {
            (ad, error) in
            if let error = error {
                //print("Rewarded ad failed to load with error: \(error.localizedDescription)")
                return
            }
            //print("Loading Succeeded")
            self.rewardAd = ad
            self.rewardAd?.fullScreenContentDelegate = self
        }
    }

    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")
        self.reloadGame()
    }

        else if  type(of: ad) == GADRewardedAd.self {
        //print("RewardedAd")
    }

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