How To Integrate Admob Rewarded Ad Swift

How To Integrate Admob Rewarded Ad Swift

Before integrating Admob rewarded ads into your app, create an Admob account and download the SDK using Cocoapods.

pod 'Google-Mobile-Ads-SDK' # Mobile ads google

Testing Ads

It’s always advised you use a test Ad unit when testing your apps. Testing your app using live ads could lead to your account being terminated or suspended. Admob’s available to test ads:

FormatSizeAd unit ID
Banner320×500ac59b0996d947309c33f59d6676399f
Banner (MRect)300×2502aae44d2ab91424d9850870af33e5af7
Interstitial320×4804f117153f5c24fa6a3a92b818a5eb630
Rewarded Ad320×4808f000bd5e00246de9c789eed39ff6096
Rewarded Playable (MRAID)320×48098c29e015e7346bd9c380b1467b33850
NativeN/A76a3fefaced247959582d2d2df6f4757

Load Ads

The first step in integrating your Ad is by initialising it in your App Delegate. To initialise your Ads, import Admob and configure your ads.

import GoogleMobileAds // Mobile advertising

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

     func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions:    [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        
        // Google services
        FirebaseApp.configure()
        Messaging.messaging().delegate = self
        GADMobileAds.sharedInstance().start(completionHandler: nil)
     }
}

In your UIViewController, import the GoogleMobileAds and set your variables such as your google ad unit and your GADRewardAd.

import UIKit
import GoogleMobileAds // Google Ads

class SettingsViewController: UIViewController {

// Your ad unit
let rewardAdUnit = "YOUR_AD_UNIT"
    
// Ad unit
var rewardedAd: GADRewardedAd?

override func viewDidLoad() {
        super.viewDidLoad()
    }
}

Create a function to be called when users perform certain actions such as when a user clicks a button, closes a view or something similar. The function can be seen below.

@objc func loadRewardedAd(){
        GADRewardedAd.load(
            withAdUnitID: self.rewardAdUnit, request: GADRequest()
            ) { (ad, error) in
              if let error = error {
                print("Rewarded ad failed to load with error: \(error.localizedDescription)")
                return
              }
              print("Loading Succeeded")
                self.rewardedAd = ad
                self.rewardedAd?.fullScreenContentDelegate = self
                if self.rewardedAd != nil {
                         self.rewardedAd?.present(fromRootViewController: self, userDidEarnRewardHandler: {
                  })
                 } else {
                           // Some other error encountered
                    }
            }
    }

Reward User

Place your function or script to reward users in the last function.

extension ViewController: GADFullScreenContentDelegate {
    
    func adDidPresentFullScreenContent(_ ad: GADFullScreenPresentingAd) {
       print("Rewarded ad presented.")
     }

     func adDidDismissFullScreenContent(_ ad: GADFullScreenPresentingAd) {
       print("Rewarded ad dismissed.")
         // Reward user here
     }
}

Going Live

That’s it, you’ve added Admob rewarded ads to your app. Once you are ready to go live, change your app unit to the one you’ve created on Admob.

Monitor Revenue

To monitor your app’s monetisation, set targets and much more download our Motics app. Learn more about it here.

Leave a Reply

Your email address will not be published.