Get callback when ADMOB reward ad is closed without seeing whole ad in ios swift

590 Views Asked by At

I am using reward admob ad in my project with latest sdk. How can i get proper callback that the user has closed the ad in between. I know there is a delegate method of fullscreencontentdelegate which has a function adDidDismiss but in that function i am doing some code block which i perform when i complete watching the ad and it just works fine but what if i closed the ad in between, because what happens is that whether i see the whole ad or not this delegate function gets called and there is no way to differentiate how would i proceed with complete and incomplete ad. Please help me with this.

video link

as in the video first time i am not watching the whole ad and i just close it then also the scratch card popup comes because of the delegate method being called, which i dont want to open and then i just watch the whole ad and get my reward which is working fine

My code snippet:

enum RewardAdType {
    case avatar, freeChips, scratchCard, chips250
}

typealias AD_COMPLETION_BLOCK = (_ success: Bool) -> ()

class RewardAdManager : NSObject
{
    //MARK: - PROPERTIES
    var rewardBasedVideoAd : GADRewardedAd? = nil
    var rewardValue = ""
    var type: RewardAdType? = nil
}

//MARK: - HELPERS
extension RewardAdManager
{
    func loadRewardedAd(vc: UIViewController, userId: String, type: RewardAdType, imageName: String? = nil, chipsCoin: String? = nil, completion: @escaping AD_COMPLETION_BLOCK)
    {
        self.type = type
        let adUnit = self.type == .avatar ? Constants.REWARD_AD_AVATAR_LIVE_ID : Constants.REWARD_AD_WINCHIPS_LIVE_ID
     
        let request = GADRequest()
        GADRewardedAd.load(withAdUnitID: adUnit, request: request) { [weak self] ad, error in
            
            guard let self = self, error == nil else {
                Helpers.hidehud()
                self?.type = nil
                self?.rewardBasedVideoAd = nil
                return
            }
           
            let serverSideVerificationOptions = GADServerSideVerificationOptions()
            serverSideVerificationOptions.userIdentifier = userId
            
            if type == .scratchCard {
                self.rewardValue = self.generateRandomRewardValue()
                serverSideVerificationOptions.customRewardString = self.rewardValue
            } else if type == .avatar {
                serverSideVerificationOptions.customRewardString = imageName
            } else if type == .freeChips {
                serverSideVerificationOptions.customRewardString = chipsCoin
            } else if type == .chips250 {
                serverSideVerificationOptions.customRewardString = "250"
            }
            
            self.rewardBasedVideoAd = ad
            self.rewardBasedVideoAd?.serverSideVerificationOptions = serverSideVerificationOptions
            self.rewardBasedVideoAd?.fullScreenContentDelegate = self
           
            self.showRewardedAd(viewController: vc, type: type, completion: completion)
        }
    }

    func showRewardedAd(viewController: UIViewController, type: RewardAdType? = nil, completion: @escaping AD_COMPLETION_BLOCK)
    {
        Helpers.hidehud()
        
        if let ad = self.rewardBasedVideoAd {
            self.type = type
            DispatchQueueHelper.delay {
                ad.present(fromRootViewController: viewController) {}
            }
            completion(true)
        } else {
            self.type = nil
            self.checkForSavedLanguage(viewController: viewController)
        }
    }
    
    func checkForSavedLanguage(viewController: UIViewController)
    {
        let lang = LanguageCode(rawValue: Defaults[.LangCode]) ?? .english
        viewController.showToast(msg: Constants.NO_ADS_MESSAGE.localizeString(string: lang))
    }
    
    func generateRandomRewardValue() -> String
    {
        var val = 0

        let random = Double.random(in: 0.1...1.0)
       
        if random < 0.20 {
            val = 150
        } else if random < 0.50 {
            val = 200
        } else if random < 0.70 {
            val = 250
        } else {
            val = 350
        }
        
        return val.toString()
    }
}

//MARK: - GADFullScreenContentDelegate
extension RewardAdManager : GADFullScreenContentDelegate {
    
    func ad(_ ad: GADFullScreenPresentingAd, didFailToPresentFullScreenContentWithError error: Error)
    {
        self.type = nil
        Helpers.hidehud()
        let lang = LanguageCode(rawValue: Defaults[.LangCode]) ?? .english
        let userInfo = ["msg":Constants.NO_ADS_MESSAGE.localizeString(string: lang)]
        NotificationCaller.shared.showLeaveMsg(userInfo: userInfo)
    }
  
    func adDidDismissFullScreenContent(_ ad: GADFullScreenPresentingAd)
    {
        guard let type = self.type else { return }
        self.rewardBasedVideoAd = nil
        let userInfo: [String:RewardAdType] = ["type":type]
        NotificationCaller.shared.showRewardTypePopup(userInfo: userInfo)
    }
}
0

There are 0 best solutions below